How to get user country without gps

Viewed 7228

In react native i am trying to get user country without using the users gps, just like Wechat registration which automatically gets the users country in the picker(select) field.

2 Answers

it can be done by two ways

  1. get the IP address of user but it works only with internet connection

  2. the best way is to get the local country of the device itself with the use of react-native-device-info

const deviceCountry = DeviceInfo.getDeviceCountry(); // "US", "IN"

ref: react-native-device-info#getdevicecountry

getDeviceCountry() is removed from react-native-device-info.

Use react-native-localize:

import * as RNLocalize from "react-native-localize";

RNLocalize.getCountry(); // -> 'UA'


If you want to get a full country name like "United States" then you can add following json file to your project: https://gist.github.com/ssskip/5a94bfcd2835bf1dea52

And then import and get full country name by code:

const countries = require('./countries.json')
// Or in ES6:
// import countries from './countries.json'

countries[RNLocalize.getCountry()] // -> 'United States'
Related