我是 iOS 开发新手,正在努力让reachability.h 类正常工作。这是我的视图控制器代码:
- (void)viewWillAppear:(BOOL)animated
{
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:@selector(checkNetworkStatus:)
name:kReachabilityChangedNotification
object:nil];
internetReachable = [Reachability reachabilityForInternetConnection];
[internetReachable startNotifier];
}
- (void)checkNetworkStatus:(NSNotification *)notice {
NetworkStatus internetStatus = [internetReachable currentReachabilityStatus];
NSLog(@"Network status: %i", internetStatus);
}
看起来不错,但运行应用程序并切换到该视图时,xcode 控制台中没有出现任何内容。
我正在使用 Reachability 2.2 和 iOS 4.2。
有什么明显的事情表明我做错了吗?
EDITED:如果您想在某些代码执行之前检查可达性,您应该使用
Reachability *reachability = [Reachability reachabilityForInternetConnection];
NetworkStatus internetStatus = [reachability currentReachabilityStatus];
if (internetStatus != NotReachable) {
//my web-dependent code
}
else {
//there-is-no-connection warning
}
您还可以在某处添加可达性观察者(即在viewDidLoad
):
Reachability *reachabilityInfo;
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(myReachabilityDidChangedMethod)
name:kReachabilityChangedNotification
object:reachabilityInfo];
别忘了打电话[[NSNotificationCenter defaultCenter] removeObserver:self];
当您不再需要可达性检测时(即在dealloc
方法)。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)