-1
我需要使用蜂窩網絡(而不是WiFi)獲取iPhone/iPad的IP地址。蘋果是否允許這樣做?如果否,有沒有其他方法可以獲得IP地址?是否可以在iOS中獲取移動數據IP地址? (不是服務提供商的公開IP)
感謝您的幫助!
我需要使用蜂窩網絡(而不是WiFi)獲取iPhone/iPad的IP地址。蘋果是否允許這樣做?如果否,有沒有其他方法可以獲得IP地址?是否可以在iOS中獲取移動數據IP地址? (不是服務提供商的公開IP)
感謝您的幫助!
我使用了我的內部IP移動數據
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地址:https://stackoverflow.com/questions/25626117/how-to-get -ip-address-in-swift - 你在找什麼? –
此外,您的蜂窩網絡地址可能比您想象的更復雜。你的接口有一個地址,但是你通過其他地址通過運營商的網絡路由到因特網。通過轉到[this](ipchicken.com) –
查看您的公共IP地址因此,我只能獲得我的公共IP地址?沒有內部IP? @Liam凱利 –