I am able to get the current IP address of my device/machine that I am using - by using this question's answer.
I have gone through this question. Java allows to get the IP address from a domain name. Is it possible in Objective C? How?
The second question is How to get the name of device/machine by using its IP address. Say for example I have an IP address 192.168.0.74 = What is the device name? in Objective C?
I wrote a Swift version of the accepted answer, though I'm not 100% sure of its correctness.
I'm not sure if this is the best way to do this, but it works for me, mostly. I put in StackOverflow's IP addresses (69.59.196.211) and it gave me back
stackoverflow.com
, but I put in one of Google's IP addresses (210.55.180.158) and it gave me backcache.googlevideo.com
(for all results, not just the first one).There can be multiple names for the address, so you might not want to stop at the first one you find.
You need to read the routing table - basically the same way as "netstat -r" command does. The netstat implementation is opensource - it's in the package
network_cmds-396.6
at
http://www.opensource.apple.com/release/mac-os-x-1082/
so you can check what it does. The default gateway contains the "G" flag but shouldn't connect the "I" flag (when both wifi and cell are active, wifi is used for internet connection - the cell gateway is not used and is given the "I" flag).
Hope it helps.