问题描述
如何获取本地IpAddress?
How do i get my local IpAddress?
我尝试使用此 Obj C
示例:
当我到达函数 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 theinterface name "en0", which is the WiFi interface on the iPhone. On a Mac it makes moresense to check for any "up-and-running" interface instead.Therefore I have rewritten the answer. It is now a Swift translation of the code inDetect any connected network.
getifaddrs()
在中定义< ifaddrs.h>
,默认情况下不包括在内。
因此你必须创建一个桥接头并添加
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>
以下函数返回
一个数组,其中包含所有本地up-and-的名称运行网络接口。
The following function returnsan 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的更新:除了采用代码
,
迭代所有接口现在可以使用新的通用
功能:
Update for Swift 3: In addition to adopting the code to themany changes in Swift 3,iterating over all interfaces can now use the new generalizedsequence()
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
}
这篇关于如何在swift中获取IP地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!