3

I'm trying to use Apple's class: Simple Ping, but I can't get this working.

When I'm running example mac os x project it's working:

2015-06-17 00:03:22.569 SimplePing[20386:3133535] pinging 192.168.1.102

2015-06-17 00:03:22.569 SimplePing[20386:3133535] #0 sent

2015-06-17 00:03:22.570 SimplePing[20386:3133535] #0 received

2015-06-17 00:03:23.570 SimplePing[20386:3133535] #1 sent

2015-06-17 00:03:23.571 SimplePing[20386:3133535] #1 received

etc..

But when I do it from my ios (swift) app:

let pinger = SimplePing(hostName: "192.168.1.102")
pinger.delegate = self;
pinger.start()

do {
    NSRunLoop.currentRunLoop().runMode(NSDefaultRunLoopMode, beforeDate: NSDate.distantFuture() as! NSDate)
} while(pinger != nil)

Not sure is do..while loop is needed - probably not needed. Anyway I tried without it too.

And I've added SimplePingDelegate to my class:

    func simplePing(pinger: SimplePing!, didFailToSendPacket packet: NSData!, error: NSError!) {
        println("didFailToSendPacket")
    }

    func simplePing(pinger: SimplePing!, didFailWithError error: NSError!) {
        println("didFailWithError")
    }

    func simplePing(pinger: SimplePing!, didReceivePingResponsePacket packet: NSData!) {
        println("didReceivePingResponsePacket")
    }

    func simplePing(pinger: SimplePing!, didReceiveUnexpectedPacket packet: NSData!) {
        println("didReceiveUnexpectedPacket")
    }

    func simplePing(pinger: SimplePing!, didSendPacket packet: NSData!) {
        println("didSendPacket")
    }

    func simplePing(pinger: SimplePing!, didStartWithAddress address: NSData!) {
        println("didStartWithAddress")
    }

So it gives me output:

2015-06-17 00:32:12.368 Available[938:150352] CFHostStartInfoResolution

2015-06-17 00:32:12.374 Available[938:150352] >HostResolveCallback

didStartWithAddress

other functions aren't called. Why?

Btw. I've also tried moving pinger into class variable like this:

var pinger: SimplePing?

No difference at all.

How can I fix this?

nhgrif
  • 61,578
  • 25
  • 134
  • 173
Makalele
  • 7,431
  • 5
  • 54
  • 81

1 Answers1

2

Not sure why this doesn't work, but you can call the ping method yourself once the address is resolved.

A variable to tell you that you can start pinging:

var canStartPinging = false

The code that calls the ping:

let pinger = SimplePing(hostName: "www.apple.com")
pinger.delegate = self;
pinger.start()

do {
    if (canStartPinging) {
        pinger.sendPingWithData(nil)
    }
    NSRunLoop.currentRunLoop().runMode(NSDefaultRunLoopMode, beforeDate: NSDate.distantFuture() as! NSDate)
} while(pinger != nil)

The SimplePing delegate method to wait for before you can start pinging:

func simplePing(pinger: SimplePing!, didStartWithAddress address: NSData!) {
    println("didStartWithAddress")
    canStartPinging = true
}
Artal
  • 8,933
  • 2
  • 27
  • 30
  • That was it! I missed sendPingWithData! Dunno why apple's example didn't have that and worked.. – Makalele Jun 17 '15 at 17:27
  • Can you please explain this snippet? do { if (canStartPinging) { pinger.sendPingWithData(nil) //For me it crashes here } NSRunLoop.currentRunLoop().runMode(NSDefaultRunLoopMode, beforeDate: NSDate.distantFuture() as! NSDate) //If i dont use this delegate never calls up } while(pinger != nil) – creative_rd Jul 01 '16 at 20:23
  • @Artal - Do you know how we can ping multiple IPAddress with Simple Ping? – creative_rd Aug 02 '16 at 17:48
  • @iRahulDubey I've never really worked with this code in production. Just took a look at the Apple sample code for the purpose of this question. I suggest that you open a separate question and see if someone has the answer.. – Artal Aug 02 '16 at 18:29