IMNetworkReachability是一个可用性框架。它旨在帮助您与网络活动事件进行接口交互。它允许您同步和异步地监控网络状态。
let reachability = IMNetworkReachability("www.google.com")
switch reachability.isReachable() {
case .reachable:
print("Reachable")
case .offline:
print("Offline")
case .error(let error):
print("Failed to check for a network reachability, error: \(error)")
}
// Declare this property where it won't go out of scope relative to your listener
let reachability = IMNetworkReachability("www.google.com")
reachability.startListening { (result) in
// this is called on a main thread
switch result {
case .reachable:
print("Reachable")
case .offline:
print("Offline")
case .error(let error):
print("Failed to check for a network reachability, error: \(error)")
}
}
用于停止通知
reachability.stopListening()
此外,别忘了在macOS上启用网络访问。
IMNetworkReachability支持多种方法在项目中安装库。
要使用CocoaPods将IMNetworkReachability集成到Xcode项目中,请在Podfile中指定它
source 'https://github.com/CocoaPods/Specs.git'
target 'iOS' do
platform :ios, '8.0'
use_frameworks!
pod 'IMNetworkReachability', '~> 0.1'
end
target 'macOS' do
platform :osx, '10.9'
use_frameworks!
pod 'IMNetworkReachability', '~> 0.1'
end
target 'tvOS' do
platform :tvos, '9.0'
use_frameworks!
pod 'IMNetworkReachability', '~> 0.1'
end
然后,运行以下命令
$ pod install
我是Ivan Magda。电子邮件:[email protected]。Twitter:@magda_ivan。
此项目是基于MIT许可证的开源软件。
有关更多信息,请参阅LICENSE文件。