2014-03-31 25 views
2

以下问题:SimplePing苹果的代表没有触发

我Implemeted一个一平一对象:

@interface PingTest : NSObject <SimplePingDelegate> 
@property (strong, nonatomic) SimplePing* ping; 

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

HostAddress是处理互联网地址的所有系统调用和函数的基本结构。如果你提供了一个IP地址,那么这个方法将永远不会被执行。尝试使用众所周知的地址,例如Google Open Dns 8.8.8.8。并检查是否正在调用didStartWithAddress方法.. –

+0

我不提供IP地址... [SimplePing simplePingWithHostName:PING_SERVER]; PING_SERVER是“www.google.com”... didStartWithAddress不会被称为... – davidOhara

+0

如果我从苹果的旧版本,没有ARC,它的工作原理... – davidOhara

回答

1

ARC被解除分配SimplePing实例(*ping在你的情况)而不是使用财产使用像这样的iVar 你也忘了添加sendPingWithDatadidStartWithAddress委托方法。 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 
}