Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get current country of Device in React Native (iOS and Android)?

I am trying to get current country of device in but didn't find anything. Is there something to do so in React Native? I tried using react-native-device-info but it is also not supporting but in previous version it can be get by getDeviceCountry(). Now for the latest version it is showing error:

TypeError: _reactNativeDeviceInfo.default.getDeviceCountry is not a function. (In '_reactNativeDeviceInfo.default.getDeviceCountry()', '_reactNativeDeviceInfo.default.getDeviceCountry' is undefined)

like image 317
Shivam Tiwari Avatar asked Sep 26 '19 09:09

Shivam Tiwari


People also ask

How do I get my current location in React Native?

Geolocation is enabled by default when you create a project with react-native init . In order to enable geolocation in the background, you need to include the 'NSLocationAlwaysUsageDescription' key in Info. plist and add location as a background mode in the 'Capabilities' tab in Xcode.

How do you check if device is Android or iOS in React Native?

An example to Detect Device is Android or IOS using React Native Platform includes the use of React Native Platform Component. As the topic name describes itself, we will detect the Device is Android or IOS. This is very useful while we have to load different elements according to the different OS.


1 Answers

According to the documentation of react-native-device-info for latest version, they have moved some of their apis to react-native-localize to reduce duplication in the react-native-community modules. react-native-localize worked perfectly for me.

Setup:

$ npm install --save react-native-localize
# --- or ---
$ yarn add react-native-localize

Usage:

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

console.log(RNLocalize.getLocales());
console.log(RNLocalize.getCurrencies());
console.log(RNLocalize.getCountry());
console.log(RNLocalize.getCalendar());
console.log(RNLocalize.getTemperatureUnit());
console.log(RNLocalize.getTimeZone());
console.log(RNLocalize.uses24HourClock());

and many more. For detailed description please visit their official documentation by the given link: react-native-localize

like image 186
Shivam Tiwari Avatar answered Sep 18 '22 21:09

Shivam Tiwari