I am using the Almofire for the network reachability. Here is the scenario:
- Connect the iPad/iPhone with mobile hotspot.
- Now turn on the mobile data, and check the network reachability status. It will return true. That is fine
- Now turn off the mobile data, but still there is hotspot connection between the iPad/iPhone and the hotspot device.
- Now check the network reachability status, it will again return true. Ideally it should return false, as the server is not reachable.
class ReachabilityManager: NSObject {
static let shared = ReachabilityManager()
let reachabilityManager = Alamofire.NetworkReachabilityManager(host: "my.server.url")
var isReachable : Bool {
return reachabilityManager?.isReachable ?? false
}
}
2
Answers
Maybe try replacing reachability with
NWPathMonitor
.It has been more reliable and gives you more options.
Here’s an example:
You can also check if user is using cellular data or is connected to a hotspot:
As per documentation:
Furthermore you can check various connection requirments or types:
This gives you more options like: .wifi, wierdEthernet, loopback, other.
See documentation on:
NWInterface.InterfaceType
let reachability = Reachability()!