-2

I need to get the exact country location of the user without using location permissions.

I have tried using locale, but it can be changed manually by user.

  let loc = Locale.current
  print(loc.regionCode)

Right now i am getting location using locale, but it can be changed by the user manually. I need to check the exact country of the user.

rmaddy
  • 314,917
  • 42
  • 532
  • 579
sreehari
  • 43
  • 5
  • 2
    What OS is this for? iOS? macOS? Linux? How do you expect to know what country the user is in without getting their actual location? – rmaddy Oct 22 '19 at 05:20
  • for iOS , i need the country only not the exact lat and long – sreehari Oct 22 '19 at 05:22
  • 4
    You can't get their location without their permission as it is the point of location permissions. It would be a breach of privacy. – STerrier Oct 22 '19 at 05:24
  • One thing you can do is to read the carrier country code from the SIM card but you can't rely on that as well since people do travel. – Desdenova Oct 22 '19 at 06:18
  • you may find [this answer](https://stackoverflow.com/a/72875861/5064932) helpful – crcalin Jul 05 '22 at 21:48

1 Answers1

0

There are web API services your app could contact to obtain the rough location of the ip address. It's not at all guaranteed to be the correct country of the device, as your user may be using an out-of-country VPN service, but it's probably the only thing you can do without asking the device for location.

Here's an example service. I know nothing about the quality of this service (I picked this one at random for this answer). There seem to be many out there, so do your homework as to which would be best for your users and yourself.

There's another approach you could take: simply ask the user, and have faith that most of them will answer honestly. If their answer doesn't make sense per the timezone of the device, maybe suspect their answer is false.

Smartcat
  • 2,834
  • 1
  • 13
  • 25