Search code examples
iosswiftclgeocoder

What is the best way to get user's current city, state, and Country in iOS Swift?


I know I can use CLGeocoder to get the user's location like locality, administrative area, and country. I just need this information(city, state, and country) while logging alone to my app. I don't want to my user to turn on the location services. I have seen apps like Binance where they are getting the city , state, and country without turning on the user's location.


Solution

  • The best way to access a user's location is through CLGeocoder as it is the most reliable source . That is the only way you can get the most accurate location .Given the fact that you do not want to use them you have limited options .

    Best option would be to actually let the user select his location in an un-harmful way . This would not guarantee a successfull return for your request though .

    You could also use NSLocale and time zone but they are not reliable because they're dependent on user's settings.

    Your best option in my opinion is to try to access the user's location through IP .So reverse geocoding the IP address of a web service request using GeoIP

    I'v attached a few links i found below , take a look ,hopefully its of use to you . You could also take a look at Maximinds GeoIP Api . Might be useful

    How to get an Ip address programatically

    Maximinds GeoIP API