问题
Following issue:
I implemeted a Ping Object:
@interface PingTest : NSObject <SimplePingDelegate>
@property (strong, nonatomic) SimplePing* ping;
SimplePing i got from Apple: https://developer.apple.com/library/mac/samplecode/SimplePing/Introduction/Intro.html
Now i am trying to send a Ping like this:
@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");
}
But my delegate methods are not called...Anyone knows why?!
EDIT: For some reason inside 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];
}
}
}
the "HostResolveCallback" never gets called....Thats the problem i think...
回答1:
ARC is deallocating the SimplePing
instance (*ping
in your case) instead of using property use an iVar like this
Also you forget to add sendPingWithData
to didStartWithAddress
delegate method. 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
}
来源:https://stackoverflow.com/questions/22766499/simpleping-apple-delegates-not-firing