2013-01-24 71 views
0

可能重复:
Check for internet connection - iOS SDK哪一种最简单的方法来检查iOS中的Internet连接?

我在寻找支票最快和最简单的方式在iOS上的连接。

我发现这一点:

-(BOOL)connectedToNetwork { 
    NSURL* url = [[NSURL alloc] initWithString:@"http://google.com/"]; 
    NSData* data = [NSData dataWithContentsOfURL:url]; 
    if (data != nil) 
     return YES; 
    return NO; 
} 

你知道,如果是有什么更简单???

伙计们,感谢所有的答案,但我正在寻找最简单,最轻的解决方案,而不是最好的解决方案(即不需要区分3G/Wi-Fi,我只搜索是/否达到一个网站)

+1

不推荐使用网站检查互联网连接。因为甚至用谷歌检查。在未来的谷歌网站可以下降(谁知道;)),但你会有互联网连接。因此,使用rechability代码来检查互联网连接。其推荐的苹果。检查我的答案。 – iPrabu

回答

4

看看苹果提供的Reachability Example

您的方法可能存在的问题是,您可能有超时,因此同步下载某些数据可能会阻止您的应用程序。因此,苹果可能会拒绝你的应用程序。

Reachability *_reachability = [Reachability reachabilityForInternetConnection]; 
NetworkStatus remoteHostStatus = [_reachability currentReachabilityStatus]; 
if (remoteHostStatus == NotReachable) { 
    // not reachable 
} else if (remoteHostStatus == ReachableViaWiFi) { 
    // reachable via Wifi 
} else if (remoteHostStatus == ReachableViaWWAN) { 
    // reachable via WWAN 
} 
1
Reachability* reachability = [Reachability sharedReachability]; 
[reachability setHostName:@"www.example.com"]; // set your host name here 
NetworkStatus remoteHostStatus = [reachability remoteHostStatus]; 

How to check for an active Internet connection on iOS or OSX?

+0

是的,我们不应该使用“www.apple。com“,我们应该使用我们各自的主机名,如果apple.com不可访问,那并不意味着我们的服务无法访问我们的应用程序,我们应该检查我们的主机名是否可用。 –

0

正如@ who9vy所述使用Reachability Example

导入这两个类Reachability.h和Reachability.m到您的项目

使用方法来检查去上网连接

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

是否可达。最好的办法是苹果Rechability类

入住这link

希望它可以帮助你..

2

使用此代码来检查设备是否连接到互联网或不

请使用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); 
} 
相关问题