Search code examples
iosswiftreachability

How to detect if an iOS user is connected to cellular network versus wi-fi?


I need to differentiate between the iPhone/iPad user being connected to cellular versus wifi. Under the SCNetworkReachabilityFlag .isWWAN, Apple says:

The absence of this flag does not necessarily mean that a connection will never pass over a cellular network. If you need to robustly prevent cellular networking, read Avoiding Common Networking Mistakes in Networking Overview.

What this means is, if this flag is enabled, the user is most likely connecting via a cellular network, but it's not guaranteed. I read Avoiding Common Networking Mistakes but it is not helpful.

How can I be 100% sure that the user is on a cellular network (or not)?

My code looks like this:

let reachability = SCNetworkReachabilityCreateWithName(nil, "localhost")
var flags = SCNetworkReachabilityFlags()
if let reachability = reachability {
    SCNetworkReachabilityGetFlags(reachability, &flags)
}
let isConnectedToCellular = flags.contains(.isWWAN)

EDIT:

If you are using this code, be sure to call it on a background thread and not the main thread.


Solution

  • You can use Reachability.swift Either you use this, or get some idea on how some certain flow is made and works.

    It has a Connection enum that goes like this:

    public enum Connection: CustomStringConvertible {
            case none, wifi, cellular
            public var description: String {
                switch self {
                case .cellular: return "Cellular"
                case .wifi: return "WiFi"
                case .none: return "No Connection"
                }
            }
        }
    

    And NetworkStatus:

    public enum NetworkStatus: CustomStringConvertible {
        case notReachable, reachableViaWiFi, reachableViaWWAN
        public var description: String {
            switch self {
            case .reachableViaWWAN: return "Cellular"
            case .reachableViaWiFi: return "WiFi"
            case .notReachable: return "No Connection"
            }
        }
    }
    

    And from there you can do the following:

    1. Check if the device is connected to the mobile data / cellular Or Wifi.
    2. Observe reachability / internet connection.