在我的应用程序中,我使用来自服务器的 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/