2

I need to find the Wi-Fi's router address from my iOS app. I searched the web for a solution but nothing seems to do it. I need something that is not using private APIs an is not against Apple policies.

I have tried solutions found in the following articles and none of them actually give me the router IP address.

How can I determine the default gateway on iPhone?

Fetching IP address of router to which iPhone is connected

Objective-C : How to fetch the router address?

How to get the WIFI gateway address on the iPhone?

There also could be a way to "calculate" the IP from the mask and device IP (or maybe my TCP/IP course is waaaaay to far back in time)...

Thanks!

Community
  • 1
  • 1
Vallieres
  • 859
  • 7
  • 19
  • 3
    I used Thomas Bernard's code shown in your last link to solve the same problem (calling from Swift and with a minor fix to remove a memory leak). It works well. Regarding the IP calculation, you can't do it, the gateway IP could be anywhere inside the valid IP range for the network. – cmatsuoka Jan 12 '15 at 19:45
  • 1
    a quick search on this also reveals the [iOS-System-Services](https://github.com/Shmoopi/iOS-System-Services) library which offers a number of network service calls. And just grabbing the code for getting my own IP address and throwing it into a demo project verified that it works – Louis Tur Jan 12 '15 at 20:12
  • The best way, though hardly easy is the C based System Configuration Framework. Keep in mind that like all network connections it's transient. – uchuugaka Jan 12 '15 at 23:21
  • @LouisTur Yes, I found it and tried it. I give me accurate information on what it reports but sadly not the router IP address. – Vallieres Jan 13 '15 at 13:44
  • @uchuugaka do you have an example? I figured it no one else shared the code it would not be as simple as getRouterIPAddress() ;) – Vallieres Jan 13 '15 at 14:12
  • what does it report v. what it should be for the router? – Louis Tur Jan 13 '15 at 15:29
  • It allows you to create callbacks that tell you the network changed. The method you use above only gives you a snapshot. If you're on a WLAN moving around or if the router decides to change allocations of addresses your IP would be stale. There's also the problem of multi homing. Signals fade and you might end up on another IP or even another interface on your own device. – uchuugaka Jan 13 '15 at 17:59
  • @LouisTur iOS-System-Services gives me the follow: IP Address is: 10.10.158.164, Wi-Fi Netmask Address is: 255.255.255.0, Wi-Fi Broadcast Address is: 10.10.158.255 but it does not have the router IP address which in my present case is 10.10.158.1. – Vallieres Jan 13 '15 at 18:34
  • that's definitely odd @Vallieres not quite sure what to make of it – Louis Tur Jan 15 '15 at 04:46
  • The author of iOS-System-Services implemented my getRouterIPAddress function in his Cordova plugin if ever you are interested. :) – Vallieres Jan 15 '15 at 13:06
  • Any solution to this currently only can get device ip, net mask and broadcast – user1408682 Oct 09 '15 at 10:56

1 Answers1

1

I have found a solution and tested it successfully. It was taken from this repo https://github.com/SiteView/GenieForiOS, I extracted only the code I needed for my getRouterIPAddress function.

Disclaimer: I'm not an Objective-C developer so the following code might have a ton of issues. Please feel free to correct me.

I have also made it into a Cordova plugin and included a demo project in the repo. It does what I need. I have also added an Android version, which is only 3 lines long. :)

https://github.com/vallieres/cordova-plugin-get-router-ip-address

Vallieres
  • 859
  • 7
  • 19