how do I get the IP address (e.g. 192.168.0.1) of a local hostname (e.g. "myComputer") on my local network in swift?
如何在swift本地网络上获取本地主机名(例如“myComputer”)的IP地址(例如192.168.0.1)?
I tried this:
我试过这个:
let host = CFHostCreateWithName(nil,"www.google.com").takeRetainedValue();
CFHostStartInfoResolution(host, .Addresses, nil);
var success: Boolean = 0;
let addresses = CFHostGetAddressing(host, &success).takeUnretainedValue() as NSArray;
if (addresses.count > 0){
let theAddress = addresses[0] as NSData;
var hostname = [CChar](count: Int(NI_MAXHOST), repeatedValue: 0)
if getnameinfo(UnsafePointer(theAddress.bytes), socklen_t(theAddress.length),
&hostname, socklen_t(hostname.count), nil, 0, NI_NUMERICHOST) == 0 {
if let numAddress = String.fromCString(hostname) {
println(numAddress)
}
}
}
This works fine for addresses like "www.google.com" but not for hostnames like "myComputer"
这适用于“www.google.com”等地址,但不适用于“myComputer”等主机名
I tried it in Xcode Simulator. There it works, but not on my iPhone
我在Xcode Simulator中尝试过它。它有效,但不适用于我的iPhone
I'll get the error:
我会收到错误:
fatal error: unexpectedly found nil while unwrapping an Optional value
... in Line 4.
......在第4行。
Thanks for your help!
谢谢你的帮助!
1 个解决方案
#1
0
Local hostnames such as myComputer will only be available to your iPhone if it's DNS server is configured to resolve them (or they are in the hosts file).
只有当DNS服务器配置为解析它们(或者它们位于hosts文件中)时,myComputer等本地主机名才可用于您的iPhone。
To resolve myComputer on your iPhone, you would need to have your iPhone configured to use a local DNS server that would serve IP addresses for the hostnames of the machines on your local network.
要在iPhone上解析myComputer,您需要将iPhone配置为使用本地DNS服务器,该服务器将为本地网络上计算机的主机名提供IP地址。
The error you're seeing relates to not getting a result. You'll need to check that the host resolves (CFHostStartInfoResolution
provides a result for this):
您看到的错误与未获得结果有关。您需要检查主机是否解析(CFHostStartInfoResolution为此提供了结果):
let host = CFHostCreateWithName(nil,"www.google.com").takeRetainedValue();
var success : Boolean = CFHostStartInfoResolution(host, .Addresses, nil);
if success > 0 {
success = 0;
let addresses = CFHostGetAddressing(host, &success).takeUnretainedValue() as NSArray
if (addresses.count > 0){
let theAddress = addresses[0] as! NSData;
var hostname = [CChar](count: Int(NI_MAXHOST), repeatedValue: 0)
if getnameinfo(UnsafePointer(theAddress.bytes), socklen_t(theAddress.length),
&hostname, socklen_t(hostname.count), nil, 0, NI_NUMERICHOST) == 0 {
if let numAddress = String.fromCString(hostname) {
println(numAddress)
}
}
}
} else {
println("Host not found!")
}
#1
0
Local hostnames such as myComputer will only be available to your iPhone if it's DNS server is configured to resolve them (or they are in the hosts file).
只有当DNS服务器配置为解析它们(或者它们位于hosts文件中)时,myComputer等本地主机名才可用于您的iPhone。
To resolve myComputer on your iPhone, you would need to have your iPhone configured to use a local DNS server that would serve IP addresses for the hostnames of the machines on your local network.
要在iPhone上解析myComputer,您需要将iPhone配置为使用本地DNS服务器,该服务器将为本地网络上计算机的主机名提供IP地址。
The error you're seeing relates to not getting a result. You'll need to check that the host resolves (CFHostStartInfoResolution
provides a result for this):
您看到的错误与未获得结果有关。您需要检查主机是否解析(CFHostStartInfoResolution为此提供了结果):
let host = CFHostCreateWithName(nil,"www.google.com").takeRetainedValue();
var success : Boolean = CFHostStartInfoResolution(host, .Addresses, nil);
if success > 0 {
success = 0;
let addresses = CFHostGetAddressing(host, &success).takeUnretainedValue() as NSArray
if (addresses.count > 0){
let theAddress = addresses[0] as! NSData;
var hostname = [CChar](count: Int(NI_MAXHOST), repeatedValue: 0)
if getnameinfo(UnsafePointer(theAddress.bytes), socklen_t(theAddress.length),
&hostname, socklen_t(hostname.count), nil, 0, NI_NUMERICHOST) == 0 {
if let numAddress = String.fromCString(hostname) {
println(numAddress)
}
}
}
} else {
println("Host not found!")
}