I recently got an application rejected for incompatibility with IPv6. The application crashed when the following code was called. I suspect the crash is due to the fact that it uses SCNetworkReachabilityCreateWithAddress when Apple recommends no longer using it.
Can someone give me a hand and help make this code below compatible with IPv6 and IPv4?
the code
import Foundation import SystemConfiguration public class Reachability { class func isConnectedToNetwork() -> Bool { var zeroAddress = sockaddr_in(sin_len: 0, sin_family: 0, sin_port: 0, sin_addr: in_addr(s_addr: 0), sin_zero: (0, 0, 0, 0, 0, 0, 0, 0)) zeroAddress.sin_len = UInt8(sizeofValue(zeroAddress)) zeroAddress.sin_family = sa_family_t(AF_INET) let defaultRouteReachability = withUnsafePointer(&zeroAddress) { SCNetworkReachabilityCreateWithAddress(nil, UnsafePointer($0)).takeRetainedValue() } var flags: SCNetworkReachabilityFlags = 0 if SCNetworkReachabilityGetFlags(defaultRouteReachability, &flags) == 0 { return false } let isReachable = (flags & UInt32(kSCNetworkFlagsReachable)) != 0 let needsConnection = (flags & UInt32(kSCNetworkFlagsConnectionRequired)) != 0 return isReachable && !needsConnection } }
Source code:
Check for internet connectivity in Swift
Call code for viewing loaded:
if Reachability.isConnectedToNetwork() == false { // internet is down let error = NSError(domain: "", code: 3, userInfo: nil) let alertView = createDefaultAlertError(error.code) let tryAgainAction = UIAlertAction(title: ClassGeneralMessages().userMessageTryAgain, style: UIAlertActionStyle.Default) { (UIAlertAction) in } else { // internet is ok // run more code here }
ios swift ipv6
GuiSoySauce
source share