2014-10-31 32 views
0

我正在使用Apple提供的Reachability類,並且遇到了一件奇怪的事情。 應用程序在每次應用程序變爲活動狀態時檢查連接,如果它處於活動狀態,則更新一些數據。 當我打開飛行模式,然後重新啓動應用程序,所以didBecomeActive將被調用,可達性返回錯誤狀態(reachableViaWiFi)。如果您再重複一遍,則返回正確的狀態。appDidBecomeActive中的可達性錯誤狀態

另外我注意到,如果你打開飛行模式,等待幾秒鐘,然後重新啓動應用程序,可達性返回正確的狀態。

對此類行爲有任何解釋嗎?

回答

0

當您檢查連接性時,您需要更嚴格。在收到可達性更改通知時添加更多條件。

檢查以下條件:

- (void)reachabilityDidChange:(NSNotification *)notification { 

    // Reachbility for internet 
    Reachability *reachability = (Reachability *)[notification object]; 
    NetworkStatus internetStatus = [reachability currentReachabilityStatus]; 

    switch (internetStatus) { 
     case NotReachable: 
     { 
      NSLog(@"The internet is down."); 
      break; 
     } 
     case ReachableViaWiFi: 
     { 
      NSLog(@"The internet is working via WIFI."); 
      break; 
     } 
     case ReachableViaWWAN: 
     { 
      NSLog(@"The internet is working via WWAN."); 
      break; 
     } 
    } 

    // Reachbility for host 
    Reachability *hostReachability = [Reachability reachabilityWithHostName:@"www.apple.com"]; 

    NetworkStatus hostStatus = [hostReachability currentReachabilityStatus]; 
    switch (hostStatus) { 
     case NotReachable: 
     { 
      NSLog(@"A gateway to the host server is down."); 
      break; 
     } 
     case ReachableViaWiFi: 
     { 
      NSLog(@"A gateway to the host server is working via WIFI."); 
      break; 
     } 
     case ReachableViaWWAN: 
     { 
      NSLog(@"A gateway to the host server is working via WWAN."); 
      break; 
     } 
    } 
} 
+0

什麼是第一和第二switch語句之間的區別?兩者似乎都在檢查'[reachability currentReachabilityStatus]' – sooper 2015-03-30 14:48:13

+0

@sooper:謝謝你:)我確實錯過了那一點,編輯答案。 – Kampai 2015-03-31 05:57:59