Apple提供了一个官方demo演示ping(ICMP)发送和接收,我们可以根据这个demo封装一个工具类来实现我们的需求。
官方参考地址:https://developer.apple.com/library/content/samplecode/SimplePing/Introduction/Intro.html
demo地址:https://github.com/6ag/iOSPingTester
使用方法
let addresses = ["blog.6ag.cn", "www.baidu.com", "www.qq.com"] JFPingManager.getFastestAddress(addressList: addresses) { (address) in guard let address = address else { print("所有地址都没有ping通") return } print("address = \(address)") }