Iphone Sdk check internet connection auotmatically in middle of the app?
Iam currently checking the internet connection when i start the app first time but if i close the app and turn on the airplane mode and then run the app back its working or it should check the internet connection if i lose 开发者_如何学运维my wifi and it should say your internet connection lost or somthing like that. Any one have proper answer for this?
Use the Reachability API. Here's some code from the app delegate file of one of my projects.
// ivars
Reachability* hostReach;
Reachability* internetReach;
Reachability* wifiReach;
- (void) reachabilityChanged: (NSNotification* )note {
Reachability *curReach = (Reachability *)[note object];
if ([curReach currentReachabilityStatus] == NotReachable) {
UIAlertView *alert = [[[UIAlertView alloc] init] autorelease];
alert.title = @"No network connection?";
alert.message = @"No network connection.";
alert.delegate = self;
[alert addButtonWithTitle:@"OK"];
[alert show];
}
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
[[NSNotificationCenter defaultCenter] addObserver: self selector: @selector(reachabilityChanged:) name: kReachabilityChangedNotification object: nil];
hostReach = [[Reachability reachabilityWithHostName: @"www.test.com"] retain];
[hostReach startNotifier];
internetReach = [[Reachability reachabilityForInternetConnection] retain];
[internetReach startNotifier];
wifiReach = [[Reachability reachabilityForLocalWiFi] retain];
[wifiReach startNotifier];
// controller setup
viewController = [[CFSplashViewController alloc] init];
[window addSubview:viewController.view];
[window makeKeyAndVisible];
return YES;
}
// and of course, remember to release all those resources..
精彩评论