互联网在iOS上可用时的通知

当互联网可用时,是否有可能在我的应用程序中收到通知或其他内容。 我知道可达性和各种东西。 但是,我想要的是启动一些待定NSUrlConnections当互联网变得可用的设备上。 有没有一个简单的方法来做到这一点,因为我不想使用循环线程,不断检查可达性。 有什么build议么?

好的,这里是关于Reachability的非常好的post: http : //www.mikeash.com/pyblog/friday-qa-2013-06-14-reachability.html (检查下面的评论!)
Tldr:当你连接回来的时候你可以阻止,但这个解决scheme并不完美。 没有100%可靠的方法来做到这一点(除了尝试循环),但你可以尝试混合这些方法。

编辑:评论@ Jonah.at.GoDaddy答案:
可达性可以给你两个连接通知错误:误报和错误否定(你可以在WWDC 2011会议上检查它,我不记得哪一个;关于联网有两个)。 所以,我的观点是:你不应该只依靠那些通知。 你可以在状态改变时触发刷新,但应该有另外一种方式(用户交互,或某种主动等待)。

这里是我使用的一些代码…它可能比你需要的更多一点:

 -(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" ); }