【问题标题】:How To check if wifi option enabled or not如何检查wifi选项是否启用
【发布时间】:2016-05-10 14:23:34
【问题描述】:

如何检查 iPhone 上是否启用了 wifi 选项(但 iPhone 可能未连接到其中一个 wifi 网络)。

【问题讨论】:

    标签: iphone objective-c ios


    【解决方案1】:

    为此,您需要在项目中导入可达性类。

    之后:-

    #import "Reachability.h"
    

    在你看来 DidLoad 写:-

    - (void)viewDidLoad {
        Reachability *internetReach = [[Reachability reachabilityForInternetConnection] retain];
        [internetReach startNotifer];
        Reachability *wifiReach = [[Reachability reachabilityForLocalWiFi] retain];
        [wifiReach startNotifer];
    
        NetworkStatus netStatus1 = [internetReach currentReachabilityStatus];
        NetworkStatus netStatus2 = [wifiReach currentReachabilityStatus];
        if(netStatus1 == NotReachable && netStatus2 == NotReachable)
        {
            UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Sorry" message:@"This feature requires an internet connection." delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
            [alertView show];
            [alertView release];
        }
        else
        {//wifi connection available;
    }
    }
    

    【讨论】:

    • 如果internat 可以访问但wi-fi 选项被禁用...这会起作用吗?
    【解决方案2】:

    为此找到了一大行代码。 将 Reachability 类添加到您的项目中,然后您可以这样做:

    BOOL isConnectedProperly = ([[Reachability reachabilityForInternetConnection] currentReachabilityStatus] == ReachableViaWiFi);
    

    【讨论】:

      【解决方案3】:
        First import Reachability files into your project.
      
       -(void)loginButtonTouched
      {  
        bool success = false;
        const char *host_name = [@"www.google.com" 
                   cStringUsingEncoding:NSASCIIStringEncoding];
      
        SCNetworkReachabilityRef reachability = SCNetworkReachabilityCreateWithName
                                                      (NULL, host_name);
        SCNetworkReachabilityFlags flags;
        success = SCNetworkReachabilityGetFlags(reachability, &flags);
        bool isAvailable = success && (flags & kSCNetworkFlagsReachable) && 
                         !(flags & kSCNetworkFlagsConnectionRequired);
      
        if (isAvailable) 
        {
            NSLog(@"Host is reachable: %d", flags);
            // Perform Action if Wifi is reachable and Internet Connectivity is present
        }
        else
        {
            NSLog(@"Host is unreachable");
            // Perform Action if Wifi is reachable and Internet Connectivity is not present
        }       
      }
      

      当loginButtonTouched 方法被调用时,我们检查www.google.com 是否可以访问。 SCNetworkReachabilityFlags 返回标志,帮助我们了解互联网连接的状态。 如果isAvailable 变量返回“true”,则 Host 为 可达意味着 Wifi 是可达的并且存在互联网连接。

      【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-09
      • 2016-06-26
      • 1970-01-01
      • 1970-01-01
      • 2015-10-13
      相关资源
      最近更新 更多