2013-09-26 54 views
3

我遇到一個小問題。我正在使用Reachability類來測試互聯網連接,並且它工作正常。但是,假設用戶使用Wifi,但他們無法訪問互聯網,則應該在約10秒鐘內重試,然後應該向用戶顯示警報,以便在那裏切換蜂窩網絡。以下是我的代碼。當Wifi處於開啓狀態時,我無法指出網絡連接緩慢的位置。如何檢查iOS中的網絡可達性

self.internetReachable = [Reachability reachabilityForInternetConnection]; 
[self.internetReachable startNotifier]; 

//_hasConnectivity = ([self.internetReachable currentReachabilityStatus] != NotReachable); 
if([self.internetReachable currentReachabilityStatus] == NotReachable) { 
    _hasConnectivity = NO; 
} 
else if([self.internetReachable currentReachabilityStatus] == ReachableViaWiFi){ 
    _hasConnectivity = YES; 
} 
else if([self.internetReachable currentReachabilityStatus] == ReachableViaWWAN) { 
    _hasConnectivity = YES; 
} 

回答

7

嘗試用下面的代碼它的返回BOOL價值,其回報YES如果連接可用其他明智它返回NO

- (BOOL)isNetworkAvailable 
{ 
    CFNetDiagnosticRef dReference;   
    dReference = CFNetDiagnosticCreateWithURL (NULL, (__bridge CFURLRef)[NSURL URLWithString:@"www.apple.com"]); 

    CFNetDiagnosticStatus status; 
    status = CFNetDiagnosticCopyNetworkStatusPassively (dReference, NULL);   

    CFRelease (dReference); 

    if (status == kCFNetDiagnosticConnectionUp) 
    { 
     NSLog (@"Connection is Available"); 
     return YES; 
    } 
    else 
    { 
    NSLog (@"Connection is down"); 
    return NO; 
    } 
} 
+1

感謝您的快速回復,所以在用戶顯示無線連接時這種情況下可以正常工作,但由於速度太慢無法訪問互聯網。 – Kashif

+0

它不適用於iPv6網絡。 – Suresh

+0

不適用於我的情況下,互聯網與3G連接,它說連接已關閉。 – magid

相關問題