Onur Buyukcaglar
Onur Buyukcaglar

Reputation: 149

.NET MAUI how to obtain Wifi/Connected network ip address of the device on iOs and Android?

How do we get the local device IP address (not the external IP address) in .NET MAUI on platforms iOS and Android?

The following answer specifies a solution for Android by using WifiManager.ConnectionInfo which Visual Studio clearly warns that the WifiManager.ConnectionInfo is obsoleted on Android 31 and higher;

Get IP address from Android and IOS Device using .NET MAUI

And the following GitHub page provides an example for iOS using NSNetService.Addresses. Again, Visual Studio states that NSNetService.Addresses is obsoleted on iOS 15 and higher and recommends using Network framework, but the documentation is mess and there aren't any examples available;

https://gist.github.com/jbe2277/4b9f5ac0fb3556569960d2e1f3c5bf28

Upvotes: 1

Views: 1384

Answers (1)

Himanshu Dwivedi
Himanshu Dwivedi

Reputation: 8139

On Android 31.0 or later, using WifiManager.ConnectionInfo is obsolete. Instead, you can use ConnectivityManager as follows to get the device's IP address:

string ipAddress = null;
var connectivityManager = (ConnectivityManager)Platform.AppContext.GetSystemService(Context.ConnectivityService);
var linkProperties = connectivityManager?.GetLinkProperties(connectivityManager?.ActiveNetwork);

if (linkProperties != null)
{
    foreach (var linkAddress in linkProperties.LinkAddresses)
    {
        if (linkAddress?.Address is Java.Net.Inet4Address inet4Address)
        {
            ipAddress = inet4Address.HostAddress;
        }
    }
}

Upvotes: 0

Related Questions