本文介绍了iOS 检查是否启用了 WiFi 辅助的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to check if WiFi assist is enabled. I am having a problem when I am connected to my access point to get some data, and when I have poor connection my cellular data is used and it interfers with my access point. Is there any way to check if this option is enabled?

推荐答案

好吧,我想我能帮上一点忙.你需要检查SCNetworkReachabilityFlags,对于我的想法,将是标志的特定组合.我没有找到支持哪些标志组合表明您正在使用 WI-FI 和 Cellular 的文档,我也没有找到允许您直接检查该设置的文档.

Ok, I think I can help a little. You need to check SCNetworkReachabilityFlags, for what I think, would be a specific combination of flags. I failed to find documentation that supports what combination of flags would indicate you are using WI-FI and Cellular, I also failed to find documentation that allows you to check that setting directly.

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

Based on previous experience Apple probably does not have a way for you to check that setting directly.

所以...这里有一些让我们开始的小代码?

So... Here is a little code to get us started?

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

您可以在您选择的扩展中定义一个变量.(也许 URLSession?)

And a variable you can define in an extension of your choice. (Maybe 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 辅助处于活动状态的设置中进行调试并观察标志.或者比我更聪明,只知道它们是什么.如果有人指出,或者我找出正确的标志组合,我会更新这个答案.

The trick will be to debug in a setting where you know Wi-Fi assist is active and observe the flags. Or be smarter than me and just know what they are. I will update this answer if someone points out, or I figure out the correct combination of flags.

这篇关于iOS 检查是否启用了 WiFi 辅助的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-04 04:12