[英]Get IP Address of Device connected to my iPhone's Personal Hotspot using Swift
I have a requirement where I need to get the IP Addresses of the devices which are currently connected with my iPhone's personal hotspot.我有一个要求,我需要获取当前与我的 iPhone 的个人热点连接的设备的 IP 地址。
Tried the internet for a long, but not getting any luck finding the solution to this.在互联网上尝试了很长时间,但没有找到解决方案的运气。
Below is the app I found which is scanning and providing similar stuff I need.下面是我找到的应用程序,它正在扫描并提供我需要的类似内容。
https://apps.apple.com/in/app/network-analyzer/id562315041 https://apps.apple.com/in/app/network-analyzer/id562315041
So technically I came to know this is a possible thing, but not getting any starting point at the moment.所以从技术上讲,我开始知道这是一件可能的事情,但目前没有任何起点。
I appreciate any hint, guidance on this.我感谢任何提示,这方面的指导。 Thanks a lot in advance非常感谢提前
To get IPAddress for wifi , wired, and cellular - swift 5获取 wifi、有线和蜂窝的 IP 地址 - 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 ?? ""
}
How to use如何使用
let strIPAddress : String = self.getIPAddress()
print("IPAddress :: \(strIPAddress)")
Note : Add this below in your project's Bridging-Header file注意:在您项目的 Bridging-Header 文件中添加以下内容
#include<ifaddrs.h>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.