gpt4 book ai didi

ios - 如何在 Swift 3 或 Swift 4 中检查互联网连接?

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:56:12 26 4
gpt4 key购买 nike

<分区>

我已尝试在 Swift 3 中检查互联网连接,但代码对我不起作用。

 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(MemoryLayout.size(ofValue: zeroAddress))
zeroAddress.sin_family = sa_family_t(AF_INET)

let defaultRouteReachability = withUnsafePointer(&zeroAddress) {
SCNetworkReachabilityCreateWithAddress(nil, UnsafePointer($0)).takeRetainedValue()
}

var flags: SCNetworkReachabilityFlags = SCNetworkReachabilityFlags(rawValue: UInt32(0))
if SCNetworkReachabilityGetFlags(defaultRouteReachability, &flags) == 0 {
return false
}

let isReachable = (flags & UInt32(kSCNetworkFlagsReachable)) != 0
let needsConnection = (flags & UInt32(kSCNetworkFlagsConnectionRequired)) != 0

return (isReachable && !needsConnection) ? true : false
}

我还导入了 SystemConfiguration 框架。请建议如何检查。

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com