To determine whether WiFi connections can be judged using reachability, then how should WiFi be judged?
Here are two approaches that are entirely based on different ideas:
Method One:
Use the Systemconfiguration.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-& Gt;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 Two:
Using KVC to Judge StatusBar
-(BOOL) iswificonnected {
uiapplication *app = [uiapplication sharedapplication];
Nsarray *children = [[[App valueforkeypath:@ "StatusBar"] valueforkeypath:@ "Foregroundview"] subviews];
Get to the network return code for
(ID child in children)
{if ([child iskindofclass:nsclassfromstring (@) Uistatusbardatanetworkitemview ")]) {
int nettype = [[Child valueforkeypath:@ ' Datanetworktype '] intvalue];
NSLog (@ "type:%@", @ (NetType));
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
}
The corresponding network status of 1,2,3,5 is 2G, 3G, 4G and WiFi respectively. (need to determine the current network type write a switch to determine the OK)
}
}
NSLog (@ "Not open network or no net work");
return NO;
}
In fact, method two is also to determine the status of the network connection, can not determine whether WiFi open. Under different network connections, StatusBar displays different icons, and when WiFi is switched on without connection, the result of method two will still be no.
The above is a small set to tell you how to determine whether the iphone WiFi open two ways, I hope to help you, if you have any questions please give me a message, small series will promptly reply to everyone. Here also thank you very much for the cloud Habitat Community website support!