在Swift中检查互联网连接可用性

有没有一种方法可以使用Swift检查互联网连接是否可用?

我知道有很多第三方库可以执行此操作,但是它们都是用Objective-C编写的。我正在寻找Swift的替代方案。


守候你守候我
浏览 597回答 3
3回答

皈依舞

如评论中所述,尽管可以在Swift中使用Objective-C库,但我想要一个更纯净的Swift解决方案。现有的Apple Reachability类和其他第三方库对于我来说转换为Swift似乎太复杂了。我用Google搜索了更多内容,然后发现本文显示了一种检查网络可用性的简单方法。我着手将其翻译为Swift。我遇到了很多麻烦,但是由于来自StackOverflow的Martin R,我设法解决了这些麻烦,并最终在Swift中获得了可行的解决方案。这是代码。import Foundationimport SystemConfigurationpublic class Reachability {&nbsp; &nbsp; class func isConnectedToNetwork() -> Bool {&nbsp; &nbsp; &nbsp; &nbsp; 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))&nbsp; &nbsp; &nbsp; &nbsp; zeroAddress.sin_len = UInt8(sizeofValue(zeroAddress))&nbsp; &nbsp; &nbsp; &nbsp; zeroAddress.sin_family = sa_family_t(AF_INET)&nbsp; &nbsp; &nbsp; &nbsp; let defaultRouteReachability = withUnsafePointer(&zeroAddress) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; SCNetworkReachabilityCreateWithAddress(nil, UnsafePointer($0)).takeRetainedValue()&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; var flags: SCNetworkReachabilityFlags = 0&nbsp; &nbsp; &nbsp; &nbsp; if SCNetworkReachabilityGetFlags(defaultRouteReachability, &flags) == 0 {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return false&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; let isReachable = (flags & UInt32(kSCNetworkFlagsReachable)) != 0&nbsp; &nbsp; &nbsp; &nbsp; let needsConnection = (flags & UInt32(kSCNetworkFlagsConnectionRequired)) != 0&nbsp; &nbsp; &nbsp; &nbsp; return isReachable && !needsConnection&nbsp; &nbsp; }}对于Swift> 3.0public class Reachability {&nbsp; &nbsp; public func isConnectedToNetwork() -> Bool {&nbsp; &nbsp; &nbsp; &nbsp; var zeroAddress = sockaddr_in()&nbsp; &nbsp; &nbsp; &nbsp; zeroAddress.sin_len = UInt8(MemoryLayout<sockaddr_in>.size)&nbsp; &nbsp; &nbsp; &nbsp; zeroAddress.sin_family = sa_family_t(AF_INET)&nbsp; &nbsp; &nbsp; &nbsp; guard let defaultRouteReachability = withUnsafePointer(to: &zeroAddress, {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $0.withMemoryRebound(to: sockaddr.self, capacity: 1) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; SCNetworkReachabilityCreateWithAddress(nil, $0)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }) else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return false&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; var flags: SCNetworkReachabilityFlags = []&nbsp; &nbsp; &nbsp; &nbsp; if !SCNetworkReachabilityGetFlags(defaultRouteReachability, &flags) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return false&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; if flags.isEmpty {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return false&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; let isReachable = flags.contains(.reachable)&nbsp; &nbsp; &nbsp; &nbsp; let needsConnection = flags.contains(.connectionRequired)&nbsp; &nbsp; &nbsp; &nbsp; return (isReachable && !needsConnection)&nbsp; &nbsp; }}这适用于3G和WiFi连接。我还通过一个工作示例将其上传到了我的GitHub。
打开App,查看更多内容
随时随地看视频慕课网APP