在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