2012-04-30 26 views
0

AppDelegate.mに次のコードがあります。NSLogの結果は常に(null)であり、到達不可能な条件は決して発生しません。なぜこれが起こっているのか/私が間違っていることを知りたいのですが。RestKit到達可能性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]; 
    } 
} 

答えて

2

ブール値がないオブジェクトであるため、%@がその文字列リテラルではありません。彼らは実際にはunsigned charですが、あなたがそれらをのNSLog:RKReachabilityObserverため番目のドキュメントによると

NSLog([[[RKClient sharedClient] reachabilityObserver] isReachabilityDetermined] ? @"Yes" : @"No"); 
+0

Doh - そうです。しかし、まだ疑問は残っていますね、なぜそれはいつもNOですか? –

5

初期化すると、RKReachabilityObserverインスタンスは、その到達可能性の状態を示すために、不確定な状態にあることはなかったですまだ確立されていない。最初のコールバックがオブザーバによって処理された後、オブザーバはreachabilityDeterminedに対してYESと応答し、networkStatusは確定応答を返します。

接続可能かどうかを確認する前に、到達可能性ステータスが確定するまで待つ必要があります。これが最初のif文がトリガーしていない理由です。この変化を監視するために

、セットアップ(this Stack Overflow questionから)通知オブザーバ:ここ

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

後で一部RestKit 0.20の変化となります。到達可能性ブロックのコードは次のようになります。

RKObjectManager *manager = [RKObjectManager managerWithBaseURL:[RemoteTools serverUrl]]; 
[manager.HTTPClient setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) { 
    if (status == AFNetworkReachabilityStatusNotReachable) { 
     UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"No network connection" 
                 message:@"You must be connected to the internet to use this app." 
                 delegate:nil 
               cancelButtonTitle:@"OK" 
               otherButtonTitles:nil]; 
     [alert show]; 
    } 
}] 
関連する問題