Well, I think I can help a little. You need to check out SCNetworkReachabilityFlags , as I think it will be a specific combination of flags. I was unable to find documentation that supports which combination of flags indicates that you are using WI-FI and Cellular, I also could not find documentation that allows you to directly check this setting.
Based on previous experience, Apple probably is not able to test this setting directly.
So ... Here's a little code to get you started?
public enum InternetStatus { case notReachable case reachableViaWWAN case reachableViaWiFi case wifiAssist }
And a variable that you can define in the 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 debugging in a setting in which you know that Wi-Fi support is active and respects flags. Or be smarter than me and just know who they are. I will clarify this answer if someone ticks, or I figure out the correct combination of flags.
source share