在后台检测可达性
Posted
技术标签:
【中文标题】在后台检测可达性【英文标题】:Detect the reachability in background 【发布时间】:2012-10-01 09:37:51 【问题描述】:我一直在测试不同的方法来实现在应用程序处于后台时知道设备是否恢复互联网的可能性,所以我测试的第一个代码是 Apple 可达性示例代码http://developer.apple.com/library/ios/#samplecode/Reachability/Introduction/Intro.html
但是当应用程序处于后台时,此代码不会通知互联网状态。所以我也尝试了以下代码,当应用程序从后台状态启动到前台时它可以工作(与 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;
我的问题是: 当应用处于后台时,当互联网状态发生变化时,如何获得通知?
【问题讨论】:
【参考方案1】:实时如果网络连接发生变化,您将无法更改,最好您可以使用Capabilities
中的Background Fetch
模式使其正常工作。首先,您需要选中背景模式的复选框:
那么你需要尽可能多地询问时间间隔越早越好所以我建议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 或更高版本中运行。
【讨论】:
我已经按照解释实现了.. 但仍然 performFetchWithCompletionHandler 不调用并且无法检查可达性。【参考方案2】:我认为没有办法在您处于后台时接收可达性通知。处理这个问题的正确方法是检查 AppDelegate 的 - (void)applicationWillEnterForeground:(UIApplication *)application 中的可达性。
后台应用程序响应的唯一后台事件是收到推送通知,这是因为操作系统会唤醒它们这样做,并且仅在用户请求时才发生。
【讨论】:
以上是关于在后台检测可达性的主要内容,如果未能解决你的问题,请参考以下文章