1

How can I get the local ip address on a wp8 with cordova (phonegap) ? Maybe I need the answer of the following thread, but in C# Get local IP address of client using javascript

Community
  • 1
  • 1
Yiannis Stavrianos
  • 335
  • 1
  • 4
  • 12
  • No it looks like you might need http://stackoverflow.com/questions/391979/get-client-ip-using-just-javascript. This is 100% JavaScript/HTML and relies on a server to bounce back the IP address it see you on. – Paul Annetts Apr 19 '13 at 10:39
  • that's a very good solution, but the app am developing is meant for local wifi network. I want the user to be able to identify their ip. – Yiannis Stavrianos Apr 19 '13 at 11:18
  • I don't know how cordova works, but if it's possible to call some custom .NET assembly from it, then you can get the IP addresses of the phone easily. I mean, you'll implement a function getting the IP addresses in C# or C++, and then call that function from javascript. – Haspemulator Apr 19 '13 at 11:27

2 Answers2

5

If you can call to C# code from cordova, then in C# it's implemented like this:

Windows.Networking.Connectivity.NetworkInformation.GetHostNames();

The resulting collection contains information about all network interfaces of the phone, via instances of Windows.Networking.HostName class. You can get IP addresses from that instance: Windows.Networking.HostName.CanonicalName.

Be warned that you'll probably receive several interfaces in the response. Most probably, you'll get one for cellular network modem and one for Wi-Fi network. If you need to distinguish between them, you can use Windows.Networking.HostName.IPInformation.NetworkAdapter.IanaInterfaceType property. For Wi-Fi, it will contain value 71. For cellular (at least for my current phone and carrier) it displays 244. The full list of values could be found on IANA website.

Haspemulator
  • 11,050
  • 9
  • 49
  • 76
2

This also worked for me in C#.. for those who need it, here's a list of the numbers:

http://msdn.microsoft.com/en-us/library/windows/apps/windows.networking.connectivity.networkadapter.ianainterfacetype(v=win.10).aspx

code example:

 var l = Windows.Networking.Connectivity.NetworkInformation.GetHostNames();
 foreach (var item in l)
 {
   if (item.Type == Windows.Networking.HostNameType.Ipv4 &&
                   ( item.IPInformation.NetworkAdapter.IanaInterfaceType == 71 || // 71 =  An IEEE 802.11 wireless network interface. (device) 
                    item.IPInformation.NetworkAdapter.IanaInterfaceType == 6) )   // 6 =  An Ethernet interface. (emulator)
      {    
         IPAddr = item.DisplayName;
      }

 }
nmishr
  • 287
  • 3
  • 6