记录一下这个ip地址获取,网上大多数都是获取本机ip地址,而我需要获取连接局域网ip地址
获取下图标记的路由器ip地址
话不多说 看干活
static func getIpAdress() -> String {
let queue = DispatchQueue(label: "Monitor")
let semaphore = DispatchSemaphore(value: 0)
// 监听Wi-Fi是否连接
var ipString = “”
if #available(iOS 13.0, *) {
let monitor = NWPathMonitor(requiredInterfaceType: .wifi)
// 连上Wi-Fi后获取IP
monitor.pathUpdateHandler = { path in
if path.status == .satisfied {
let endpoint = String(describing: path.gateways[0])
ipString = "http://" + endpoint.components(separatedBy: (":"))[0] + port
print("ip路由器地址: \(ipString)")
}
semaphore.signal()
}
monitor.start(queue: queue)
semaphore.wait()
}
return ipString
}