【问题标题】:App crashing when using Reachability Classes to check for internet connection使用可达性类检查互联网连接时应用程序崩溃
【发布时间】:2011-10-18 18:42:57
【问题描述】:

我正在使用此代码检查互联网连接,但我遇到了一个崩溃提示 +[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;
        }
    }
}

【问题讨论】:

  • 不,我已经完成了@class Reachability 并且我也已经导入了
  • 是的,我已经导入了文件,我已经完成了#import "Reachability.h"
  • 我做到了,它在Reachability *newReach = [Reachability reachabilityForInternetConnection];崩溃了
  • 从 Reachability 2.0 升级到 2.2 后,我现在收到此错误:` *** 由于未捕获的异常 'NSInvalidArgumentException' 导致应用程序终止,原因:'+[Reachability reachabilityForInternetConnection]:无法识别的选择器发送到类 0xcbf0c4'`
  • 另外两个问题是在黑暗中拍摄的。 1)您的源文件是否已从原始苹果文件损坏/更改? 2) 如果您在 xCode 中使用工作区,有时文件将在工作区中,但不在项目中。当您折叠项目披露三角形时,它的文件将折叠掉。是这样吗?

标签: iphone objective-c cocoa-touch crash reachability


【解决方案1】:

确保您的Reachability 版本为:2.2,最近发生了一些更改,如果您不使用 2.2,可能会导致此崩溃。

这里是version2.2的链接 Reachability.hReachability.m

另外,如果有帮助,这是我用于同一任务的工作代码:。

在我的 appDidFinishLaunching 中(hostReachableinternetReachable 是我的应用代理的 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];
    }
}

【讨论】:

    【解决方案2】:

    您应该关闭 ARC。 转到构建阶段,选择此调用并双击右边缘并键入

    -fno -objc -arc
    

    我认为您也可以放弃保留代码。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-06-09
      • 1970-01-01
      • 1970-01-01
      • 2016-06-08
      • 1970-01-01
      相关资源
      最近更新 更多