iOS可访问性和应用程序暂停/恢复行为

时间:2013-08-28 18:01:29

标签: ios reachability

我正在使用Apple的Reachability示例代码来检测网络连接更改,并想知道一旦应用程序返回到前台,人们将如何获得准确的网络状态。

在我目前可见的UIViewController中,我正在注册为kReachabilityChangedNotification的观察者(与Reachability示例代码相同),并且还在侦听UIApplicationWillEnterForegroundNotification和UIApplicationWillResignActiveNotification事件。

在我当前可见的UIViewController中,就在应用程序暂停之前:

// While the app is in the foreground, I configure SCNetworkReachabilitySetCallback 
// At this point, I have lost connectivity.

[reachability startNotifier];

// this returns NotReachable, as expected
NetworkStatus status = [reachability currentReachabilityStatus];

现在暂停应用并更改wifi设置(更改网络设置以建立连接)

After app resume: (in the UIApplicationWillEnterForegroundNotification handler)
// this still returns NotReachable even though I now have connectivity.
NetworkStatus status = [reachability currentReachabilityStatus];

关于我做得不对的任何建议?

1 个答案:

答案 0 :(得分:0)

应用程序恢复后,请尝试以下操作:

//Reachability is the class from Apple's Reachability sample

Reachability *reachability = [Reachability reachabilityForLocalWiFi];

NetworkStatus netStatus = [reachability currentReachabilityStatus];

if (netStatus == NotReachable)
{
    NSLog(@"Not Reachable");
} else
{
    NSLog(@"Reachable");
}