首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >后台可达性检测

后台可达性检测
EN

Stack Overflow用户
提问于 2012-10-01 17:37:51
回答 2查看 6.2K关注 0票数 20

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

但当应用程序处于后台时,此代码不会通知互联网状态。所以我也尝试了下面的代码,当App从后台状态启动到前台时,它可以工作(与Apple reachability示例代码相同)

代码语言:javascript
复制
- (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;
    }
}
}

我的问题是:当应用程序在后台时,当互联网状态发生变化时,如何获得通知?

EN

回答 2

Stack Overflow用户

发布于 2016-02-25 07:33:13

实时你不能改变如果网络连接改变了,你能做的最好的就是从Capabilities使用Background Fetch模式。首先,您需要选中后台模式的复选框:

然后你需要尽可能频繁地询问时间间隔,越快越好,所以我建议使用application:didFinishLaunchingWithOptions:,你需要这样写:

代码语言:javascript
复制
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    [application setMinimumBackgroundFetchInterval:UIApplicationBackgroundFetchIntervalMinimum];
    return YES;
}

UIApplicationBackgroundFetchIntervalMinimum尽可能的频繁,但它并不是从文档中获取的精确秒数:

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

然后,当后台fetch将被触发时,你可以使用方法签入AppDelegate

代码语言:javascript
复制
-(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或更高版本中运行。

票数 9
EN

Stack Overflow用户

发布于 2012-10-14 17:35:53

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

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

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/12670774

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档