SimplePing Apple代表没有解雇 [英] SimplePing Apple Delegates not firing

查看:100
本文介绍了SimplePing Apple代表没有解雇的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下问题:

我实施了一个Ping对象:

I implemeted a Ping Object:

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

我从Apple获得SimplePing: https://developer.apple.com/library/mac/samplecode/SimplePing/Introduction/Intro.html

SimplePing i got from Apple: https://developer.apple.com/library/mac/samplecode/SimplePing/Introduction/Intro.html

现在我正在尝试发送这样的Ping:

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?!

编辑:出于某些原因,在SimplePing.m中:

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];
    }
}
}

HostResolveCallback永远不会得到叫....这就是我认为的问题...

the "HostResolveCallback" never gets called....Thats the problem i think...

推荐答案

ARC正在取消分配 SimplePing instance( * ping 在你的情况下)而不是使用属性使用像这样的iVar
你还忘了添加 sendPingWithData didStartWithAddress 委托方法。 Lookat

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
}

这篇关于SimplePing Apple代表没有解雇的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆