我最近因爲與IPv6不兼容而被拒絕。當下面的代碼被調用時,該應用程序導致崩潰。我懷疑這次崩潰是因爲當Apple建議不再使用它時,它會使用SCNetworkReachabilityCreateWithAddress
。Swift中的IPv6網絡上的網絡連接檢查崩潰
任何人都可以給我一個幫助,使這個代碼低於兼容IPv6和IPv4?
代碼
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
}
}
代碼源:上視圖
Check for internet connection availability in Swift
代碼呼叫沒有負載:
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
}
請出示你的代碼,當你調用該函數。 –
@ Christian Abella,代碼補充說。 – GuiSoySauce
我面臨同樣的拒絕問題。你能解決這個問題,以及如何?請提出建議。 –