DDPingTools 3.0.0

DDPingTools 3.0.0

DamonHu 维护。



  • 作者
  • DamonHu

DDPingTools

这是一个使用 Swift 语言开发的 iOS 平台 Ping 工具,支持使用 Cocoapods 集成。可以按照以下三个步骤使用

日志 iPhone X 状态栏 statusBar 状态栏 statusBar
the ScreenShot 日志截图预览 the ScreenShot 状态栏 the ScreenShot 状态栏

介绍

尽管 AFNetworkingalamofire 提供了检测移动网络的功能,但它们只知道用户的网络连接模式,而不知道用户的真实用户体验。可能用户已经连接了 WiFi,但网络速度并不像 3G 网络那么快。

因此,它封装了这个功能。您可以通过这个项目通过请求域名进行 ping 操作来获取返回时间并判断用户网络是否在正常范围内。

该包基于 Apple 的 SimplePing 并进一步优化,使其更易于使用,并且可以在三个步骤中调用

CocoaPods

pod 'DDPingTools'

1. 使用主机名创建 ping 对象

let pingTools = DDPingTools(hostName: "www.apple.com")

2. 开始 Ping

pingTools.start(pingType: .any, interval: .second(10)) { (response, error) in
      print(response?.pingAddressIP ?? "")
 }

interval 大于 0 时,Ping 请求将以固定的时间间隔重复发送。当 interval 等于 0 时,只发送一个 ping 请求

响应的 response 包含以下内容

  • pingAddressIP 是对应域名的 IP 地址
  • responseTime ping 响应时间
  • responseBytes 响应字节

3. 停止请求

pingTools.stop()

4、可选配置

public var timeout: HDPingTimeInterval = .millisecond(1000)  //user defined timeout. The default value is 1000 ms. if it is set to 0, it will wait all the time
public var debugLog = true                                  //enable log output
public var stopWhenError = false                            //stop Ping when an error is encountered
public private(set) var isPing = false				//you can judge whether there is a ping task in progress
public var showNetworkActivityIndicator: NetworkActivityIndicatorStatus = .auto              //Whether to display in the status bar

注意:如果网站或连接的路由器设置为禁止 Ping,则在 ping 时会报告错误。您需要先删除限制才能正常使用它