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.framework
Library 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