我一直在测试不同的方式来实现在应用程序处于后台时设备是否能够恢复互联网的可能性,因此我测试的第一个代码是Apple可访问性示例代码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;
}
}
}
我的问题是: 当应用处于后台时互联网状态发生变化时获得通知的方式是什么?
答案 0 :(得分:8)
直播如果网络连接发生变化,你无法改变,你可以做的非常好是使用来自{{1的Background Fetch
模式}}。 Firstable你需要选中后台模式的复选框:
然后你需要尽可能频繁地询问时间间隔 越快越好,所以我建议你Capabilities
,你需要把这一行:
application:didFinishLaunchingWithOptions:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
[application setMinimumBackgroundFetchInterval:UIApplicationBackgroundFetchIntervalMinimum];
return YES;
}
它尽可能频繁,但它不是从文档中提取之间的确切秒数:
系统支持的最小提取间隔。
然后,当背景提取触发时,您可以使用方法
检入UIApplicationBackgroundFetchIntervalMinimum
AppDelegate
所有这些都适用于 iOS 7.0 或更高版本。
答案 1 :(得分:2)
我不相信当你在后台时有办法接收可达性通知。处理这个问题的正确方法是检查AppDelegate的 - (void)applicationWillEnterForeground:(UIApplication *)应用程序中的可达性。
后台应用程序响应的唯一背景事件是接收推送通知,这是因为操作系统将其唤醒,然后仅在用户请求时将其唤醒。