I'm trying to get the latency from host for a pretty good time and I'm stuck in. Already tried Simple Ping , but seems it doesn't return the latency. The closest I've done was when I use the TKC-PingTest for MAC OS. That works perfect but only in the iPhone Simulator because when use the iPhone I get an error due the patch "/sbin/ping" TKC uses. Besides these two, I already tried many others and got nothing.
相关问题
- CALayer - backgroundColor flipped?
- Core Data lightweight migration crashes after App
- How can I implement password recovery in an iPhone
- State preservation and restoration strategies with
- “Zero out” sensitive String data in Swift
相关文章
- 现在使用swift开发ios应用好还是swift?
- C# 使用ping来判断网络是否连通,但是拔掉网线就PingException
- UITableView dragging distance with UIRefreshContro
- TCC __TCCAccessRequest_block_invoke
- Where does a host app handle NSExtensionContext#co
- xcode 4 garbage collection removed?
- Unable to process app at this time due to a genera
- Swift - hide pickerView after value selected
Following is full working example which pings exactly once given address and then returns ping time in miliseconds:
Objective-C
And example usage is as follows:
Swift
Usage:
Just for convenience I'm using SimplePing which as stated in docs is fully compatible with iOS:
Please note that I'm using singleton, as I repeatedly check latency, however if you need this just once you can adopt it without singleton instance. Also SimplePing uses hosts, which will block your main thread so calling it in separate thread might be useful.
Swift 3 implementation of hris.to's answer:
You can easily extend simple ping to calculate the latency. Simpleping.h defines the SimplePingDelegate protocol. There are two methods of interest -
didSendPacket
anddidReceivePingResponsePacket
. A naive implementation for timing the latency would beI say this is a niave implementation because it doesn't deal with the case where another packet is sent before the response is received or where packets are dropped. To deal with this you would need to examine the packet data to determine whether the sequence number was consistent between the send and receive events.