In this screencast, learn how you can handle and detect Internet connection issues using reachability.
This is a companion discussion topic for the original entry at https://www.raywenderlich.com/5963-reachability-in-ios
In this screencast, learn how you can handle and detect Internet connection issues using reachability.
Hi,
I saw the screen cast and thought to play around with the sample code. The Reachability API seems to detect the presence network vs. no connection. But if I try a crazy URL (as shown below) it still says that it is reachable.
private let reachability = SCNetworkReachabilityCreateWithName(nil, "ww.ray____wenderlich.om")
I’ve always used URL Session’s dataTask() which also appears to return an error when there’s no Internet; so, I’m not seeing the huge benefit of using the Reachability API.
Perhaps the only benefit would be to get notifications when the internet connection changes.
Can you elaborate on this? Is it correct for, basically, an invalid URL to return that it is reachable?
Thanks.
Ramon.
@smifsud Can you please help with this when you get a chance? Thank you - much appreciated! :]
We’re using that parameter to only check if it’s possible to make the attempt, but not necessarily if it will resolve. Reachability will give you a lot of information, like cellular connection, cellular type, etc .extending beyond simply notifying us if there is a connection or not. Depending on what content you are serving up, you may even want to make changes based on the type of connection the user has.
the class only detects changes when the network goes off
but not when it comes back online, then the notification doesn’t work anymore
it took me a while to figure out what to change
i just wanted people to know that its possible to fix the issue and its simpler then you might guess
@kvebeeck Could you please let us know how you solved this exactly? Thank you - much appreciated!
sure
i added this function in my AppDelegate
i made a global Bool variable named isInternetAvailable
then in AppDelegate i started a timer that calls this function
Blockquote
private func checkReachable()
{
var flags = SCNetworkReachabilityFlags()
SCNetworkReachabilityGetFlags(reachability!, &flags)
if (isNetworkReachable(with: flags))
{
//print (flags)
if flags.contains(.isWWAN) {
isInternetAvailable = true
return
}
isInternetAvailable = true
return
}
else if (!isNetworkReachable(with: flags)) {
isInternetAvailable = false
return
}
}
now you can see if network is available by checking isInternetAvailable anywhere in the project