测试已测试 | ✗ |
Lang语言 | SwiftSwift |
许可 | BSD |
发布最后发布 | 2017年10月 |
SwiftSwift 版本 | 4.0 |
SPM支持 SPM | ✗ |
Maintained by Cory, Jeff Meador.
由 Vectorform 创建和维护。
Yarp(又一个是可达性库)是一个专注于可靠性和简单性的可达性框架。Yarp 完全支持 IPv6 和 IPv4。Yarp 允许您使用 blocks 或 notifications 来观察可达性变化。
以下示例中的 IPv4 地址可以被其 IPv6 对应地址替换。
在 Yarp 1.0.0 版本之前,初始化时 IP 地址不会执行初始回调。在 1.0.0 版本中,现在它们将返回回调。
//base init defaults to "0.0.0.0" which is Apple's special internet reachability address
let yarp: Yarp? = Yarp()
//Custom hostname init
let yarp: Yarp? = Yarp(hostName: "www.google.com") //or "http://216.58.195.238" either will work
//Custom Address init
let yarp: Yarp? = Yarp(hostAddress: "216.58.195.238")
yarp?.start()
yarp?.addHandler("key1", handler: { (yarp) in
if let reachable = yarp.isReachable {
print("block yarp has reachable-ness: \(reachable)")
}
})
您可以监听 Yarp 发送的 notification。如果您只有一个 Yarp 对象,您可以安全地将对象参数设置为 nil,但如果您使用多个对象来监控多个主机,则将对象参数传递给 addObserver 函数将确保您只接收那个对象的可达性通知。
//listen for ANY Yarp notification
NotificationCenter.default.addObserver(self, selector: #selector(reachabilityChanged, name: Yarp.StatusChangedNotification, object: nil)
// OR
//listen for a specific Yarp notification
NotificationCenter.default.addObserver(self, selector: #selector(reachabilityChanged, name: Yarp.StatusChangedNotification, object: yarpObject)
func reachabilityChanged(notification: Notification) {
if let yarp = notification.object as? Yarp {
//Note: isReachable will likely never be null here
if let reachable = yarp.isReachable {
defaultStatusLabel.text = "Default isReachable: \(reachable)"
}
}
}
如果您不想使用所列的任何依赖管理器,您可以手动将 Yarp 集成到您项目中。
Jeff Meador, [email protected]
Cory Bechtel, [email protected]
Yarp 在 BSD 许可下可用。有关更多信息,请参阅 LICENSE 文件。