Jon*_*Jon 5 iphone crash cocoa-touch objective-c reachability
我正在使用此代码来检查互联网连接,但我正在崩溃说 +[Reachability reachabilityForInternetConnection]: unrecognized selector sent to class 0xcbe0c8
我已经导入了Reachability .h/.m和systemconfig框架.崩溃就行了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;
}
}
}
Run Code Online (Sandbox Code Playgroud)
确保您的Reachability版本是:2.2,最近发生了一些变化,如果您不使用 2.2,可能会导致此崩溃。
以下是Reachability.h和Reachability.m 2.2 版的链接
另外,如果有帮助,这是我针对同一任务的工作代码:
在我的appDidFinishLaunching(hostReachable和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];
}
Run Code Online (Sandbox Code Playgroud)
然后,回调:
- (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];
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2946 次 |
| 最近记录: |