Reputation: 155
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.
Upvotes: 4
Views: 11802
Reputation: 2343
getDeviceCountry() is removed from react-native-device-info.
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'
Upvotes: 9
Reputation: 1374
it can be done by two ways
get the IP address of user but it works only with internet connection
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
Upvotes: 8