【问题标题】:Reachability cant connect through cellular connection in swift 3可达性无法通过swift 3中的蜂窝连接进行连接
【发布时间】:2017-08-30 02:06:57
【问题描述】:

这是我检查我的应用程序连接的课程,因为它需要使用互联网/移动连接,但如果它通过蜂窝连接 (3G/4G/LTE) 连接,我的应用程序将无法运行。任何人都可以帮我一把吗?谢谢

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

    let defaultRouteReachability = withUnsafePointer(to: &zeroAddress) {
        $0.withMemoryRebound(to: sockaddr.self, capacity: 1) {zeroSockAddress in
            SCNetworkReachabilityCreateWithAddress(nil, zeroSockAddress)
        }
    }

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

    let isReachable = flags == .reachable
    let needsConnection = flags == .connectionRequired
    let wWAN = flags == .isWWAN

    return isReachable || wWAN && !needsConnection

}
}

【问题讨论】:

    标签: ios swift reachability


    【解决方案1】:

    我在我的代码中使用了这个方法来查找设备是否连接到互联网!我在 StackOverFlow 上找到了代码,但不记得链接了。如果有人知道链接,请发表评论,以便我可以更新帖子

    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(to: &zeroAddress) {
            $0.withMemoryRebound(to: sockaddr.self, capacity: 1) {zeroSockAddress in
                SCNetworkReachabilityCreateWithAddress(nil, zeroSockAddress)
            }
        }
    
        var flags: SCNetworkReachabilityFlags = SCNetworkReachabilityFlags(rawValue: 0)
        if SCNetworkReachabilityGetFlags(defaultRouteReachability!, &flags) == false {
            return false
        }
    
        /* Only Working for WIFI
         let isReachable = flags == .reachable
         let needsConnection = flags == .connectionRequired
    
         return isReachable && !needsConnection
         */
    
        // Working for Cellular and WIFI
        let isReachable = (flags.rawValue & UInt32(kSCNetworkFlagsReachable)) != 0
        let needsConnection = (flags.rawValue & UInt32(kSCNetworkFlagsConnectionRequired)) != 0
        let ret = (isReachable && !needsConnection)
    
        return ret
    
    }
    

    【讨论】:

    • 那么它现在可以在蜂窝连接上工作吗?因为我没有使用此代码运行测试的物理设备。谢谢
    • 是的,当您有蜂窝网络连接时,该代码有效。我已经在我的物理 iPhone 6s 上对其进行了测试,它运行良好。 :) 如果我的回答有帮助,您能否将其标记为您问题的答案? @Fritz Gerald Moran
    猜你喜欢
    • 2019-10-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多