swift - 获取 Apple Watch 的 IP 地址
问题描述
如何以编程方式访问 Apple Watch 的 IP 地址?我在 iOS 中使用 UIDevice 扩展来查找 IP。这是 UIDevice 的扩展,但在 watchOS 中不受支持,其他选项可能是什么?
extension UIDevice {
private struct InterfaceNames {
static let wifi = ["en0"]
static let wired = ["en2", "en3", "en4"]
static let cellular = ["pdp_ip0","pdp_ip1","pdp_ip2","pdp_ip3"]
static let supported = wifi + wired + cellular
}
func ipAddress() -> String? {
var ipAddress: String?
var ifaddr: UnsafeMutablePointer<ifaddrs>?
if getifaddrs(&ifaddr) == 0 {
var pointer = ifaddr
while pointer != nil {
defer { pointer = pointer?.pointee.ifa_next }
guard
let interface = pointer?.pointee,
interface.ifa_addr.pointee.sa_family == UInt8(AF_INET) || interface.ifa_addr.pointee.sa_family == UInt8(AF_INET6),
let interfaceName = interface.ifa_name,
let interfaceNameFormatted = String(cString: interfaceName, encoding: .utf8),
InterfaceNames.supported.contains(interfaceNameFormatted)
else { continue }
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)
guard
let formattedIpAddress = String(cString: hostname, encoding: .utf8),
!formattedIpAddress.isEmpty
else { continue }
ipAddress = formattedIpAddress
break
}
freeifaddrs(ifaddr)
}
return ipAddress
}
}
UIDevice.current.ipAddress()
这在 watchOS 中怎么可能?
解决方案
推荐阅读
- ajax - 在 WebSockets 之前,实时浏览器聊天是如何实现的?
- json - scala - 如何将 JSON 字符串解析为单独的记录?
- android - Google Play Console App Analytics 极其乏善可陈?
- django - 如果用户忘记其用户名django如何处理
- c# - 具有多个按钮的 Unity ScrollView
- openssl - 使用 openssl 创建具有包属性的 TLS 私钥
- azure - 使用 Azure 逻辑应用将响应从 HTTP 保存到 Blob 存储
- ansible - 我可以在 ansible 模块中有多个参数值吗?
- spring-boot - 通过 Shared Redis 会话连接的两个 Spring 应用程序的 AuthenticationSuccessEvent Listener
- javascript - KeyboardEvent event.key 和 event.code 不同