Two methods to determine whether the iPhone WiFi is enabled are connected to WiFi and iPhone WiFi

Source: Internet
Author: User

Two methods to determine whether the iPhone WiFi is enabled are connected to WiFi and iPhone WiFi

In iOS, the API used to query the network information of the current connection is CNCopyCurrentNetworkInfo.

This API is located in SystemConfiguration. framework. You need to add. h and include library files for use.

You can directly include

# Import <SystemConfiguration/CaptiveNetwork. h>

The Code is as follows:

 

+ (NSString *) getWifiName

{

NSString * wifiName = nil;

CFArrayRef wifiInterfaces = CNCopySupportedInterfaces ();

If (! WifiInterfaces ){

Return nil;

}

NSArray * interfaces = (_ bridge NSArray *) wifiInterfaces;

For (NSString * interfaceName in interfaces ){

CFDictionaryRef dictRef = CNCopyCurrentNetworkInfo (_ bridge CFStringRef) (interfaceName ));

If (dictRef ){

NSDictionary * networkInfo = (_ bridge NSDictionary *) dictRef;

NSLog (@ "network info-> % @", networkInfo );

WifiName = [networkInfo objectForKey :( _ bridge NSString *) kCNNetworkInfoKeySSID];

CFRelease (dictRef );

}

}

CFRelease (wifiInterfaces );

Return wifiName;

}

How can I determine whether the Wi-Fi connection can be determined by using Reachability?
The following two methods are completely based on different ideas:

Method 1:
UseSystemConfiguration.frameworkLibrary for judgment

#import <ifaddrs.h>#import <net/if.h>#import <SystemConfiguration/CaptiveNetwork.h>- (BOOL) isWiFiEnabled {    NSCountedSet * cset = [NSCountedSet new];    struct ifaddrs *interfaces;    if( ! getifaddrs(&interfaces) ) {        for( struct ifaddrs *interface = interfaces; interface; interface = interface->ifa_next)         {            if ( (interface->ifa_flags & IFF_UP) == IFF_UP ) {                [cset addObject:[NSString stringWithUTF8String:interface->ifa_name]];            }        }    }    return [cset countForObject:@"awdl0"] > 1 ? YES : NO;}

Method 2:

Use KVC to determine StatusBar

-(BOOL) isWiFiConnected {UIApplication * app = [UIApplication sharedApplication]; NSArray * children = [[app valueForKeyPath: @ "statusBar"] valueForKeyPath: @ "foregroundView"] subviews]; // obtain the network return code for (id child in children) {if ([child isKindOfClass: NSClassFromString (@ "UIStatusBarDataNetworkItemView")]) {int netType = [[child valueForKeyPath: @ "dataNetworkType"] intValue]; NSLog (@ "type: % @", @ (netTyp E); if (netType = 1) {NSLog (@ "2G"); return NO;} else if (netType = 2) {NSLog (@ "3G"); return NO;} else if (netType = 3) {NSLog (@ "4G"); return NO ;} else if (netType = 5) {NSLog (@ "Wifi"); return YES;} // 1, 2, 3, 5 respectively correspond to the network status of 2G, 3G, 4G and WIFI. (If you need to determine the current network type, write a switch to determine it)} NSLog (@ "not open network or no net work"); return NO ;}

In fact, method 2 also determines the network connection status and does not determine whether WiFi is enabled. StatusBar displays different icons in different network connection States. When WiFi is enabled but NO connection is available, the result of method 2 is still NO.

Link: http://blog.csdn.net/smking/article/details/38895275

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.