NWEndPoint:从另一个VC获取网关的IP地址时,它返回nil

问题描述

我已经找到了解决这个问题的方法,请参见下面的答案。

我定义了一个函数getGatewayInfo(),以获取网关IP地址并将其返回给调用方。 但是,当我从其他VC的print(NetworkUtility().getGatewayInfo())调用此函子viewDidLoad时,它返回nil。

从日志中,我可以看到gatewayIPAddressNWEndpoint获得了值,即192.168.1.1。但是,它返回nil并打印出“找不到网关IP地址!”。你能给我一些提示我做错了什么吗?

Gateway IP address is not found!
Gateway: 192.168.1.1
import UIKit
import Network

class NetworkUtility {
    var gatewayIPAddress: String?
    
    func getGatewayInfo() -> String {
        let monitor = NWPathMonitor(requiredInterfaceType: .wifi)
        monitor.pathUpdateHandler = { path in
            let endpoint = path.gateways[0]
            
            switch endpoint {
            case .hostPort(let host,_):
                self.gatewayIPAddress = host.debugDescription
                print("Gateway: \(self.gatewayIPAddress!)")
            default:
                break
            }
        }
        monitor.start(queue: dispatchQueue(label: "nwpathmonitor.queue"))
        
        if let gatewayIPAddress = gatewayIPAddress {
            return gatewayIPAddress
        } else {
            return "Gateway IP address is not found!"
        }
    }
}

解决方法

这里的问题是monitor.pathUpdateHandler路径中的处理程序代码(关闭)是异步执行的,因此先前代码中的return语句将在此之前执行。最终,返回的参数为nil

由于我们不知道闭包中的代码何时会像一些网络请求一样完成。因此,我们无法在此函数中使用return方法。相反,我们应该使用另一个完成处理程序来返回参数值,即回调。就像我们在某些JSON请求函数中所做的一样。

涉及另一个completionHandler的代码,以防返回字符串参数:

func getGatewayInfo(completionHandler: @escaping (String) -> ()) {
    let monitor = NWPathMonitor(requiredInterfaceType: .wifi)
    monitor.pathUpdateHandler = { path in
        if let endpoint = path.gateways.first {
            switch endpoint {
            case .hostPort(let host,_):
                let remoteHost = host.debugDescription
                print("Gateway: \(remoteHost)")
                // Use callback here to return the ip address to the caller
                completionHandler(remoteHost)
            default:
                break
            }
        } else {
            print("Wifi connection may be dropped.")
        }
    }
    monitor.start(queue: DispatchQueue(label: "nwpathmonitor.queue"))
}

呼叫者:

override func viewDidLoad() {
    super.viewDidLoad()
    title = pageTitle
    
    // Receive remoteHost from callback parameter(aka. gateway ip address)
    NetworkUtility().getGatewayInfo { (remoteHost) in
        print("remote: \(remoteHost)")
        DispatchQueue.main.async {
            self.gwIPAddress.text = remoteHost
        }
    }
}