gpt4 book ai didi

ios - 在 iOS 上检测启用/禁用 WiFi 的更好方法?

转载 作者:行者123 更新时间:2023-11-29 11:49:13 25 4
gpt4 key购买 nike

在令人难以置信的咬牙切齿之后,我终于找到了一种方法,可以成功检测 iOS 上是否启用了 WiFi,而不管它是否已连接。这种东西至少有几个用途,我认为这不会违反 Apple Law(tm) 的精神或条文。

但是,它很丑陋并且可能永远无法工作。它目前适用于 iOS 10.2.1,截至 2017 年 1 月 31 日。我将在下面给出我的答案,希望有人可以改进它。我深入研究了 Reachability(不满足要求)、CaptiveNetwork、HotspotHelper、SCNetworkConfiguration、Xamarin 的 System.Net.NetworkInterface 等。据我所知,这才是真正有效的。

最佳答案

解决方案的要点是,当 getifaddrs() 报告了两个名称为“awdl0”的接口(interface)时,WiFi 将被启用。只有一个,它被禁用了。

我感谢 pebble8888 将我指向 https://github.com/alirp88/SMTWiFiStatus这是 Objective-C,缺少注释让人很难理解正在发生的事情或作者的意图。

这是我完整且完整的 Xamarin/C# 解决方案,对于任何其他主要语言用户来说应该都非常易读:

using System;
using System.Runtime.InteropServices;

namespace Beacon.iOS
{
/// <summary>
/// code stolen from the Xamarin source code to work around goofy interactions between
/// the good-god-why-would-it-work-that-way iOS and the entirely reasonable Xamarin
/// (it doesn't report interfaces that are reported multiple times)
/// </summary>
class XamHack
{
//
// Types
//
internal struct ifaddrs
{
#pragma warning disable 0649
public IntPtr ifa_next;
public string ifa_name;
public uint ifa_flags;
public IntPtr ifa_addr;
public IntPtr ifa_netmask;
public IntPtr ifa_dstaddr;
public IntPtr ifa_data;
#pragma warning restore
}

//
// OS methods
//
[DllImport("libc")]
protected static extern int getifaddrs(out IntPtr ifap);

[DllImport("libc")]
protected static extern void freeifaddrs(IntPtr ifap);


//
// Methods
//

/// <summary>
/// Our glorious hack. I apologize to the programming gods for my sins
/// but this works (for now) and functionality trumps elegance. Even this.
/// Reverse engineered from: https://github.com/alirp88/SMTWiFiStatus
/// </summary>
public static bool IsWifiEnabled()
{
int count = 0;
IntPtr ifap;

// get the OS to put info about all the NICs into a linked list of buffers
if (getifaddrs(out ifap) != 0)
throw new SystemException("getifaddrs() failed");

try
{
// iterate throug those buffers
IntPtr next = ifap;
while (next != IntPtr.Zero)
{
// marshall the data into our struct
ifaddrs addr = (ifaddrs)Marshal.PtrToStructure(next, typeof(ifaddrs));

// count the instances of the sacred interface name
if ("awdl0" == addr.ifa_name)
count++;

// move on to the next interface
next = addr.ifa_next;
}
}
finally
{
// leaking memory is for jerks
freeifaddrs(ifap);
}

// if there's two of the sacred interface, that means WiFi is enabled. Seriously.
return (2 == count);
}

} // class
} // namespace

关于ios - 在 iOS 上检测启用/禁用 WiFi 的更好方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41969989/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com