Two ways to determine if the iphone WiFi is open _ios

Source: Internet
Author: User

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!

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.