2014-05-13 55 views
5

我使用AFNetworking並想知道如何檢測用戶連接到無線網絡沒有活動的互聯網連接的情況。AFNetworking檢測無線連接時沒有連接到互聯網的活躍

我重現了這種情況購買動力路由器沒有連接DSL線。

AFNetworking return AFNetworkReachabilityStatusReachableViaWiFi = 2 

我的代碼:

[[AFNetworkReachabilityManager sharedManager] setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) { 
     NSLog(@"Reachability: %@", AFStringFromNetworkReachabilityStatus(status)); 
     self.isInternetAvialable = status > 0; 
    }]; 

感謝

我refacotred代碼要像

AFNetworkReachabilityManager* manager = [AFNetworkReachabilityManager managerForDomain:@"http://www.google.com"]; 
     [manager setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) { 
      NSLog(@"Reachability: %@", AFStringFromNetworkReachabilityStatus(status)); 
      self.isInternetAvialable = status > 0; 
     }]; 
     [manager startMonitoring]; 

現在塊永遠不會叫!

+0

沒有公認的答案爲你工作你可以設置變化塊? – SandeepAggarwal

回答

2

而不是使用[AFNetworkReachabilityManager sharedManager]剛剛監測的網絡連接,則可以使用[AFNetworkReachabilityManager managerForDomain:(NSString *)domain]並傳遞適當的主機名 - 例如「www.google.com」 - 這樣您就可以測試n您連接到的網絡可以訪問互聯網 - 它仍然可能被本地DNS和網絡服務器欺騙,但我認爲不太可能有人這樣做。

一旦你有你AFNetworkReachabilityManager爲您共享經理

+0

嘿,請檢查我原來的帖子編輯 – ibm123

+0

你等了多久?由於DNS超時,可能需要很長時間才能失敗。您也可以嘗試一個衆所周知的主機,例如8.8.8.8以避免DNS查找 – Paulw11

1

使用Reachability類及其reachabilityForInternetConnection方法。

Reachability *networkReachability = [Reachability reachabilityForInternetConnection]; 
NetworkStatus networkStatus = [networkReachability currentReachabilityStatus];  

if (networkStatus == NotReachable) 
{   
    NSLog(@"There is NO Internet connection");   
} 
else 
{   
    NSLog(@"There is Internet connection");   
} 
0

通過蘋果公司的Reachability類。它具有您需要的所有功能。這裏最好的選擇是使用一個域名(如谷歌)來檢查實際的互聯網連接。

總結示例代碼:

添加觀察者在didFinishLaunchingWithOptions:

[[NSNotificationCenter defaultCenter] addObserver:self 
             selector:@selector(checkNetworkStatus:) 
              name:kReachabilityChangedNotification 
              object:nil]; 

internetReachable = [[Reachability reachabilityForInternetConnection] retain]; 

[internetReachable startNotifier]; 

// check if a pathway to a random host exists 

hostReachable = [Reachability reachabilityWithHostname:@"SOME_HOST"]; 

[hostReachable startNotifier]; 

然後,在目標方法:

- (void) checkNetworkStatus:(NSNotification *)notice { 

NetworkStatus internetStatus = [internetReachable 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; 
    } 
} 

NetworkStatus hostStatus = [hostReachable 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

不幸的是,蘋果公司的可達性實際上並沒有告訴你它是否可以連接到主機,只要它能夠連接到一個主機。根據他們的說明:'注意:可達性無法告訴您的應用程序是否可以連接到特定的主機,只有可用的接口可用於連接,以及該接口是否爲WWAN。「您可以通過斷開互聯網從您的WiFi路由器。只要設備連接到WiFi,可達性仍然會說Google可以訪問。 – jeffjv