Reachability in iOS with iOS 12

Can you explain each line of this code? Cant find proper explanations in documentation Only reachable flag is enough for detecting wifi ? why we need connectionRequired, connectionOnDemand, connectionOnTraffic, interventionRequired private func checkReachable() -> Bool { var flags = SCNetworkReachabilityFlags() SCNetworkReachabilityGetFlags(SCNetworkReachabilityCreateWithName(nil, “https://www.google.com”)!, &flags) let isReachable = flags.contains(.reachable) let needsConnection = flags.contains(.connectionRequired) let canConnectAutomatically = flags.contains(.connectionOnDemand) || flags.contains(.connectionOnTraffic) let canConnectWithoutUserInteraction = canConnectAutomatically && !flags.contains(.interventionRequired) return (isReachable && (!needsConnection || canConnectWithoutUserInteraction)) }

Hi @jesro, thank you for posting your question here. From my understanding, network reachability is how we can understand if a user is connected to wifi or not. So we can check for network connection by using SCNetworkReachabilityFlags which is part of the framework SystemConfiguration. When we want to check and use the network information we are receiving, flags will be used to let us know what is going on. For example, you have the variable called flags. This will be accessing .connectionRequired meaning a connection must be established before the address can be reached. You are also using .reachable which means the address can be reached using the current network configuration. Then jumping to .connectionOnDemand and .connectionOnTraffic, these two will allow for the address to be reached with the current configuration but it needs to have an established connection first. The difference between the two is that .connectionOnDemand will create a connection on demand whereas .connectionOnTraffic will start the connection if there is any traffic directed to that address. Lastly, .interventionRequired will allow the address to be reached using the current network configuration, but a connection must first be established. Your constant isReachable is checking to see if those flags are available for use because it is using the instance method contains. Hope this clears things up for you and gives you a better understanding on what these flags do.

Best,
Gina

hey @gdelarosa, thanks for your response… it was helpful for me .
best regards!!

This topic was automatically closed after 166 days. New replies are no longer allowed.