iOS检查是否启用了WiFi辅助功能

时间:2016-12-16 12:59:18

标签: ios wifi

我正在尝试检查是否启用了WiFi辅助功能。当我连接到我的接入点以获取一些数据时,我遇到了问题,当我连接不良时,我使用了蜂窝数据并且它干扰了我的接入点。有没有办法检查是否启用此选项?

1 个答案:

答案 0 :(得分:2)

好的,我想我可以帮一点点。你需要check SCNetworkReachabilityFlags,我认为,这将是一个特定的旗帜组合。我没有找到支持哪些标志组合表明您正在使用WI-FI和Cellular的文档,我也找不到允许您直接检查该设置的文档。

根据以往的经验,Apple可能无法直接检查该设置。

所以...这是一个让我们入手的小代码?

public enum InternetStatus {
   case notReachable
   case reachableViaWWAN
   case reachableViaWiFi
   case wifiAssist
}

您可以在自己选择的扩展中定义变量。 (也许URLSession?)

static public var internetStatus: InternetStatus {

    var zeroAddress = sockaddr_in()
    zeroAddress.sin_len = UInt8(MemoryLayout<sockaddr_in>.size)
    zeroAddress.sin_family = sa_family_t(AF_INET)

    guard let defaultRouteReachability = withUnsafePointer(to: &zeroAddress, {
        $0.withMemoryRebound(to: sockaddr.self, capacity: 1) {
            SCNetworkReachabilityCreateWithAddress(nil, $0)
        }
    }) else {
        return .notReachable
    }

    var flags: SCNetworkReachabilityFlags = []

    if flags.contains(.connectionOnDemand) {
        print("Connection On Demand")
    }

    if flags.contains(.connectionAutomatic) {
        print("Connection Automatic")
    }

    if flags.contains(.connectionOnTraffic) {
        print("Connection On Traffic")
    }

    if flags.contains(.connectionRequired) {
        print("Connection Required")
    }

    if flags.contains(.interventionRequired) {
        print("Intervention Required")
    }

    if flags.contains(.isDirect) {
        print("isDirect")
    }

    if flags.contains(.isLocalAddress) {
        print("Local Address")
    }

    if flags.contains(.isWWAN) {
        print("WWAN")
    }

    if flags.contains(.reachable) {
        print("Reachable")
    }

    if flags.contains(.transientConnection) {
        print("Transient Connection")
    }


    if !SCNetworkReachabilityGetFlags(defaultRouteReachability, &flags) {
        return .notReachable
    }

    if flags.contains(.reachable) == false {
        // The target host is not reachable.
        return .notReachable
    }
    else if flags.contains(.isWWAN) == true {
        // WWAN connections are OK if the calling application is using the CFNetwork APIs.
        return .reachableViaWWAN
    }
    else if flags.contains(.connectionRequired) == false {
        // If the target host is reachable and no connection is required then we'll assume that you're on Wi-Fi...
        return .reachableViaWiFi
    }else if flags.contains(.connectionRequired) && flags.contains(.isWWAN) {
        // Not sure here, maybe Wi-Fi assist is currently being utilized? Will need to test.
        return .wifiAssist
    }else if (flags.contains(.connectionOnDemand) == true || flags.contains(.connectionOnTraffic) == true) && flags.contains(.interventionRequired) == false {
        // The connection is on-demand (or on-traffic) if the calling application is using the CFSocketStream or higher APIs and no [user] intervention is needed
        return .reachableViaWiFi
    }
    else {
        return .notReachable
    }
}

诀窍是在您知道Wi-Fi辅助处于活动状态并观察标志的设置中进行调试。或者比我聪明,只知道它们是什么。如果有人指出,我会更新这个答案,或者我找出正确的旗帜组合。