[英]App crashing when using Reachability Classes to check for internet connection
I'm using this code to check for an internet connection but I'm getting a crash saying +[Reachability reachabilityForInternetConnection]: unrecognized selector sent to class 0xcbe0c8
我正在使用此代码检查互联网连接,但我发现崩溃说
+[Reachability reachabilityForInternetConnection]: unrecognized selector sent to class 0xcbe0c8
I've imported Reachability .h/.m and the systemconfig framework. 我已经导入了Reachability .h / .m和systemconfig框架。 Crash is at line
self.internetRechable = [[Reachability reachabilityForInternetConnection] retain];
崩溃是在线
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;
}
}
}
Make sure your Reachability
is at version: 2.2, a few things changed recently that may cause thiscrash if your not using 2.2. 确保你的
Reachability
是版本:2.2,最近发生了一些变化,如果你不使用2.2,可能会导致崩溃。
Here are links to version2.2 of Reachability.h and Reachability.m 以下是Reachability.h和Reachability.m版本2.2的链接
Also, if it helps, heres my working code for this same task:. 此外,如果它有帮助,继承我的工作代码同样的任务:
In my appDidFinishLaunching
( hostReachable
and internetReachable
are ivars of my app delegate): 在我的
appDidFinishLaunching
( hostReachable
和internetReachable
是我的app delegate的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];
}
Then, the callback: 然后,回调:
- (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];
}
}
You should turn ARC off. 你应该关闭ARC。 Go to build phases, select this calls and double click at the right edge and type
转到构建阶段,选择此调用并双击右边缘并键入
-fno -objc -arc
I think you can drop off retain code too. 我想你也可以放弃保留代码。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.