以下问题:
我实现了一个Ping对象:
@interface PingTest : NSObject <SimplePingDelegate>
@property (strong, nonatomic) SimplePing* ping;
我从Apple获得SimplePing:https://developer.apple.com/library/mac/samplecode/SimplePing/Introduction/Intro.html
现在我正试图像这样发送一个Ping:
@synthesize ping;
ping = [SimplePing simplePingWithHostName:PING_SERVER];
ping.delegate = self;
[ping start];
#pragma mark - SimplePingDelegates
- (void)simplePing:(SimplePing *)pinger didStartWithAddress:(NSData *)address {
}
- (void)simplePing:(SimplePing *)pinger didSendPacket:(NSData *)packet
{
NSLog(@"didSendPacket");
}
- (void)simplePing:(SimplePing *)pinger didFailToSendPacket:(NSData *)packet error:(NSError *)error
{
NSLog(@"didFailToSendPacket");
}
- (void)simplePing:(SimplePing *)pinger didReceivePingResponsePacket:(NSData *)packet
{
NSLog(@"didReceivePingResponsePacket");
}
但我的委托方法没有被调用......任何人都知道为什么?!
编辑:出于某种原因,在SimplePing.m中:
- (void)start
// See comment in header.
{
// If the user supplied us with an address, just start pinging that. Otherwise
// start a host resolution.
if (self->_hostAddress != nil) {
[self startWithHostAddress];
} else {
Boolean success;
CFHostClientContext context = {0, (__bridge void *)(self), NULL, NULL, NULL};
CFStreamError streamError;
assert(self->_host == NULL);
self->_host = CFHostCreateWithName(NULL, (__bridge CFStringRef) self.hostName);
assert(self->_host != NULL);
CFHostSetClient(self->_host, HostResolveCallback, &context);
CFHostScheduleWithRunLoop(self->_host, CFRunLoopGetCurrent(), kCFRunLoopDefaultMode);
NSLog(@">CFHostStartInfoResolution");
success = CFHostStartInfoResolution(self->_host, kCFHostAddresses, &streamError);
NSLog(@"<CFHostStartInfoResolution");
if ( ! success ) {
[self didFailWithHostStreamError:streamError];
}
}
}
“HostResolveCallback”永远不会被调用....这就是我认为的问题......
答案 0 :(得分:1)
ARC正在取消分配SimplePing
实例(在您的情况下为*ping
),而不是使用属性使用像这样的iVar
此外,您忘记将sendPingWithData
添加到didStartWithAddress
委托方法。 Lookat
@implementation PingTest
{
/*CHANGED*/
SimplePing *_pinger;
}
- (void) startPinger
{
/*CHANGED*/
ping = [SimplePing simplePingWithHostName:PING_SERVER];
ping.delegate = self;
[ping start];
}
#pragma mark - SimplePingDelegates
- (void)simplePing:(SimplePing *)pinger didStartWithAddress:(NSData *)address
{
/*CHANGED*/
[pinger sendPingWithData:nil];
}
- (void)simplePing:(SimplePing *)pinger didSendPacket:(NSData *)packet
{
NSLog(@"didSendPacket");
/*CHANGED*/
//Capture time here if you want to measure the latency
}
- (void)simplePing:(SimplePing *)pinger didFailToSendPacket:(NSData *)packet error:(NSError *)error
{
NSLog(@"didFailToSendPacket");
}
- (void)simplePing:(SimplePing *)pinger didReceivePingResponsePacket:(NSData *)packet
{
NSLog(@"didReceivePingResponsePacket");
/*CHANGED*/
//Capturing time here again and comparing it with the one from didSentPacket will
// give you the latency
}