I’m having some issues to get the default gateway IP on iOS using swift. I found an application on apple store called 'Net Analyzer' that can get the default gateway IP as you can see on the image below.
On the screen shot you can see the Default Gateway IP, The DNS Server IP and the device IP address. Can anyone give me a clue how to do it?
Currently I can get the device interfaces to get the device IP, but I want to get the default gateway that the device is connected.
I'm using the current code to get the interfaces addresses
func getIFAddresses() -> [String] {
var addresses = [String]()
// Get list of all interfaces on the local machine:
var ifaddr : UnsafeMutablePointer<ifaddrs>?
guard getifaddrs(&ifaddr) == 0 else { return [] }
guard let firstAddr = ifaddr else { return [] }
// For each interface ...
for ptr in sequence(first: firstAddr, next: { $0.pointee.ifa_next }) {
let flags = Int32(ptr.pointee.ifa_flags)
let addr = ptr.pointee.ifa_addr.pointee
// Check for running IPv4, IPv6 interfaces. Skip the loopback interface.
if ((flags & (IFF_UP|IFF_RUNNING|IFF_LOOPBACK)) == (IFF_UP|IFF_RUNNING)) &&
(addr.sa_family == UInt8(AF_INET) || addr.sa_family == UInt8(AF_INET6)) {
// Convert interface address to a human readable string:
var hostname = [CChar](repeating: 0, count: Int(NI_MAXHOST))
if (getnameinfo(ptr.pointee.ifa_addr, socklen_t(addr.sa_len), &hostname, socklen_t(hostname.count),
nil, socklen_t(0), NI_NUMERICHOST) == 0) {
let address = String(cString: hostname)
addresses.append(address)
}
}
}
freeifaddrs(ifaddr)
return addresses
}
By the way, is there any way to diff the Wi-Fi interface from the Mobile Network interface?