Hi, I want to force my app to use wifi for it to work. For that im using the en0 interface. It works fine,but does an iphone always use this interface. In my case, should I check en1 and en2 interfaces for wifi ?
Always using en0 for wifi?
You should not rely on any particular interface name being the Wi-Fi interface. You can require Wi-Fi in your NWParameters as an interface type: https://developer.apple.com/documentation/network/nwparameters/2998706-requiredinterfacetype
Avoid checking like this, but if you absolutely need the name, you can use a NWPathMonitor to require Wi-Fi and then check the name of the interface that it returns.
May I ask how you finally solved it.
finally solved it.
Finally, solved what?
As ekinnear pointed out, it’s not safe to assume that the standard Wi-Fi interface is en0
. If you want to identify an interface, it’s best to do that via its attributes. However, the correct way to do that depends on why you’re doing that.
Why do you need to identify the Wi-Fi interface?
Share and Enjoy
—
Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"
Is there any way to correctly get the gateway address of the iPhone connected to the hotspot?
If you’re looking for the gateway address for the current Wi-Fi network, NWPathMonitor
can do that for you. For example:
var monitor: NWPathMonitor?
func start() {
let m = NWPathMonitor(requiredInterfaceType: .wifi)
self.monitor = m
m.pathUpdateHandler = { newPath in
print(newPath.gateways)
}
m.start(queue: .main)
}
This code comes with some important caveats:
-
There’s no guarantee that the Wi-Fi interface is relevant. The device might be connected to the Internet over WWAN, with the Wi-Fi being associated to a network published by a Wi-Fi accessory that doesn’t have Internet connectivity.
-
Or the user might have Ethernet plugged in.
-
There’s no guarantee that there’s only one Wi-Fi interface. If there are multiple, this code will give you the ‘best’ one, for some definition of best.
-
Addresses can be an arbitrary mix of IPv4 and IPv6.
Share and Enjoy
—
Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"