检测后台的可达性

Ben*_*Ben 20 multithreading background reachability ios swift

我一直在测试不同的方法,以实现在应用程序处于后台时设备是否能够恢复互联网的可能性,因此我测试的第一个代码是Apple可访问性示例代码http://developer.apple.com/library/ios /#samplecode/Reachability/Introduction/Intro.html

但是,当应用程序处于后台时,此代码不会通知Internet状态.所以我也尝试了下面的代码,当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;
    }
}
}
Run Code Online (Sandbox Code Playgroud)

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

Jak*_*kub 9

如果网络连接发生变化,你无法改变直播,你可以做的最好的工作就是使用Background Fetch模式Capabilities.Firstable你需要选中后台模式的复选框: 在此输入图像描述

然后,你需要问的时间间隔往往你能越快越好,所以我建议application:didFinishLaunchingWithOptions:,你需要把这个行:

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

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);
}
Run Code Online (Sandbox Code Playgroud)

所有这些都适用于iOS 7.0或更高版本.


Gor*_*ove 2

我认为没有办法在您处于后台时接收可达性通知。处理此问题的正确方法是检查 AppDelegate 的 - (void)applicationWillEnterForeground:(UIApplication *)application 中的可访问性。

后台应用程序反应的唯一后台事件是接收推送通知,这是因为操作系统会唤醒它们来执行此操作,并且仅在用户请求时才这样做。