How to retrieve the CountryCode / ISO?
Asked Answered
C

4

8

Our intention is to filter the content based on location without requesting Location permission from the Android device,

In-order to meet this requirement, we have tried the following options and still its not reliable

  1. Telephony Manager

it wont work without a Sim card, again say if the user from USA travels india now the below snippets returns US not India

var telephonyManager = context?.getSystemService(Context.TELEPHONY_SERVICE) as TelephonyManager
val countryCodeValue = telephonyManager.networkCountryIso
  1. Locale

Locale returns US or UK by default even if they are in India, usually user's Locale would be in English by default

 Locale.getDefault().getCountry()
  1. GPS

We doesn't want the Location permission to be requested

  1. Retrieving the Country details form the logged-in Google account

Reply from Google - They dont have any dedicated API to retrieve countryCode of an user Note: iOS has a dedicated API which tell the CountryCode of the user, but Android doesn't

  1. IP address

We tried to get the IP of the Android device (from several IP fusion websites), there are following two cases

  • User connected to WiFi - we are able to get the countryCode (WIFI IP : 64.134.234.17)
  • User connected to Mobile Data - we are getting the wrong countryCode even we are in USA, it says that IP is from JAPAN (MOBILE NETWORK IP: 210.253.218.106)

Note: Even Netflix determine the country during the launch, without requesting the Location permission

Cobbler answered 14/6, 2018 at 7:59 Comment(2)
A long ago i had used http://freegeoip.net/json/ . You can give it a try if you already haven't .Pomade
@Cobbler please correct Your problem's title +"without asking permission".Dorkus
H
3

I think you have only two possibilities...or ask permission...or without asking permission you have to ask the user to insert an address (also only the city) and retrive the country code by mean of Google Geocoding Service

Heliotrope answered 14/6, 2018 at 10:48 Comment(0)
S
3

I agreed the above comments. You can't get the precise country by anyways other than there GPS location. You can read this one hope you will get some idea about

Regarding your query how netflix is using it

https://help.netflix.com/en/node/26100

Brief

1.Disable any proxies (Means they are checking with there ip's)

2.Check your current IP address by visiting whatismyip.com (Same)

Regards

Sikh answered 26/6, 2018 at 7:54 Comment(0)
G
3

have you try using an API ex: http://ip-api.com/json

that will return country code and many other information all by analyze request IP "(no need obtained IP from device)" .

but obvious we need assume user not use any proxy or vpn

(There are plenty of other apis or you can implement and host one by yourself)

Gametophore answered 26/6, 2018 at 10:18 Comment(0)
M
0

No way of getting it without Location permission, your better approximation is the localization by ip but as you saw there is no guarantee of precise localization

Marquetry answered 14/6, 2018 at 8:12 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.