如何快速获取IP地址 [英] How to get Ip address in swift
问题描述
如何获取我的本地 IP 地址?
How do i get my local IpAddress?
我尝试使用这个 Obj C
示例:如何以编程方式获取iphone的IP地址
I tried to use this Obj C
example: how to get ip address of iphone programmatically
当我到达函数 getifaddrs()
时,我不能再进一步了.我无法使用该功能.
When i get to the function getifaddrs()
i can't get any further. i can't use the function.
是否有其他方法可以做到这一点,还是我的方法错误?
Is there an alternative way to do this, or am I approaching this the wrong way?
推荐答案
正如在讨论中所证明的那样,OP 需要 Mac 上的接口地址,而不是我最初认为的 iOS 设备上的接口地址.问题中引用的代码检查接口名称en0",即iPhone上的WiFi接口.在 Mac 上它使更多相反,检查任何启动并运行"的界面是有意义的.因此,我重写了答案.它现在是代码的 Swift 翻译检测任何连接的网络.
As it turned out in the discussion, OP needs the interface address on a Mac and not on an iOS device as I thought initially. The code referenced in the question checks for the interface name "en0", which is the WiFi interface on the iPhone. On a Mac it makes more sense to check for any "up-and-running" interface instead. Therefore I have rewritten the answer. It is now a Swift translation of the code in Detect any connected network.
getifaddrs()
定义在
中,默认不包含.因此,您必须创建一个桥接头并添加
getifaddrs()
is defined in <ifaddrs.h>
, which is not included by default.
Therefore you have to create a bridging header and add
#include <ifaddrs.h>
以下函数返回一个包含所有本地正常运行"网络接口名称的数组.
The following function returns an array with the names of all local "up-and-running" network interfaces.
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 ...
var ptr = ifaddr
while ptr != nil {
defer { ptr = ptr.memory.ifa_next }
let flags = Int32(ptr.memory.ifa_flags)
let 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(ptr.memory.ifa_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
}
<小时>
Swift 3 的更新:除了采用代码到Swift 3 中的许多变化,迭代所有接口现在可以使用新的通用化sequence()
函数:
Update for Swift 3: In addition to adopting the code to the
many changes in Swift 3,
iterating over all interfaces can now use the new generalized
sequence()
function:
func getIFAddresses() -> [String] {
var addresses = [String]()
// Get list of all interfaces on the local machine:
var ifaddr : UnsafeMutablePointer<ifaddrs>?
guard getifaddrs(&ifaddr) == 0 else { return [] }
guard let firstAddr = ifaddr else { return [] }
// For each interface ...
for ptr in sequence(first: firstAddr, next: { $0.pointee.ifa_next }) {
let flags = Int32(ptr.pointee.ifa_flags)
let addr = ptr.pointee.ifa_addr.pointee
// 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](repeating: 0, count: Int(NI_MAXHOST))
if (getnameinfo(ptr.pointee.ifa_addr, socklen_t(addr.sa_len), &hostname, socklen_t(hostname.count),
nil, socklen_t(0), NI_NUMERICHOST) == 0) {
let address = String(cString: hostname)
addresses.append(address)
}
}
}
}
freeifaddrs(ifaddr)
return addresses
}
这篇关于如何快速获取IP地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!