Woo! Hooy!
We have just recieved your project brief and our expert will contact you shortly.
Send Again
Close
It’s mandatory for all applications to check internet connection is available or not. So you can check the internet availability by using SCNetworkReachability API of SystemConfiguration Framework which is by default provided in iOS, You do not have to use any third party library for that.Follow these steps to check internet availability:-
1 |
import SystemConfiguration |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
import Foundation import SystemConfiguration public class ReachabilityTest { class func isConnectedToNetwork() -> Bool { var zeroAddress = sockaddr_in() zeroAddress.sin_len = UInt8(MemoryLayout.size(ofValue: zeroAddress)) zeroAddress.sin_family = sa_family_t(AF_INET) let defaultRouteReachability = withUnsafePointer(to: &zeroAddress) { $0.withMemoryRebound(to: sockaddr.self, capacity: 1) {zeroSockAddress in SCNetworkReachabilityCreateWithAddress(nil, zeroSockAddress) } } var flags = SCNetworkReachabilityFlags() if !SCNetworkReachabilityGetFlags(defaultRouteReachability!, &flags) { return false } let isReachable = (flags.rawValue & UInt32(kSCNetworkFlagsReachable)) != 0 let needsConnection = (flags.rawValue & UInt32(kSCNetworkFlagsConnectionRequired)) != 0 return (isReachable && !needsConnection) } } |
1 2 3 4 5 6 7 |
if ReachabilityTest.isConnectedToNetwork() { print("Internet connection available") } else{ print("No internet connection available") } |
Be the first to comment.