Starbody
Starbody

Reputation: 155

How to get user country without gps

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

Answers (2)

Ilarion Halushka
Ilarion Halushka

Reputation: 2343

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'

Upvotes: 9

Jigar
Jigar

Reputation: 1374

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

Upvotes: 8

Related Questions