我有一个要求,我需要得到当前与我的iPhone的个人热点连接的设备的IP地址。
在网上试了很长时间,但没有找到解决这个问题的办法。
下面是我发现的应用程序,它是扫描和提供类似的东西,我需要。
https://apps.apple.com/in/app/network-analyzer/id562315041
所以从技术上讲,我知道这是一件可能的事情,但目前还没有任何起点。
我感谢任何提示,指导在这方面。提前谢谢你
获取wifi,有线和蜂窝的IPAddress - swift 5
func getIPAddress() -> String {
var address: String?
var ifaddr: UnsafeMutablePointer<ifaddrs>? = nil
if getifaddrs(&ifaddr) == 0 {
var ptr = ifaddr
while ptr != nil {
defer { ptr = ptr?.pointee.ifa_next }
guard let interface = ptr?.pointee else { return "" }
let addrFamily = interface.ifa_addr.pointee.sa_family
if addrFamily == UInt8(AF_INET) || addrFamily == UInt8(AF_INET6) {
// wifi = ["en0"]
// wired = ["en2", "en3", "en4"]
// cellular = ["pdp_ip0","pdp_ip1","pdp_ip2","pdp_ip3"]
let name: String = String(cString: (interface.ifa_name))
if name == "en0" || name == "en2" || name == "en3" || name == "en4" || name == "pdp_ip0" || name == "pdp_ip1" || name == "pdp_ip2" || name == "pdp_ip3" {
var hostname = [CChar](repeating: 0, count: Int(NI_MAXHOST))
getnameinfo(interface.ifa_addr, socklen_t((interface.ifa_addr.pointee.sa_len)), &hostname, socklen_t(hostname.count), nil, socklen_t(0), NI_NUMERICHOST)
address = String(cString: hostname)
}
}
}
freeifaddrs(ifaddr)
}
return address ?? ""
}
如何使用
let strIPAddress : String = self.getIPAddress()
print("IPAddress :: (strIPAddress)")
注意:在项目的bridge - header文件
中添加以下内容#include<ifaddrs.h>