How can I determine the default gateway on iPhone?

后端 未结 5 698
栀梦
栀梦 2020-12-03 09:31

I need to obtain the IP address of the default gateway in an iPhone application. I cannot find the proper API. Anyone know if iPhone exposes this information?

5条回答
  •  离开以前
    2020-12-03 09:48

    On iOS ≥ 12, you can use the Network framework.

    The NWPath you get from the NWPathMonitor.pathUpdateHandler will have the NWPath.gateways variable set to the gateways IPs.

    let monitor = NWPathMonitor(requiredInterfaceType: .wifi)
    monitor.pathUpdateHandler = { path in
        print(path.gateways)
    }
    monitor.start(queue: DispatchQueue(label: "nwpathmonitor.queue"))
    

提交回复
热议问题