使用可达性类检查互联网连接时应用程序崩溃

2024-03-05

我正在使用此代码来检查互联网连接,但我收到崩溃消息+[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(使用前将#替换为@)

使用可达性类检查互联网连接时应用程序崩溃 的相关文章

  • 在 Objective-C iPad 开发中发布

    我正在尝试发出 POST 请求 但我似乎无法弄清楚出了什么问题 我从服务器收到响应 但我的电子邮件 密码对似乎没有正确发送 读取 由服务器 它告诉我不存在这样的帐户 这是我的代码 它包含在一个函数中 当用户按下我创建的 登录 按钮时调用该函
  • Iphone 上的 Javascript 日期解析

    我正在开发一个针对移动设备的离线 Javascript 网站 iPhone 就是这样的一种移动设备 我正在尝试从 REST API JSON 对象的成员 解析日期 我在用着 Date parse 2010 03 15 10 30 00 这适
  • 如何使 UITextView 像 Notes 应用程序一样使用文本展开

    如何使 UITextView 随其内部的文本展开 你可以试试这个 UITextView textView your UITextView NSString text the text that you want to place in th
  • 动态框架中未定义的架构符号

    我正在开发一个 iOS 框架 该框架包含多个第三方框架并使用 UnitySendMessage C 方法与 Unity 进行通信 我想创建一个动态框架 支持 iOS8 但我偶然发现以下编译错误 Undefined symbols for a
  • DeadSystemException启动服务Android 7

    在过去的几周里 我在我的事故报告中看到 Fatal Exception java lang RuntimeException Unable to start service com MyService ef705d8 with Intent
  • iOS7 无法隐藏状态栏

    我刚刚将我的 iPhone 5 iOS 7 升级到了四个测试版 现在 当我在这台 iPhone 上通过 Xcode 5 运行我的应用程序时 状态栏不会隐藏 尽管它应该隐藏 不工作 UIApplication sharedApplicatio
  • 防止 Windows 中的崩溃消息

    我的应用程序需要扫描经常导致崩溃的第三方文件 为了克服这个问题 它使用一个单独的进程来扫描这些文件并 每当这个进程崩溃时 我的应用程序就会实例化另一个应用程序 我的问题是 每次崩溃后我都会收到 Windows 崩溃消息 AuxScanner
  • 对 Objective-c 对象数组进行排序

    所以我有一个自定义课程Foo有许多成员 interface Foo NSObject NSString title BOOL taken NSDate dateCreated 在另一堂课上我有一个NSMutableArray包含这些对象的列
  • iPhone 表格部分页眉和页脚中的文本

    我有一个关于在表视图部分的页眉和页脚中打印文本的问题 我想在每行的页脚中显示一些文本 但这样做时遇到问题 当我开始下一部分时 它从第一个部分的下面开始绘制 要了解我到底需要什么 请转到 设置 gt 常规 gt 网络 在此屏幕中 我们有多个部
  • 如何在 iPhone 上使用 glib? [关闭]

    Closed 这个问题需要多问focused help closed questions 目前不接受答案 这几天我正在做一个项目 我的项目中包含了一个第三方库 该库是基于glib的 这真的让我抓狂 大家能告诉我我该怎么办吗 如何在 iPho
  • 为什么分割视图控制器必须始终是您创建的任何界面的根?

    在苹果的开发者指南中 他们指出 分割视图控制器必须始终是您创建的任何界面的根 see here http developer apple com library ios featuredarticles ViewControllerPGfo
  • 锁定时 AVAudioPlayer 在 iPhone 5 中不播放音频

    使用 AVAudioPlayer 我尝试在 iphone 播放器播放时播放声音 当设备锁定时也是如此 问题是 在 iPhone 4s ios 7 中 效果很好 但在带有 6 和 7 ios 的 iPhone 5 上没有任何反应 In the
  • MPMoviePlayerViewController,删除 Quicktime 符号/添加背景图像?

    我有一个播放音频的 MPMoviePlayerViewController 我想删除 Quicktime 徽标和 或向播放器添加自定义背景图像 但保留播放控件 我发誓我在 iOS 5 之前就已经这样做过 但我无法重新弄清楚 我尝试过的事情
  • 使用 popToRootViewController 时我丢失了导航栏

    我有一个 iOS 应用程序 其中主屏幕是 UICollectionViewController 从集合视图中选择项目时 视图将推送到该项目的详细信息视图 在细节视图中 我构建了一个从侧面移出的抽屉 滑块 为了让视图看起来像我想要的那样 我隐
  • 检索旋转的 UIImageView 的所有 4 个坐标

    如何获取 UIImageView 的 4 个坐标 我知道可以得到CGRect以及origin x和origin y 但是如何找到所有4个角呢 编辑 我正在旋转 UIImageViews 这就是我问的原因 P 您可以添加矩形的宽度和高度来获取
  • ObjC 中的 self 是什么?我应该什么时候使用它?

    什么是self在 Objective C 中是什么意思 我应该何时何地使用它 是否类似于this在Java中 self指的是您正在使用的当前类的实例 是的 它类似于this在爪哇 如果您想对该类的当前实例执行操作 则可以使用它 例如 如果您
  • dequeueReusableCellWithIdentifier: 如何工作?

    我想要一些精确的信息dequeueReusableCellWithIdentifier kCellIdentifier 如果我理解得很好 下面的 NSLOG 应该只打印一次 但事实并非如此 那么 dequeueReusableCell 的意
  • AFNetworking 2.0 可达性不起作用

    我在使 AFNetworking Reachability 模块正常工作时遇到一些问题 我已经使用 ReachabilityStatusChangeBlock 设置了 AFHTTPRequestOperationManager 但它从未被调
  • 如何知道我的 Xcode iPhone 项目是否使用 ARC?

    我想知道我的 Xcode iPhone 项目是否正在使用 ARC 但我不记得在创建项目时是否勾选了该框 我怎样才能得到这些信息 选择您的项目 然后构建设置 寻找Objective C 自动引用计数 in the Apple LLVM 编译器
  • 如何在 iOS 中通过 wifi 传输大文件

    我下载了WiTap http developer apple com library ios samplecode WiTap Introduction Intro html代码来自苹果网站 它用于通过本地 WiFi 网络传输数据 我正在从

随机推荐