Checking internet connectivity during startup iphone application
In my application i am using XML file from server.开发者_开发百科
So during start up of application i want to check internet connectivity.
If internet connectivity is not there then i want to show screen which tells user to exit .
How can i implement that.
Import the "Reachability" files from the Apple sample code project and start it up like this:
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(checkNetworkStatus:) name:kReachabilityChangedNotification object:nil];
internetReachable = [[Reachability reachabilityForInternetConnection] retain];
[internetReachable startNotifier];
hostReachable = [[Reachability reachabilityWithHostName:@"yourwebsite.com"] retain];
[hostReachable startNotifier];
Then check for internet connection as below. It is very convenient, as you can always check the connection on the fly when you need it.
if ([internetReachable currentReachabilityStatus]==NotReachable ||
[hostReachable currentReachabilityStatus]==NotReachable) {
// react appropriately
}
Don't worry about this slowing you down - it is asynchronous. You can react to the Internet being up or down after making sure the UI does what the user expects.
Um, you actually should not do this during startup, because it can make things very slow.
Right after startup you could use SCNetworkReachability.
It's a phone, with lots of radio power save modes, which can take a while (a variable amount of time) to turn on and (re)connect. Just try to download your XML asynchronously, and let the user decide if the connection is taking too long.
Otherwise you will risk telling the user to exit just after you've wasted radio power and the connection comes live again.
精彩评论