当使用Reachability类检查互联网连接时,应用程序崩溃 [英] App crashing when using Reachability Classes to check for internet connection

查看:233
本文介绍了当使用Reachability类检查互联网连接时,应用程序崩溃的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用这个代码检查互联网连接,但我得到一个崩溃说 + [Reachability reachabilityForInternetConnection]:无法识别的选择器发送到类0xcbe0c8



我已导入可达性.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];

//检查是否存在到随机主机的路径
self.hostReachable = [[Reachability reachabilityWithHostName:@www.apple.com] retain];
[self.hostReachable startNotifier];

- (void)checkNetworkStatus:(NSNotification *)notice
{
//在网络状态更改后调用

NetworkStatus InternetStatus = [self.internetRechable currentReachabilityStatus];
switch(internetStatus)
{
case NotReachable:
{
NSLog(@互联网已关闭。
// self.internetActive = NO;
break;
}
case ReachableViaWiFi:
{
NSLog(@互联网正在通过WIFI工作。
// self.internetActive = YES;
break;
}
case ReachableViaWWAN:
{
NSLog(@互联网正在通过WWAN工作。
// self.internetActive = YES;
break;
}
}

NetworkStatus hostStatus = [self.hostReachable currentReachabilityStatus];
switch(hostStatus)
{
case NotReachable:
{
NSLog(@到主机服务器的网关关闭。
// self.hostActive = NO;
break;
}
case ReachableViaWiFi:
{
NSLog(@主机服务器的网关通过WIFI工作。
// self.hostActive = YES;
break;
}
case ReachableViaWWAN:
{
NSLog(@主机服务器的网关正在通过WWAN工作。
// self.hostActive = YES;
break;
}
}
}


解决方案>

请确保您的可触及性的版本为:2.2,最近更改了一些可能会导致这个崩溃,如果你不使用2.2。



以下是
版本2.2的链接 Reachability.h Reachability.m



此外,如果有帮助,请继续我的工作代码为同样的任务:



在我的 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];
}

然后,回调:

   - (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];
}
}


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

I've imported Reachability .h/.m and the systemconfig framework. Crash is at line 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.

Here are links to version2.2 of Reachability.h and Reachability.m

Also, if it helps, heres my working code for this same task:.

In my appDidFinishLaunching (hostReachable and internetReachable are ivars of my app delegate):

//....
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];
    }
}

这篇关于当使用Reachability类检查互联网连接时,应用程序崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆