iphone - 在启动 iPhone 应用程序期间检查互联网连接

标签 iphone

在我的应用程序中,我使用来自服务器的 XML 文件。

所以在应用程序启动期间我想检查互联网连接。

如果没有互联网连接,那么我想显示告诉用户退出的屏幕。

我该如何实现它。

最佳答案

Apple sample code 导入“可达性”文件项目并像这样启动它:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(checkNetworkStatus:) name:kReachabilityChangedNotification object:nil];
internetReachable = [[Reachability reachabilityForInternetConnection] retain];
[internetReachable startNotifier];

hostReachable = [[Reachability reachabilityWithHostName:@"yourwebsite.com"] retain];
[hostReachable startNotifier];

然后检查互联网连接,如下所示。这非常方便,因为您可以随时在需要时即时检查连接。

if ([internetReachable currentReachabilityStatus]==NotReachable ||
    [hostReachable currentReachabilityStatus]==NotReachable) {
    // react appropriately
}

不用担心这会减慢你的速度 - 它是异步的。在确保 UI 符合用户期望后,您可以对 Internet 的启动或关闭使用react。

关于iphone - 在启动 iPhone 应用程序期间检查互联网连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7072111/

相关文章:

ios - ios8 中 iphone 的 UIPopoverController 显示白屏

iphone - 调用了dismissViewControllerAnimated,但没有解除ViewController

iphone - 当点击网页内播放 YouTube 视频时,iOS 6.1 UIWebview 崩溃

iphone - 如何在 ASP.NET 3.5 中开发可在 iPhone 3G 上查看的基于 Web 的应用程序?

iphone - 在 Objective-C 中将 AppleScript 发送到远程计算机

iphone - NSUserDefaults setObject 和 IntegerForKey,在同一个键上使用,如果不是,我该如何解决?

ios - 核心数据一对多关系

iphone - 我如何给 UIButton 提供外部链接?可能吗?

iphone - 如何在不同对象之间共享一个 UIManagedDocument?

iphone - NSMutableArray 访问问题