Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS Check if WiFi assist is enabled

Tags:

ios

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?

like image 439
Flipper Avatar asked Dec 16 '16 12:12

Flipper


People also ask

Why can't I turn off Wi-Fi assist iPhone?

If the feature is still greyed out, I'd suggest resetting your network settings. To do so, navigate to Settings > General > Reset > Reset Network Settings and tap Reset Network Settings.

Should I turn on Wi-Fi assist on iPhone?

Wi-Fi Assist will then allow your mobile data to kick in so you don't lose your connection. This means if your wifi signal is not very strong then the phone will start using the mobile data signal instead, without a change in the symbols letting you know this. We would recommend turning Off Wi-Fi Assist.


1 Answers

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.

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
}

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
    }
}

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.

like image 195
Jon Vogel Avatar answered Oct 16 '22 23:10

Jon Vogel