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.