检测wifi是否已打开 [英] Detect if wifi is turned on

查看:109
本文介绍了检测wifi是否已打开的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法检测iPhone / iPad上是否启用了wifi?

Is there a way to detect if wifi is enabled on an iPhone/iPad?

我不想知道我是否可以上网,因为我使用Reachability类。我只需要知道设备上是否启用了wifi。

I am not interested to see if I can reach the Internet, for that I use the Reachability class. I just need to know if wifi has been enabled on the device.

感谢任何提示。

推荐答案

也许这就是你要找的东西:

Maybe this is what you are looking for:


DEAD LINK: http://www.enigmaticape.com/blog/determine-wifi-启用ios-one-weird-trick

Wayback Machine Archive: https://web.archive.org/web/ 20161114213529 / http://www.enigmaticape.com/blog/determine-wifi-enabled-ios-one-weird-trick

Wayback Machine Archive: https://web.archive.org/web/20161114213529/http://www.enigmaticape.com/blog/determine-wifi-enabled-ios-one-weird-trick

你想做什么没有框架,但有一个可行的技巧。如果你列出了可用的接口,那么当wifi打开时会出现一些接口(有些接口只是在连接时出现。你可以列出这样的接口:

There isn't a framework to what you want to do, but there is a trick that might work. If you list the available interfaces, there will be some interfaces that just appear when the wifi is turned on (and some just appear when you are connected to one. You can list the interfaces like this:

struct ifaddrs *interfaces;

if(!getifaddrs(&interfaces)) {
    for( struct ifaddrs *interface = interfaces; interface; interface=interface->ifa_next) {
        BOOL up = (interface->ifa_flags & IFF_UP) == IFF_UP;
        if ( up ) {
            NSLog(
                @"Name : %s, sa_family : %d",
                interface->ifa_name,
                interface->ifa_addr->sa_family
            );
        }
    }
}

输出Wifi关闭:

Name : lo0, sa_family : 18
Name : lo0, sa_family : 30
Name : lo0, sa_family : 2
Name : lo0, sa_family : 30
Name : pdp_ip0, sa_family : 18
Name : pdp_ip0, sa_family : 2
Name : en0, sa_family : 18
Name : awdl0, sa_family : 18

带wifi的输出:

Name : lo0, sa_family : 18
Name : lo0, sa_family : 30
Name : lo0, sa_family : 2
Name : lo0, sa_family : 30
Name : pdp_ip0, sa_family : 18
Name : pdp_ip0, sa_family : 2
Name : en0, sa_family : 18
Name : awdl0, sa_family : 18
Name : awdl0, sa_family : 30

带wifi和连接的输出:

Output with wifi on and connected:

Name : lo0, sa_family : 18
Name : lo0, sa_family : 30
Name : lo0, sa_family : 2
Name : lo0, sa_family : 30
Name : pdp_ip0, sa_family : 18
Name : pdp_ip0, sa_family : 2
Name : en0, sa_family : 18
Name : en0, sa_family : 30
Name : en0, sa_family : 2
Name : awdl0, sa_family : 18
Name : awdl0, sa_family : 30

如果你探索ifaddrs结构,你会发现al所以连接网络的BSSID / SSID。

If you explore the ifaddrs structure you will find also the BSSID/SSID of the connected network.

这篇关于检测wifi是否已打开的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆