Checking for Internet Connection in an iApp
Checking for Internet Connection in an iApp
- Subject: Checking for Internet Connection in an iApp
- From: John Love <email@hidden>
- Date: Sun, 16 Oct 2011 17:45:05 -0400
I really have been just about everywhere, e.g., stack overflow.com and iphonedevsdk.com and have really learned a lot … unfortunately, what I have learned is that there are opposing opinions on how to best check for an active internet connection with respect to effectiveness and speed. Such comments such as "You're not testing for what you think you are!".
Considering my lack of success with Apple's Reachability Code, I have found that the asynchronous call to:
NSString *urlString = @"myURL";
NSURL *theURL = [NSURL URLWithString:urlString];
NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:theURL];
NSURLConnection *theConnection = [NSURLConnection connectionWithRequest:theRequest delegate:self];
which does hook up with the delegate method:
- (void )connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
This magic does indeed work, but I could fry a couple of eggs while I'm waiting and I know that the iApp folks will bounce me for that delay.
Maybe my method of "taking my connection" down may be in error -- I am simply pushing off the "standby" button on my cable modem -- I have also just unplugged the modem.
With respect to my attempts in using Apple's Reachability code:
Here are the two properties:
Reachability *itsInternetReachability;
BOOL itsInternetActive;
This method is called when I start my app:
- (void )startObserveInternetConnection
{
self.itsInternetReachability = [Reachability reachabilityForInternetConnection];
NSNotificationCenter *notificationCenter = [NSNotificationCenter defaultCenter];
[notificationCenter addObserver:self
selector:@selector(pingInternetConnection:)
name:kReachabilityChangedNotification
object:internetReachability_];
[internetReachability_ startNotifier];
}
- (void )pingInternetConnection:(NSNotification *)sentNotification
{
if ([[sentNotification object] isKindOfClass:[Reachability class]])
{
NetworkStatus internetStatus = [self.itsInternetReachability currentReachabilityStatus];
self.itsInternetActive = (internetStatus != NotReachable);
}
else
{
self.itsInternetActive = NO;
}
}
Isn't the function of -startNotifier to enable the callback method to be continuously called within the main RunLoop?
Yet the only time I can get this callback method to be called is if I call NSNotificationCenter's -postNotificationName elsewhere in the code??
-[notificationCenter postNotificationName:kReachabilityChangedNotification object:internetReachability_];
Triple thanks …
John Love
Touch the Future! Teach!
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden