Reachability - for the local IP address of the local network

I'm trying to check device availability in WIFI

I have modified code from Maher Ali's book “Advanced IOS4 Programming” to use the IP address as follows

- (BOOL) networkConnected: (NSString *) addr { SCNetworkReachabilityFlags flags = 0; SCNetworkReachabilityRef netReachability; BOOL retrievedFlags = NO; // added the "if" and first part of if statement // if (hasLeadingNumberInString(addr)) { struct sockaddr_in the_addr; memset((void *)&the_addr, 0, sizeof(the_addr)); the_addr.sin_family = AF_INET; the_addr.sin_port = htons(1001); const char* server_addr = [addr UTF8String]; unsigned long ip_addr = inet_addr(server_addr); the_addr.sin_addr.s_addr = ip_addr; netReachability = SCNetworkReachabilityCreateWithAddress(kCFAllocatorDefault, (struct sockaddr*)&the_addr); } else { netReachability = SCNetworkReachabilityCreateWithName(kCFAllocatorDefault, [addr UTF8String]); } if (netReachability) { retrievedFlags = SCNetworkReachabilityGetFlags(netReachability, &flags); CFRelease(netReachability); } if (!retrievedFlags || !flags) { return NO; } return YES; } 

But it always returns NO using the IP address and port, which, as I know, I can connect to (i.e. if I skip this check, everything will be fine)

I am trying to connect to port "10.0.0.59" 1001, which I can do

it compiles - and I stepped (on ipod) through it, and it works through the code socket section.

netReachability returns as zero.

Relevant Headings Included

 #import "Reachability.h" #include <CFNetwork/CFSocketStream.h> #include <arpa/inet.h> 

Can anyone determine what I am doing wrong?

+4
source share
2 answers

I use Tony Million a newer achievement ability that makes such things very easy. To do this, you can simply do something like:

 if([[Reachability reachabilityForLocalWiFi] isReachable]) { // It reachable on wifi } 
+1
source

Not sure if this help, but I use Reachability.h from apple every time I want to check if the application is connected to the Internet (or any internal IP)

here is my code

 #import "Reachability.h" if([[Reachability sharedReachability] internetConnectionStatus] == NotReachable) { UIAlertView *myAlert = [[UIAlertView alloc] initWithTitle:@"No Internet Connection" message:@"You require an internet connection via WiFi or cellular network for location finding to work." delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil]; [myAlert show]; [myAlert release]; }else{ // Do something } 
0
source

Source: https://habr.com/ru/post/1345187/


All Articles