我已经设置了一些方法来检查我的应用程序中的网络状态.
在我的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]; } //]]]]]]
有任何想法吗?
解决方法
我建议使用Apple的Reachability类.
Here是Apple的示例应用程序.
The Reachability sample application demonstrates how to use the SystemConfiguration framework to monitor the network state of an iPhone or iPod touch. In particular,it demonstrates how to know when IP can be routed and when traffic will be routed through a Wireless Wide Area Network (WWAN) interface such as EDGE or 3G.
@H_403_34@