我的 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/