删除 Wi-Fi 网络时,如何在后台获取 iOS 中的可达性通知?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9810585/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 17:27:55  来源:igfitidea点击:

How to get Reachability Notifications in iOS in Background when dropping Wi-Fi network?

iosnetworkingreachability

提问by Tim Camber

I'm using Reachability successfully to determine the status of the network, and to be notified of changes (e.g. Reachability Guide for iOS 4).

我成功地使用 Reachability 来确定网络状态,并收到更改通知(例如iOS 4 的 Reachability Guide)。

My question isn't how to get Reachability up and running, but rather the following.

我的问题不是如何启动和运行 Reachability,而是以下问题。

My AppDelegatehandles the Reachability stuff. The app receives notifications (kReachabilityChangedNotification) while the app is running, and when the app is in the Background (applicationDidEnterBackground:).

AppDelegate处理可达性的事情。应用kReachabilityChangedNotification程序在运行时和应用程序在后台时接收通知 ( ) ( applicationDidEnterBackground:)。

The app is designed to reload a playing audio stream when it notices that it's lost a Wi-Fi connection, e.g. To test, I turned Wi-Fi on and off in Settings, and everything worked perfectly. In real-world testing, I often lose Wi-Fi connectivity when I exit the range of the access point. I've found that Reachability isn't helping me too much in this case. I'm not sure if it's because Reachability notifications don't come through when the screen is locked, or if Reachability doesn't handle the slow diminishing of signal from an increasingly distant Wi-Fi access point, but regardless I can't figure out why the real-world testing doesn't match the idealized case.

该应用程序旨在在发现 Wi-Fi 连接丢失时重新加载正在播放的音频流,例如为了测试,我在“设置”中打开和关闭了 Wi-Fi,一切正常。在实际测试中,当我离开接入点的范围时,我经常会失去 Wi-Fi 连接。我发现 Reachability 在这种情况下对我帮助不大。我不确定是不是因为在屏幕锁定时 Reachability 通知没有通过,或者 Reachability 无法处理来自越来越远的 Wi-Fi 接入点的信号缓慢减弱,但无论如何我都想不通为什么在实际测试的理想情况不符。

This is what my code looks like. I first set up to receive notifications, and start listening to Reachability:

这就是我的代码的样子。我首先设置接收通知,然后开始收听 Reachability:

- (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];    

    ....

    return YES;
}

and then, this is the function that responds to changes in connectivity:

然后,这是响应连接性变化的函数:

- (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");
            break;            
        }
        case ReachableViaWWAN:
        {
            NSLog(@"The internet is working via WWAN!");
            break;            
        }
    }    
}

The notifications come through even when the app is in the background, but they don't in the real-world testing described above.

即使应用程序在后台,通知也会出现,但它们不会在上述的真实世界测试中出现。

Thanks for any help.

谢谢你的帮助。

回答by Arvis

By default in the background state app stays for a short time only, most apps move to the suspended state shortly afterward. That mean the app is in the background but is not executing code. So your custom implemented notification do not work. Must requery NetworkReachability at Wakeup Time in app delegate methodes:

默认情况下,应用程序在后台状态只停留很短的时间,大多数应用程序很快就会进入暂停状态。这意味着应用程序在后台,但没有执行代码。所以你的自定义实现的通知不起作用。必须在应用程序委托方法中的唤醒时间重新查询 NetworkReachability:

applicationWillEnterForeground:
applicationDidBecomeActive 

回答by newenglander

I was working on a VoIP app, which is launched in the background when the iPhone boots up, at which point there might be no network reachability (e.g. if the phone has both a passcode and/or a SIM card with a PIN code). But since the app is launched directly in the background, the delegate method applicationDidEnterBackground:is not called. Instead what I did was use @Hurden's idea directly in application:didFinishLaunchingWithOptions, checking the applicationStateto see if the app was actually starting in the background. This enabled my app to get the kReachabilityChangedNotificationnotification after the phone was unlocked (enabling the WiFi to connect using the stored password).

我正在开发一个 VoIP 应用程序,它在 iPhone 启动时在后台启动,此时可能没有网络可达性(例如,如果手机同时具有密码和/或带有 PIN 码的 SIM 卡)。但是由于应用程序是直接在后台启动的,因此applicationDidEnterBackground:不会调用委托方法。相反,我所做的是直接在 中使用@Hurden 的想法application:didFinishLaunchingWithOptions,检查applicationState应用程序是否真的在后台启动。这使我的应用程序能够kReachabilityChangedNotification在手机解锁后收到通知(使 WiFi 能够使用存储的密码进行连接)。