Hi all, As far as my research, I found there are two ways to ping an IP Address (ipv4 and ipv6) in MacOS in Swift.
- SimplePing
- Run command line in code like below
func doPing() {
let task = Process()
task.executableURL = URL(fileURLWithPath: "/sbin/ping")
task.arguments = [ "-c", "4", "10.1.141.100" ]
let outputPipe = Pipe()
task.standardOutput = outputPipe
let outputHandle = outputPipe.fileHandleForReading
outputHandle.readabilityHandler = { pipe in
if let ouput = String(data: pipe.availableData, encoding: .utf8) {
if !ouput.isEmpty {
log += ouput
print("----> ouput: \(ouput)")
}
} else {
print("Error decoding data: \(pipe.availableData)")
}
}
task.launch()
}
So which way is better and which way should I implement for the project?