0

我正在使用 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];

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

4

1 回答 1

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");
}
于 2013-08-28T18:57:49.737 回答