在win 或 mac系統的終端下都有方便的ping命令來檢測網絡的連通性!html
在iOS上可使用蘋果封裝好的工具來開發ping鏈接測試網絡
SimplePing 下載app
1. 主要接口工具
//初始化一個地址 - (instancetype)initWithHostName:(NSString *)hostName NS_DESIGNATED_INITIALIZER; //開始ping - (void)start; //發送ping數據 - (void)sendPingWithData:(nullable NSData *)data; //中止 - (void)stop;
2. 主要回調測試
@optional //ping 開始 - (void)simplePing:(SimplePing *)pinger didStartWithAddress:(NSData *)address; //ping 失敗 - (void)simplePing:(SimplePing *)pinger didFailWithError:(NSError *)error; //成功發送一份ping數據 - (void)simplePing:(SimplePing *)pinger didSendPacket:(NSData *)packet sequenceNumber:(uint16_t)sequenceNumber; //發送ping數據失敗 - (void)simplePing:(SimplePing *)pinger didFailToSendPacket:(NSData *)packet sequenceNumber:(uint16_t)sequenceNumber error:(NSError *)error; //收到ping響應數據 - (void)simplePing:(SimplePing *)pinger didReceivePingResponsePacket:(NSData *)packet sequenceNumber:(uint16_t)sequenceNumber; //收到未知響應數據 - (void)simplePing:(SimplePing *)pinger didReceiveUnexpectedPacket:(NSData *)packet;
3. 測試程序SimplePing appui
4. 示例中還有一個測試用的mactool,編譯生成可執行文件,在終端下執行 spa
./SimplePing -4 www.baidu.comcode
這樣便可測試htm
https://developer.apple.com/library/content/samplecode/SimplePing/Introduction/Intro.htmlblog