【问题标题】:Which is the simplest way to check for Internet connection in iOS? [duplicate]在 iOS 中检查 Internet 连接的最简单方法是什么? [复制]
【发布时间】:2013-01-08 01:38:54
【问题描述】:

可能重复:
Check for internet connection - iOS SDK

我正在寻找在 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,我只是在寻找网站的“是/否”范围)

【问题讨论】:

  • 不建议检查与网站的 Internet 连接。因为即使与谷歌检查。在未来,单独的谷歌网站可能会关闭(谁知道;))但你将有互联网连接。因此,使用 rechability 代码来检查互联网连接。它是苹果推荐的。检查我的答案。

标签: iphone ios objective-c


【解决方案1】:

看看苹果提供的Reachability Example

您的方法可能遇到的问题是您可能会超时,因此某些数据的同步下载可能会阻止您的应用。因此,Apple 可能会拒绝您的应用。

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
}

【讨论】:

    【解决方案2】:

    我建议不要采用这种方法。由于这段代码,我的一个应用程序遭到拒绝。而是选择Apple's Reachability Classes.

    【讨论】:

      【解决方案3】:

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

      在 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);
      }
      

      【讨论】:

        【解决方案4】:
        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?

        【讨论】:

        • 是的,我们不应该使用 "www.apple.com" ,我们应该使用各自的主机名。如果 apple.com 无法访问,这并不意味着我们的服务无法访问您的应用程序。我们应该仅在可用的情况下检查我们的主机名。
        【解决方案5】:

        正如@who9vy 所说,使用Reachability Example

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

        使用方法检查互联网连接

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

        【讨论】:

          【解决方案6】:

          检查可达性的最佳方法是 Apple Rechability 类

          查看link

          希望对你有帮助..

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2011-01-03
            • 1970-01-01
            • 2013-08-19
            • 1970-01-01
            相关资源
            最近更新 更多