对于iOS 9,我使用Reachability public class检查设备是否连接到互联网。我将我的Swift 2代码转换为Swift 3,并且Reachability不再适用。有人能告诉我如何检查iOS 10上的互联网连接吗?谢谢! 下面的代码片段:检查互联网连接(iOS 10)
open class Reachability {
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) {
SCNetworkReachabilityCreateWithAddress(nil, UnsafePointer($0))
}
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)
}
}
请参阅http:// stackoverflow.com/questions/39046377/swift-3-unsafepointer0-no-longer-compile-in-xcode-8-beta-6 – MartinMcB
请注意,http://stackoverflow.com/questions/25623272/how-to-use -s-scnetworkreachability-in-swift/25623647#25623647已更新为Swift 3. –
我认为这将帮助您与您的问: http:// stackoverflow。com/a/39783037/4301118 – sharon