我已经设置了一些方法来检查我的应用程序中的网络状态。
在我的viewDidLoad
方法中,我调用initNetworkCheck
:
[self initNetworkCheck];
[super viewDidLoad];
if(internetActive == NO){
compose.enabled = NO;
}
所以我想在启动时检查硬件是否有互联网连接。问题是它总是给我 NO,但当我记录它时,internetActive 实际上是 YES。
//[[[[[[network check methods
-(void)checkNetworkStatus:(NSNotification *)notice{
NetworkStatus internetStatus = [internetReachable currentReachabilityStatus];
switch (internetStatus) {
case NotReachable:{
self.internetActive = NO;
break;
}
case ReachableViaWiFi:{
self.internetActive = YES;
break;
}
case ReachableViaWWAN:{
self.internetActive = YES;
break;
}
}
NetworkStatus hostStatus = [hostReachable currentReachabilityStatus];
switch (hostStatus){
case NotReachable:{
self.hostActive = NO;
break;
}
case ReachableViaWiFi:{
self.hostActive = YES;
break;
}
case ReachableViaWWAN:{
self.hostActive = YES;
break;
}
}
}
-(void)initNetworkCheck{
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(checkNetworkStatus:) name:kReachabilityChangedNotification object:nil];
internetReachable = [[Reachability reachabilityForInternetConnection] retain];
[internetReachable startNotifier];
hostReachable = [[Reachability reachabilityWithHostName:@"www.google.com"] retain];
[hostReachable startNotifier];
}
//]]]]]]
有任何想法吗?