Swift51.com
Swift 头像
Swift  2016-01-24 18:23

NetReachability,检查网络连接的连通性

回复:0  查看:5239  感兴趣:16  赞:0  

NetReachability用于检查网络连接的连通性。

private lazy var reachability: NetReachability = NetReachability(hostname: "www.baidu.com")

//添加通知
NSNotificationCenter.defaultCenter().addObserver(self, selector: "statusChanged", name: FFReachabilityChangedNotification, object: nil)
reachability.startNotifier()

//移除通知
reachability.stopNotifier()
NSNotificationCenter.defaultCenter().removeObserver(self, name: FFReachabilityChangedNotification, object: nil)

//观察者
func statusChanged() {
    print(reachability.currentReachabilityStatus)
}

相关开源代码