Is it possible to receive a notification or something inside my app when the internet BECOMES available. I'm aware of reachability and all kinds of stuff. But what i want is to start some pending NSUrlConnections when internet becomes available on the device. Is there a easy way to do this, because I don't want to use a looping thread that checks constantly for reachability. Any suggestions?
Ok, here is very nice post about Reachability: http://www.mikeash.com/pyblog/friday-qa-2013-06-14-reachability.html (check comments below!)
Tldr: You can fire block when your connection is back, but this solution is not perfect. There is no 100% reliable way to do this (except trying in loop), but you can try to mix those approaches.
Edit: Comment to @Jonah.at.GoDaddy answer:
Reachability can give you both connection notification errors: false positive and false negative (you can check it at WWDC 2011 session, I don’t remember which one; there are two about networking). So, my point is: you should never rely only on those notification. You can trigger refresh on status change, but there should be another way of doing it (user interaction, or some kind of active waiting).
Here is some code that I use...it may be a bit more than you need:
-(void)checkNetworkStatus
{
// check for internet connection
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(checkNetworkStatus:) name:kReachabilityChangedNotification object:nil];
// check if a pathway to a random host exists
self.hostReachable = [Reachability reachabilityWithHostname:@"google.com"];
[self.hostReachable startNotifier];
}
-(void) checkNetworkStatus:(NSNotification *)notice
{
NetworkStatus hostStatus = [self.hostReachable currentReachabilityStatus];
switch (hostStatus)
{
case NotReachable:
{
DDLogInfo(@"A gateway to the host server is down.");
if( self.canReachGoogle )
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle: LOCALIZED_NoInternetConnection
message: LOCALIZED_ConnectionNeeded
delegate:self cancelButtonTitle:LOCALIZED_Ok otherButtonTitles:nil];
[alert show];
}
self.canReachGoogle = NO;
break;
}
case ReachableViaWiFi:
{
DDLogInfo(@"A gateway to the host server is working via WIFI.");
self.canReachGoogle = YES;
break;
}
case ReachableViaWWAN:
{
DDLogInfo(@"A gateway to the host server is working via WWAN.");
self.canReachGoogle = YES;
break;
}
}
DDLogInfo(@"Network connection has changed and is now: %@", self.canReachGoogle ? @"enabled" : @"disabled" );
}
© 2022 - 2024 — McMap. All rights reserved.
startNotifier
and you'll get a notification when the Internet connectivity changes. – Prentice