网络状态监听

2019-01-28  本文已影响3人  跬步千里_LenSky
//导入头文件
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
    }
}
上一篇下一篇

猜你喜欢

热点阅读