ios 开发 ping
在win 或 mac系统的终端下都有方便的ping命令来检测网络的连通性!
在iOS上可以使用苹果封装好的工具来开发ping连接测试
SimplePing 下载
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 app
4. 示例中还有一个测试用的mactool,编译生成可执行文件,在终端下执行
./SimplePing -4 www.baidu.com
这样即可测试
https://developer.apple.com/library/content/samplecode/SimplePing/Introduction/Intro.html