2010-04-19 87 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); 
}