【问题标题】:iOS Reachability and app suspend/resume behavioriOS 可达性和应用挂起/恢复行为
【发布时间】:2013-08-28 18:01:29
【问题描述】:

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

对我做的不正确有什么建议吗?

【问题讨论】:

  • 看看这个链接,stackoverflow.com/questions/4846822/…,它讨论了应用程序委托方法和重新进入前台。你能展示更多你的代码吗?例如,您的应用程序中哪里有这个?它是您的应用程序委托吗?在它的什么地方?
  • 更好地澄清了我的问题。

标签: ios reachability


【解决方案1】:

应用恢复后,试试这个:

//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");
}

【讨论】:

  • 在恢复时,当我知道它已连接时,我仍然无法访问。
  • NSLog 中的 if 似乎不匹配。当netStatus == NotReachable 时,它正在打印reachable
猜你喜欢
  • 1970-01-01
  • 2012-05-23
  • 1970-01-01
  • 1970-01-01
  • 2012-05-20
  • 2011-07-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多