美文网首页
网络状态监听

网络状态监听

作者: 跬步千里_LenSky | 来源:发表于2019-01-28 16:32 被阅读3次
    //导入头文件
    import Alamofire
    
    class ServerCheck {
        private static let sharedInstance = ServerCheck()
        var manager = NetworkReachabilityManager(host:"www.baidu.com")
        init() {
            manager?.listener = { status in
                print("\(status)")
            }
            //开始监听
            manager?.startListening()
        }
        class var sharedManager:ServerCheck {
            let instance = self.sharedInstance
            return instance
        }
        func networkReachabilityStatus() -> Alamofire.NetworkReachabilityManager.NetworkReachabilityStatus {
            let status:Alamofire.NetworkReachabilityManager.NetworkReachabilityStatus = (manager?.networkReachabilityStatus)!
            print("当前网络状态:\(status)")
            return status
        }
    }
    
    class SerVer {
        enum CustomNetStatus {
            case NONET //无网络
            case WIFI  //WiFi
            case WWAN  // 3G 或 4G
        }
        class open func netWorkState()->CustomNetStatus{
            var status:CustomNetStatus = .NONET
            let serVerS:NetworkReachabilityManager.NetworkReachabilityStatus = ServerCheck.sharedManager.networkReachabilityStatus()
            switch serVerS {
            case .notReachable:
                status = .NONET
                break
            case .reachable(NetworkReachabilityManager.ConnectionType.ethernetOrWiFi):
                status = .WIFI
                break
            case .reachable(NetworkReachabilityManager.ConnectionType.wwan):
                status = .WWAN
                break
                
            default:
                status = .NONET
                break
            }
            return status
        }
    }
    

    相关文章

      网友评论

          本文标题:网络状态监听

          本文链接:https://www.haomeiwen.com/subject/ggfejqtx.html