检测背景中的可达性

我一直在testing不同的方式来实现的可能性,以知道设备上网时,它的应用程序是在后台,所以我testing的第一个代码是苹果的可达性示例代码http://developer.apple.com/library/ios /#samplecode/Reachability/Introduction/Intro.html

但是这个代码在后台应用程序中不会通知互联网状态。 所以我也尝试了下面的代码,当App从Background状态启动到前台时(与Apple可达性示例代码相同)

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { // check for internet connection [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(checkNetworkStatus:) name:kReachabilityChangedNotification object:nil]; // Set up Reachability internetReachable = [[Reachability reachabilityForInternetConnection] retain]; [internetReachable startNotifier]; ... } - (void)applicationDidEnterBackground:(UIApplication *)application { // check for internet connection [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(checkNetworkStatus:) name:kReachabilityChangedNotification object:nil]; // Set up Reachability internetReachable = [[Reachability reachabilityForInternetConnection] retain]; [internetReachable startNotifier]; } - (void)checkNetworkStatus:(NSNotification *)notice { // called after network status changes NetworkStatus internetStatus = [internetReachable currentReachabilityStatus]; switch (internetStatus) { case NotReachable: { NSLog(@"The internet is down."); break; } case ReachableViaWiFi: { NSLog(@"The internet is working via WIFI"); //Alert sound in Background when App have internet again UILocalNotification *notification = [[[UILocalNotification alloc] init] autorelease]; if (notification) { [notification setFireDate:[NSDate date]]; [notification setTimeZone:[NSTimeZone defaultTimeZone]]; [notification setRepeatInterval:0]; [notification setSoundName:@"alarmsound.caf"]; [notification setAlertBody:@"Send notification internet back"]; [[UIApplication sharedApplication] scheduleLocalNotification:notification]; } break; } case ReachableViaWWAN: { NSLog(@"The internet is working via WWAN!"); //Alert sound in Background when App have internet again UILocalNotification *notification = [[[UILocalNotification alloc] init] autorelease]; if (notification) { [notification setFireDate:[NSDate date]]; [notification setTimeZone:[NSTimeZone defaultTimeZone]]; [notification setRepeatInterval:0]; [notification setSoundName:@"alarmsound.caf"]; [notification setAlertBody:@"Send notification internet back"]; [[UIApplication sharedApplication] scheduleLocalNotification:notification]; } break; } } } 

我的问题是: 当应用程序在后台时,互联网状态发生变化时,通知的方式 什么?

如果networking连接发生变化, Live就无法更改,但可以做的最好的方法是使用Capabilities Background Fetch模式。 首先,您需要选中背景模式checkbox: 在这里输入图像说明

然后你需要尽可能经常地询问时间间隔所以我build议application:didFinishLaunchingWithOptions:你需要把这一行:

 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { [application setMinimumBackgroundFetchInterval:UIApplicationBackgroundFetchIntervalMinimum]; return YES; } 

UIApplicationBackgroundFetchIntervalMinimum是尽可能经常的,但是从文档中提取的时间不是确切的秒数:

系统支持的最小提取间隔。

然后当背景获取将触发你可以检查AppDelegate的方法:

 -(void)application:(UIApplication *)application performFetchWithCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler{ Reachability *reachability = [Reachability reachabilityForInternetConnection]; [reachability startNotifier]; NetworkStatus status = [reachability currentReachabilityStatus]; switch (status) { case NotReachable: { NSLog(@"no internet connection"); break; } case ReachableViaWiFi: { NSLog(@"wifi"); break; } case ReachableViaWWAN: { NSLog(@"cellurar"); break; } } completionHandler(YES); } 

所有这些都可以在iOS 7.0或更高版本中使用。

我不相信有一种方法可以在后台收到可达性通知。 处理这个问题的正确方法是在AppDelegate的 – (void)applicationWillEnterForeground:(UIApplication *)应用程序中检查可达性。

后台应用程序反应的唯一后台事件是收到推送通知,这是因为操作系统将其唤醒,然后才由用户请求。

你的应用程序应该是多任务(VoIP,LOCATION或audio)。 通过这种方式,您可以在后台应用程序中捕捉networking的变化。