问题
How get current ip-address for Xamarin.Forms ( Cross Platform) ? WifiManager I can't use and can't WifiManager include?
回答1:
public string MyIp;
foreach (IPAddress adress in Dns.GetHostAddresses(Dns.GetHostName()))
{
MyIp = "IP Adress: " + adress.ToString();
SettingsTab.IP_Adress = MyIp;
break;
}
回答2:
try to do this:
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;
}
回答3:
Or try this:
var IpAddress = Dns.GetHostAddresses(Dns.GetHostName()).FirstOrDefault();
if (IpAddress != null)
{
return IpAddress.ToString();
}
来源:https://stackoverflow.com/questions/27904375/get-current-ip-address-xamarin-forms-cross-platform