Swift - 获取设备的WIFI IP地址我需要在Swift中获取iOS设备的IP地址。这不是关于此的其他问题的重复!我需要只获得WiFi IP地址,如果没有wifi地址 - 我需要处理它。Stack Overflow上有一些关于它的问题,但是只有返回ip地址的函数。例如(来自如何在swift中获取IP地址):func getIFAddresses() -> [String] {
var addresses = [String]()
// Get list of all interfaces on the local machine: var ifaddr : UnsafeMutablePointer<ifaddrs> = nil
if getifaddrs(&ifaddr) == 0 {
// For each interface ... for (var ptr = ifaddr; ptr != nil; ptr = ptr.memory.ifa_next) {
let flags = Int32(ptr.memory.ifa_flags)
var addr = ptr.memory.ifa_addr.memory // Check for running IPv4, IPv6 interfaces. Skip the loopback interface. if (flags & (IFF_UP|IFF_RUNNING|IFF_LOOPBACK)) == (IFF_UP|IFF_RUNNING) {
if addr.sa_family == UInt8(AF_INET) || addr.sa_family == UInt8(AF_INET6) {
// Convert interface address to a human readable string: var hostname = [CChar](count: Int(NI_MAXHOST), repeatedValue: 0)
if (getnameinfo(&addr, socklen_t(addr.sa_len), &hostname, socklen_t(hostname.count),
nil, socklen_t(0), NI_NUMERICHOST) == 0) {
if let address = String.fromCString(hostname) {
addresses.append(address)
}
}
}
}
}
freeifaddrs(ifaddr)
}
return addresses}在这里,我得到2个值 - 来自移动互联网(我认为)的地址和我需要的WiFi地址。有没有其他方法来获得只有WiFi IP地址?
3 回答
- 3 回答
- 0 关注
- 1367 浏览
添加回答
举报
0/150
提交
取消