3

I am using Xamarin.Android and wrote the following code:

public TextView text;
text = FindViewById<TextView>(Resource.Id.viewIP);
foreach (IPAddress adress in Dns.GetHostAddresses(Dns.GetHostName()))
{
    text.Text = "IP Adress: " + adress;
}

However, when I open the application it shuts down immediately. Am I using the correct way of getting the IP address' of the device?

Cheesebaron
  • 24,131
  • 15
  • 66
  • 118
Jame Hoo
  • 65
  • 4
  • 8

4 Answers4

6

From the Xamarin forums

Java.Util.IEnumeration networkInterfaces = NetworkInterface.NetworkInterfaces;

while(networkInterfaces.HasMoreElements)
{
  Java.Net.NetworkInterface netInterface = 
                            (Java.Net.NetworkInterface)networkInterfaces.NextElement();
  Console.WriteLine(netInterface.ToString());
}
Jason
  • 86,222
  • 15
  • 131
  • 146
2

added to mainifest:

<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.INTERNET" />

for get local ip:

public static string GetLocalIPAddress()
{
    var host = Dns.GetHostEntry(Dns.GetHostName());
    foreach (var ip in host.AddressList)
    {
        if (ip.AddressFamily == AddressFamily.InterNetwork)
        {
            return ip.ToString();
        }
    }
    throw new Exception("Local IP Address Not Found!");
}

See this answer : https://stackoverflow.com/a/6803109/4349342

Community
  • 1
  • 1
c0mmander
  • 77
  • 1
  • 6
0

For me this worked in PCL Xamarin:

public static string GetIPAddress()
{
    var AllNetworkInterfaces = Collections.List(Java.Net.NetworkInterface.NetworkInterfaces);
    var IPAddres = "";
    foreach (var interfaces in AllNetworkInterfaces)
    {
        if (!(interfaces as Java.Net.NetworkInterface).Name.Contains("eth0")) continue;

        var AddressInterface = (interfaces as Java.Net.NetworkInterface).InterfaceAddresses;
        foreach (var AInterface in AddressInterface)
        {
            if(AInterface.Broadcast != null)
                IPAddres = AInterface.Address.HostAddress;
        }
    }
        return IPAddres;
}
Neuron
  • 5,141
  • 5
  • 38
  • 59
0

All the answers I've seen to this question have only gotten the internal IP address of my device while on my home network (198.162.#.#). So I took a slightly different approach, and ask the internet more directly. ipify.org has a nice and simple endpoint for getting your IP address, that can be executed in your shared code. For example...

var client = new HttpClient();
var response = await client.GetAsync("https://api.ipify.org/?format=json");
var resultString = await response.Content.ReadAsStringAsync();

var result = JsonConvert.DeserializeObject<IpResult>(resultString);

var yourIp = result.Ip;

Where "IpResult" is a POCO with a single string property named "Ip" (that you need to create, in addition to this code.)

Dan
  • 1,011
  • 9
  • 6
  • 1
    what if the link gets down at some point? and is there no way at all to get the public IP Address without having to hit any link? – Shailesh Aug 18 '21 at 11:29