【问题标题】:How Application will get to know Availability of internet connection in iPhone?应用程序如何知道 iPhone 中互联网连接的可用性?
【发布时间】:2013-07-12 22:02:21
【问题描述】:

我是 Iphone 应用程序开发的新手,所以我有几个问题。帮我解决这个问题。

1) iphone 上的所有应用程序如何知道,当用户在设置中打开 wifi 按钮或蜂窝按钮时,有可用的互联网连接?

2) 如何区分 wifi 连接和蜂窝连接?

3) iphone中是否也有类似于android概念的广播接收器机制?

我在谷歌上搜索了许多互联网连接链接,并了解了 iPhone 中的 Reachibilty 类,但我无法清楚地了解它是如何工作的?如果有任何一次可以给我链接,详细解释我的可达性,那就太好了。或任何其他实现上述功能的机制。

我正在编写一个应用程序,我需要在其中开始上传一些数据,当应用程序知道有可用的连接时,即使应用程序处于后台或前台也会发生这种情况。

提前致谢

【问题讨论】:

    标签: ios nsnotificationcenter internet-connection


    【解决方案1】:

    您可以使用 link 中提供的 Rechability 类

    也可以通过这个链接来帮助你确定。

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

    或者这行简单的代码也很有用

    Reachability *reachability = [Reachability reachabilityForInternetConnection];   
    NetworkStatus networkStatus = [reachability currentReachabilityStatus];    
    if (networkStatus == NotReachable) {        
        NSLog(@"There IS NO internet connection");        
    } else {        
    
         NSLog(@"There IS internet connection");        
    
    
        }        
    }
    

    【讨论】:

      【解决方案2】:

      你应该使用Reachability这个类

       NetworkStatus netStatus = [curReach currentReachabilityStatus];
      
      NSString* statusString= @"";
      switch (netStatus)
      {
          case NotReachable:
          {
              statusString = @"Access Not Available";
      
              //Minor interface detail- connectionRequired may return yes, even when the host is unreachable.  We cover that up here...
      
              break;
          }
      
          case ReachableViaWWAN:
          {
              statusString = @"Reachable WWAN";
      
              break;
          }
          case ReachableViaWiFi:
          {
               statusString= @"Reachable WiFi";
      
              break;
        }
      }
      
      [[Reachability reachabilityForInternetConnection] startNotifier];
      [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(someMethod:) name:kReachabilityChangedNotification object:nil];
      

      【讨论】:

        【解决方案3】:

        尝试使用此示例代码 Sample
        https://github.com/tonymillion/Reachability

        它还允许您检查是否启用了 WiFi:

        Reachability* reachability = [Reachability sharedReachability];
        [reachability setHostName:@"www.google.com"];    // set your host name here
        NetworkStatus remoteHostStatus = [reachability remoteHostStatus];
        
        if(remoteHostStatus == NotReachable) { }
        else if (remoteHostStatus == ReachableViaWiFiNetwork) { }
        else if (remoteHostStatus == ReachableViaCarrierDataNetwork) { }
        

        【讨论】:

        • 这不是很好,因为如果www.example.com 关闭,它将无法通过测试
        • @SAMIRRATHOD 如何在不检查连接请求的情况下自动知道?
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-03-14
        • 1970-01-01
        • 1970-01-01
        • 2015-08-19
        相关资源
        最近更新 更多