我正在使用此代码来检查互联网连接,但我收到崩溃消息+[Reachability reachabilityForInternetConnection]: unrecognized selector sent to class 0xcbe0c8
我已经导入了可达性.h/.m http://developer.apple.com/library/ios/samplecode/Reachability/Reachability.zip和系统配置框架。崩溃发生在线路上self.internetRechable = [[Reachability reachabilityForInternetConnection] retain];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(checkNetworkStatus:) name:kReachabilityChangedNotification object:nil];
self.internetRechable = [[Reachability reachabilityForInternetConnection] retain];
[self.internetRechable startNotifier];
// check if a pathway to a random host exists
self.hostReachable = [[Reachability reachabilityWithHostName: @"www.apple.com"] retain];
[self.hostReachable startNotifier];
- (void) checkNetworkStatus:(NSNotification *)notice
{
// called after network status changes
NetworkStatus internetStatus = [self.internetRechable currentReachabilityStatus];
switch (internetStatus)
{
case NotReachable:
{
NSLog(@"The internet is down.");
// self.internetActive = NO;
break;
}
case ReachableViaWiFi:
{
NSLog(@"The internet is working via WIFI.");
// self.internetActive = YES;
break;
}
case ReachableViaWWAN:
{
NSLog(@"The internet is working via WWAN.");
// self.internetActive = YES;
break;
}
}
NetworkStatus hostStatus = [self.hostReachable currentReachabilityStatus];
switch (hostStatus)
{
case NotReachable:
{
NSLog(@"A gateway to the host server is down.");
// self.hostActive = NO;
break;
}
case ReachableViaWiFi:
{
NSLog(@"A gateway to the host server is working via WIFI.");
// self.hostActive = YES;
break;
}
case ReachableViaWWAN:
{
NSLog(@"A gateway to the host server is working via WWAN.");
// self.hostActive = YES;
break;
}
}
}
确保您的Reachability
版本:2.2,最近发生了一些变化,如果您不使用 2.2,可能会导致此崩溃。
这里是 version2.2 的链接可达性.h http://developer.apple.com/library/ios/ipad/#samplecode/Reachability/Listings/Classes_Reachability_h.html#//apple_ref/doc/uid/DTS40007324-Classes_Reachability_h-DontLinkElementID_5 and 可达性.m http://developer.apple.com/library/ios/ipad/#samplecode/Reachability/Listings/Classes_Reachability_m.html#//apple_ref/doc/uid/DTS40007324-Classes_Reachability_m-DontLinkElementID_6
另外,如果有帮助,这是我针对同一任务的工作代码:
In my appDidFinishLaunching
(hostReachable
and internetReachable
是我的应用程序委托的 ivars):
//....
if ([[Reachability reachabilityWithHostName:@"google.com"] currentReachabilityStatus] == NotReachable) {
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(checkNetworkStatus:) name:kReachabilityChangedNotification object:nil];
internetReachable = [[Reachability reachabilityForInternetConnection] retain];
[internetReachable startNotifier];
hostReachable = [[Reachability reachabilityWithHostName:@"google.com"] retain];
[hostReachable startNotifier];
}
然后,回调:
- (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;
}
if (internetActive && hostActive) {
[self refreshAllData];
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)