Reputation: 7551
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?
Upvotes: 3
Views: 16501
Reputation: 9143
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
}
Upvotes: 2