Determine User Location

Jassim Al Rahma 1,166 Reputation points


ia m trying to determine the user location so that. can redirect to the store page based on his location (which is the country or state)

I know it can be done with Xamarin Essentials but what if he did not grant the access?

i though of getting the location from his IP.

I tried below code:

private string GetLocalAddress()
    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!");

but getting:

System.Net.Sockets.SocketException: Could not resolve host 'iPhone'

Kindly advise..

.NET Standard
.NET Standard
A formal specification of .NET APIs that are available on multiple .NET implementations.
493 questions
A Microsoft open-source app platform for building Android and iOS apps with .NET and C#.
4,934 questions
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
7,607 questions
.NET Runtime
.NET Runtime
.NET: Microsoft Technologies based on the .NET software framework.Runtime: The period of time during which a program is being executed in a computer.
983 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Castorix31 69,176 Reputation points

    The method : Get the user's location works for me (Windows 10, VS 2019)
    (test in a Winforms app, without "location capability", I get GeolocationAccessStatus.Allowed and the right location
    with Geoposition pos = await geolocator.GetGeopositionAsync();)