从Apple example开始,我使用SimplePing检查主机是否可以访问,但是当我使用无法访问的ip进行测试时,其委托方法
- (void)simplePing:(SimplePing *)pinger didFailWithError:(NSError *)error
或
- (void)simplePing:(SimplePing *)pinger didReceivePingResponsePacket:(NSData *)packet
没有被调用,这个委托方法
- (void)simplePing:(SimplePing *)pinger didSendPacket:(NSData *)packet
@property (nonatomic, strong, readwrite) SimplePing *pinger;
@property (weak, nonatomic) IBOutlet UITextField *pingTextField;
@property (weak, nonatomic) IBOutlet UIButton *pingBtn;
- (IBAction)runWithHostName:(id)sender
{
NSString *hostName = self.pingTextField.text;
self.pinger = [SimplePing simplePingWithHostName:hostName];
self.pinger.delegate = (id)self;
[self.pinger start];
}
- (void)simplePing:(SimplePing *)pinger didStartWithAddress:(NSData *)address {
[self.pinger sendPingWithData:nil];
}
- (void)simplePing:(SimplePing *)pinger didSendPacket:(NSData *)packet {
NSLog(@"didSendPacket:");
}
- (void)simplePing:(SimplePing *)pinger didFailWithError:(NSError *)error {
NSLog(@"didFailWithError: %@", [error description]);
}
- (void)simplePing:(SimplePing *)pinger didFailToSendPacket:(NSData *)packet error:(NSError *)error {
// Eg they're not connected to any network
NSLog(@"didFailToSendPacket: %@", [error description]);
}
- (void)simplePing:(SimplePing *)pinger didReceivePingResponsePacket:(NSData *)packet {
NSLog(@"didReceivePingResponsePacket:");
}
- (void)simplePing:(SimplePing *)pinger didReceiveUnexpectedPacket:(NSData *)packet {
NSLog(@"didReceiveUnexpectedPacket:");
}
当我ping到无法访问的主机时,日志为:
2014-12-23 11:45:26.933 PingHelper[1733:60695] >CFHostStartInfoResolution
2014-12-23 11:45:27.004 PingHelper[1733:60695] <CFHostStartInfoResolution
2014-12-23 11:45:27.121 PingHelper[1733:60695] >HostResolveCallback
2014-12-23 11:45:27.131 PingHelper[1733:60695] didSendPacket:
当我ping到可访问的主机时,日志是:
2014-12-23 11:52:37.841 PingHelper[1733:60695] >CFHostStartInfoResolution
2014-12-23 11:52:37.876 PingHelper[1733:60695] <CFHostStartInfoResolution
2014-12-23 11:52:37.882 PingHelper[1733:60695] >HostResolveCallback
2014-12-23 11:52:37.903 PingHelper[1733:60695] didSendPacket:
2014-12-23 11:52:37.903 PingHelper[1733:60695] didReceivePingResponsePacket: