RestKit可达性为null

tac*_*cos 0 iphone objective-c reachability ios restkit

我的代码中包含以下代码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];
    }
}
Run Code Online (Sandbox Code Playgroud)

Eva*_*ski 5

根据以下文件RKReachabilityObserver:

初始化时,RKReachabilityObserver实例处于不确定状态,表示尚未建立可达性状态.在观察者处理第一个回调之后,观察者将回答YES为reachabilityDetermined并且networkStatus将返回确定的响应.

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

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

[[NSNotificationCenter defaultCenter] addObserver:self 
                                      selector:@selector(reachabilityStatusChanged:) 
                                      name:RKReachabilityDidChangeNotification object:nil];
Run Code Online (Sandbox Code Playgroud)