20

我一直在测试不同的方法来实现当应用程序处于后台时知道设备是否恢复互联网的可能性,所以我测试的第一个代码是 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;
    }
}
}

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

4

2 回答 2

9

如果网络连接发生变化,您将无法更改Live ,要使其正常工作,您可以做的最好Background Fetch的事情就是使用Capabilities. 首先,您需要选中背景模式的复选框: 在此处输入图像描述

然后你需要尽可能多地询问时间间隔越早越好,所以我建议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或更高版本。

于 2016-02-24T23:33:13.137 回答
2

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

后台应用程序响应的唯一后台事件是收到推送通知,这是因为操作系统会唤醒它们这样做,并且只有在用户请求时才会这样做。

于 2012-10-14T09:35:53.183 回答