iOS Reachability Not Recognizing When Host is Removed

℡╲_俬逩灬. 提交于 2019-12-24 05:32:48

问题


I am building an application and trying to check and see if a device is still available on the network (by connecting to the devices IPAddress). I am using reachability to confirm that it is available.

When I network access for the iOS device (turn on airplane mode for example) everything works properly, but if I remove the device from the network, reachability does not seem to notice the change.

It seems like reachability is caching the results, and not seeing the update.


回答1:


Don't use reachability then!

Use this bit of code instead which works a treat;

NSString *connected = [NSString stringWithContentsOfURL:[NSURL URLWithString:@"http://www.google.com"]];
wait(25000);
if (connected == NULL) {

NSLog(@"Not Connected"); 
//Code to show if not connected

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:@"Oops! You aren't connected to the Internet." 
delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alert show];
[alert release];

} else {
NSLog(@"Connected Successfully!");
//Any other code for successful connection
}



回答2:


The SCReachability API only checks if the local hardware is configured such that it could reach the specified address; it does not actually attempt to reach it. To determine if the target is alive and kicking, you must attempt to open a connection to it.




回答3:


Check out this answer. While the pixelbit's answer is viable, burtlo is right to bring up that just waiting 25 seconds isn't a great idea. Using the NSURLConnection is much cleaner.




回答4:


Apple updated the Reachabilty class file. You can test it with latest Xcode. here is link: Apple Reachabilty Sample Code

I tested with Xcode 8.3.1 on iPhone 6 version 10.3.1. It will notify user for change in network status.



来源:https://stackoverflow.com/questions/7015644/ios-reachability-not-recognizing-when-host-is-removed

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!