在swift中使用Simple Ping(iOS) [英] Using Simple Ping in swift (iOS)
问题描述
我正在尝试使用Apple的类:简单Ping,但是我无法使用它。
I'm trying to use Apple's class: Simple Ping, but I can't get this working.
当我运行示例mac os x项目时它是工作:
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] pinging 192.168.1.102
2015-06-17 00:03:22.569 SimplePing [20386:3133535]#0已发送
2015-06-17 00:03:22.569 SimplePing[20386:3133535] #0 sent
2015-06-17 00: 03:22.570 SimplePing [20386:3133535]#0收到
2015-06-17 00:03:22.570 SimplePing[20386:3133535] #0 received
2015-06-17 00:03:23.570 SimplePing [20386:3133535]#1已发送
2015-06-17 00:03:23.570 SimplePing[20386:3133535] #1 sent
2015-06-17 00:03:23.571 SimplePing [20386:3133535]#1收到
2015-06-17 00:03:23.571 SimplePing[20386:3133535] #1 received
等..
但是当我从我的ios(swift)应用程序执行此操作时:
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)
不确定是否需要while循环 - 可能不需要。无论如何我也试过没有它。
Not sure is do..while loop is needed - probably not needed. Anyway I tried without it too.
我已经将SimplePingDelegate添加到我的班级:
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")
}
所以它给了我输出:
2015-06-17 00:32:12.368可用[938:150352] CFHostStartInfoResolution
2015-06-17 00:32:12.368 Available[938:150352] CFHostStartInfoResolution
2015-06 -17 00:32:12.374可用[938:150352]> HostResolveCallback
2015-06-17 00:32:12.374 Available[938:150352] >HostResolveCallback
didStartWithAddress
didStartWithAddress
不调用其他函数。为什么?
other functions aren't called. Why?
Btw。我也尝试将pinger移动到类变量中:
Btw. I've also tried moving pinger into class variable like this:
var pinger: SimplePing?
完全没有区别。
如何我能解决这个问题吗?
How can I fix this?
推荐答案
不确定为什么这不起作用,但你可以在地址后自己调用ping方法已解决。
Not sure why this doesn't work, but you can call the ping method yourself once the address is resolved.
一个变量,告诉你可以开始ping:
A variable to tell you that you can start pinging:
var canStartPinging = false
调用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)
在开始ping之前等待的 SimplePing
委托方法:
The SimplePing
delegate method to wait for before you can start pinging:
func simplePing(pinger: SimplePing!, didStartWithAddress address: NSData!) {
println("didStartWithAddress")
canStartPinging = true
}
这篇关于在swift中使用Simple Ping(iOS)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!