How to get Reachability Notifications in iOS in Ba

2019-01-31 07:58发布

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).

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

My AppDelegate handles the Reachability stuff. The app receives notifications (kReachabilityChangedNotification) while the app is running, and when the app is in the Background (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.

This is what my code looks like. I first set up to receive notifications, and start listening to 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.

2条回答
兄弟一词,经得起流年.
2楼-- · 2019-01-31 08:46

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 applicationState to see if the app was actually starting in the background. This enabled my app to get the kReachabilityChangedNotification notification after the phone was unlocked (enabling the WiFi to connect using the stored password).

查看更多
该账号已被封号
3楼-- · 2019-01-31 08:50

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:

applicationWillEnterForeground:
applicationDidBecomeActive 
查看更多
登录 后发表回答