iphone - RestKit 可达性 null

标签 iphone objective-c ios restkit reachability

我的 AppDelegate.m 中有以下代码 - NSLog 的结果始终是 (null),因此条件没有可达性永远不会被解雇。我想知道为什么会这样/我做错了什么。

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    [IKRestKitManager configureRestKit];

    self.window.rootViewController = self.tabBarController;

    [self.window makeKeyAndVisible];

    [self prepareForLogin];

    return YES;
}

#pragma mark - onstart

- (void)prepareForLogin {

    if ([[[RKClient sharedClient] reachabilityObserver] isReachabilityDetermined] && ![[RKClient sharedClient] isNetworkReachable]) {
        UIAlertView *reachAV = [[UIAlertView alloc] initWithTitle:@"Cannot connect to Internet" message:@"iK9 cannot reach the Internet. Please be sure that your device is connected to the Internet and try again." delegate:self cancelButtonTitle:@"Retry" otherButtonTitles:nil];
        reachAV.tag = 0;
        [reachAV show];
    }

    NSLog(@"%@",[[[RKClient sharedClient] reachabilityObserver] isReachabilityDetermined]);


    if (![IKUserController loggedInUser]) {
        IKLoginViewController *loginVC = [[IKLoginViewController alloc] init];
        loginVC.scenario = SCENARIO_NEW;
        [self.window.rootViewController presentModalViewController:loginVC animated:YES];
    }
}

最佳答案

根据 RKReachabilityObserver 的文档:

When initialized, RKReachabilityObserver instances are in an indeterminate state to indicate that reachability status has not been yet established. After the first callback is processed by the observer, the observer will answer YES for reachabilityDetermined and networkStatus will return a determinate response.

您需要等到可达性状态确定后,再检查连接是否可用。这就是您的第一个 if 语句未触发的原因。

要监视此更改,请设置通知观察器(来自 this Stack Overflow question ):

[[NSNotificationCenter defaultCenter] addObserver:self 
                                      selector:@selector(reachabilityStatusChanged:) 
                                      name:RKReachabilityDidChangeNotification object:nil];

关于iphone - RestKit 可达性 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10377622/

相关文章:

iphone - 我的应用程序如何保存和打开 iPhone 照片库中的照片?

ios - 如何在屏幕上已经有触摸的情况下激活 UIGestureRecognizer?

iphone - UINavigationController - 实现返回开始按钮

ios - 在 UITableViewCells 上显示 'Copy' 弹出窗口的简单方法,如地址簿 App

ios - sendAsynchronousRequest 使 UI 卡住

iphone - 如何在 AQClient 线程中追踪 iPad 应用程序的奇怪崩溃?

ios - 未绘制 Mkpolyline

ios - 使用Core Bluetooth时,两部iPhone既可以是中央设备又可以是外围设备吗?

ios - Swift View Controller 初始化程序问题

iphone编程: collision detection of a UIImageView and an animation