2010-04-19 111 views
1

我看到有關可達性的任何帖子,但人們並沒有真正給出問題的確切答案。 在我的應用程序使用的可達性代碼的蘋果,在我的appDelegate我用這個:Iphone互聯網連接(可達性)


-(BOOL)checkInternet { 

Reachability *reachability = [Reachability reachabilityWithHostName:@"www.google.com"]; 

NetworkStatus internetStatus = [reachability currentReachabilityStatus]; 
BOOL internet; 

if ((internetStatus != ReachableViaWiFi) && (internetStatus != ReachableViaWWAN)) { 
    internet = NO; 
}else { 
    internet = YES; 
} 
return internet;  
} 

所以問題是,即使我有互聯網連接,這段代碼告訴我,我沒有一個。 有沒有人知道如何做到這一點?

感謝,

+0

剛試試這個代碼,它會完美的工作。 http://stackoverflow.com/questions/8356656/reachability-crashes-app/14452743#14452743 – 2013-01-22 06:27:30

回答

7

你或許應該使用+[Reachability reachabilityForInternetConnection]而不是可達特定名稱(當然,除非這就是你的實際需要)。

畢竟,在您仍然可以正常連接互聯網的情況下,某個特定的服務器可能無法連接,可能有各種各樣的原因。

這是我做的:

BOOL hasInet; 
Reachability *connectionMonitor = [Reachability reachabilityForInternetConnection]; 
[[NSNotificationCenter defaultCenter] 
    addObserver: self 
    selector: @selector(inetAvailabilityChanged:) 
    name: kReachabilityChangedNotification 
    object: connectionMonitor]; 

hasInet = [connectionMonitor currentReachabilityStatus] != NotReachable; 

然後

-(void)inetAvailabilityChanged:(NSNotification *)notice { 
    Reachability *r = (Reachability *)[notice object]; 
    hasInet = [r currentReachabilityStatus] != NotReachable; 
} 

這很好地工作對我來說。

+0

謝謝,但我也找到解決我的問題的另一種方法。通過閱讀Reachability_readme.txt,他們表示,通過嘗試識別hostName(有時會失敗),連接可能需要一段時間,所以我簡單地使用像(74.125.71.104)這樣的google DNS,現在它的工作正常,速度也很快。 – ludo 2010-04-19 08:07:05

+0

嗨ludo和弗蘭克,我用你的代碼和Reachability示例代碼,但它仍然無法正確檢測到Internet的可用性。你如何使用IP地址74.125.71.104?我想不應該是reachabilityWithAddress:(const struct sockaddr_in *)hostAddress;或reachabilityWithHostName:(NSString *)hostName?謝謝。 – lionfly 2010-10-11 16:23:01

+0

嗨ludo你能分享你的代碼給我檢查互聯網連接。 – 2011-10-31 12:00:15

0

使用此代碼來檢查設備是否連接到互聯網或不

使用此代碼在viewDidLoad中:

Reachability* internetReachable; = [Reachability reachabilityForInternetConnection]; 
    [internetReachable startNotifier]; 

    hostReachable = [Reachability reachabilityWithHostName: @"www.apple.com"] ; 
    [hostReachable startNotifier]; 

這個功能添加到您的代碼:

-(void) checkNetworkStatus:(NSNotification *)notice 
{ 
    recheabilityBool=FALSE; 
    nonrecheabilityBool=FALSE; 
    // called after network status changes 
    NetworkStatus internetStatus = [internetReachable currentReachabilityStatus]; 
    switch (internetStatus) 
    { 
     case NotReachable: 
     { 
      nonrecheabilityBool=TRUE; 
      internetCon=0; 
      //NSLog(@"The internet is down."); 


      break; 
     } 
     case ReachableViaWiFi: 
     { 
      NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults]; 
      internetCon=404; 
      [prefs setInteger:internetCon forKey:@"conKey"]; 

      //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: 
     { 
      internetCon=0; 
      if(nonrecheabilityBool==FALSE) 
      { 
       //NSLog(@"A gateway to the host server is down."); 
      } 
      break; 

     } 
     case ReachableViaWiFi: 
     { 
      if(recheabilityBool==FALSE) 
      { 

       recheabilityBool=TRUE; 

       NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults]; 
       internetCon=404; 
       [prefs setInteger:internetCon forKey:@"conKey"]; 


       //NSLog(@"The internet is working via WIFI."); 
       break; 
      } 


      //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; 
     } 
    } 
} 

- (BOOL)connected 
{ 
    Reachability *reachability = [Reachability reachabilityForInternetConnection]; 
    NetworkStatus networkStatus = [reachability currentReachabilityStatus]; 
    return !(networkStatus == NotReachable); 
}