react-native-device-info
Device Information for React Native.
TOC
Installation
Using npm:
npm install --save react-native-device-info
or using yarn:
yarn add react-native-device-info
⚠️ If you are on React Native > 0.47, you must use version 0.11.0 of this library or higher
Linking
Automatic
react-native link react-native-device-info
(or using rnpm
for versions of React Native < 0.27)
rnpm link react-native-device-info
Manual
iOS (via Cocoa Pods)
Add the following line to your build targets in your Podfile
pod 'RNDeviceInfo', :path => '../node_modules/react-native-device-info'
Then run pod install
iOS (without Cocoa Pods)
In XCode, in the project navigator:
- Right click Libraries
- Add Files to [your project's name]
- Go to
node_modules/react-native-device-info
- Add the
.xcodeproj
file
In XCode, in the project navigator, select your project.
- Add the
libRNDeviceInfo.a
from the deviceinfo project to your project's Build Phases ➜ Link Binary With Libraries - Click
.xcodeproj
file you added before in the project navigator and go the Build Settings tab. Make sure All is toggled on (instead of Basic). - Look for Header Search Paths and make sure it contains both
$(SRCROOT)/../react-native/React
and $(SRCROOT)/../../React
- Mark both as recursive (should be OK by default).
Run your project (Cmd+R)
(Thanks to @brysgo for writing the instructions)
Android
- optional in
android/build.gradle
:
...
ext {
// dependency versions
googlePlayServicesVersion = "<Your Services Version>"
}
...
- in
android/app/build.gradle
:
dependencies {
...
compile "com.facebook.react:react-native:+" // From node_modules
+ compile project(':react-native-device-info')
}
- in
android/settings.gradle
:
...
include ':app'
+ include ':react-native-device-info'
+ project(':react-native-device-info').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-device-info/android')
With React Native 0.29+
+ import com.learnium.RNDeviceInfo.RNDeviceInfo;
public class MainApplication extends Application implements ReactApplication {
//......
@Override
protected List<ReactPackage> getPackages() {
return Arrays.<ReactPackage>asList(
+ new RNDeviceInfo(),
new MainReactPackage()
);
}
......
}
With older versions of React Native:
+ import com.learnium.RNDeviceInfo.RNDeviceInfo;
public class MainActivity extends ReactActivity {
......
@Override
protected List<ReactPackage> getPackages() {
return Arrays.<ReactPackage>asList(
+ new RNDeviceInfo(),
new MainReactPackage()
);
}
}
(Thanks to @chirag04 for writing the instructions)
Windows
- Open the solution in Visual Studio for your Windows apps
- right click your in the Explorer and click Add > Existing Project...
- Navigate to
./<app-name>/node_modules/react-native-device-info/windows/RNDeviceInfo
and add RNDeviceInfo.csproj
- this time right click on your React Native Windows app under your solutions directory and click Add > Reference...
- check the
RNDeviceInfo
you just added and press ok - open up
MainReactNativeHost.cs
for your app and edit the file like so:
+ using RNDeviceInfo;
......
protected override List<IReactPackage> Packages => new List<IReactPackage>
{
new MainReactPackage(),
+ new RNDeviceInfoPackage(),
};
(Thanks to @josephan for writing the instructions)
Usage
var DeviceInfo = require('react-native-device-info');
API
getAPILevel()
Gets the API level.
Examples
const apiLevel = DeviceInfo.getAPILevel();
Notes
See API Levels
getApplicationName()
Gets the application name.
Examples
const appName = DeviceInfo.getApplicationName();
getBatteryLevel()
Gets the battery level of the device as a float comprised between 0 and 1.
Examples
DeviceInfo.getBatteryLevel().then((batteryLevel) => {
});
Notes
Returns -1 on the iOS Simulator
getBrand()
Gets the device brand.
Examples
const brand = DeviceInfo.getBrand();
getBuildNumber()
Gets the application build number.
Examples
const buildNumber = DeviceInfo.getBuildNumber();
Notes
There is a type inconsistency: Android return an integer instead of the documented string.
getBundleId()
Gets the application bundle identifier.
Examples
const bundleId = DeviceInfo.getBundleId();
getCarrier()
Gets the carrier name (network operator).
Examples
const carrier = DeviceInfo.getCarrier();
getDeviceCountry()
Gets the device country based on the locale information.
Examples
const deviceCountry = DeviceInfo.getDeviceCountry();
getDeviceId()
Gets the device ID.
Examples
const deviceId = DeviceInfo.getDeviceId();
getDeviceLocale()
Gets the device locale.
Examples
const deviceLocale = DeviceInfo.getDeviceLocale();
getDeviceName()
Gets the device name.
Examples
const deviceName = DeviceInfo.getDeviceName();
Android Permissions
getFirstInstallTime()
Gets the time at which the app was first installed, in milliseconds.
Examples
const firstInstallTime = DeviceInfo.getFirstInstallTime();
getFontScale()
Gets the device font scale.
The font scale is the ratio of the current system font to the "normal" font size, so if normal text is 10pt and the system font is currently 15pt, the font scale would be 1.5
This can be used to determine if accessability settings has been changed for the device; you may want to re-layout certain views if the font scale is significantly larger ( > 2.0 )
Examples
const fontScale = DeviceInfo.getFontScale();
getFreeDiskStorage()
Gets available storage size, in bytes.
Examples
const freeDiskStorage = DeviceInfo.getFreeDiskStorage();
Notes
From developer.android.com:
Return the primary shared/external storage directory.
Note: don't be confused by the word "external" here. This directory can better be thought as
media/shared storage. It is a filesystem that can hold a relatively large amount of data and
that is shared across all applications (does not enforce permissions). Traditionally this is
an SD card, but it may also be implemented as built-in storage in a device that is distinct
from the protected internal storage and can be mounted as a filesystem on a computer.
getIPAddress()
Gets the device current IP address.
Examples
DeviceInfo.getIPAddress().then(ip => {
});
Android Permissions
getInstallReferrer
Gets the referrer string upon application installation.
Examples
const referrer = DeviceInfo.getInstallReferrer();
getInstanceID()
Gets the application instance ID.
Examples
const instanceId = DeviceInfo.getInstanceID();
Notes
See https://developers.google.com/instance-id/
getLastUpdateTime()
Gets the time at which the app was last updated, in milliseconds.
Examples
const lastUpdateTime = DeviceInfo.getLastUpdateTime();
getMACAddress()
Gets the network adapter MAC address.
Examples
DeviceInfo.getMACAddress().then(mac => {
});
Android Permissions
getManufacturer()
Gets the device manufacturer.
Examples
const manufacturer = DeviceInfo.getManufacturer();
getMaxMemory()
Returns the maximum amount of memory that the JVM will attempt to use, in bytes.
Examples
const maxMemory = DeviceInfo.getMaxMemory();
getModel()
Gets the device model.
Examples
const model = DeviceInfo.getModel();
getPhoneNumber()
Gets the device phone number.
Examples
const phoneNumber = DeviceInfo.getPhoneNumber();
Android Permissions
Notes
This can return undefined
in certain cases and should not be relied on. SO entry on the subject.
getReadableVersion()
Gets the application human readable version.
Examples
const readableVersion = DeviceInfo.getReadableVersion();
getSerialNumber()
Gets the device serial number.
Examples
const serialNumber = DeviceInfo.getSerialNumber();
getSystemName()
Gets the device OS name.
Examples
const systemName = DeviceInfo.getSystemName();
getSystemVersion()
Gets the device OS version.
Examples
const systemVersion = DeviceInfo.getSystemVersion();
getTimezone()
Gets the device default timezone.
Examples
const timezone = DeviceInfo.getTimezone();
getTotalDiskCapacity()
Gets full disk storage size, in bytes.
Examples
const storageSize = DeviceInfo.getTotalDiskCapacity();
getTotalMemory()
Gets the device total memory, in bytes.
Examples
const totalMemory = DeviceInfo.getTotalMemory();
getUniqueID()
Gets the device unique ID.
Examples
const uniqueId = DeviceInfo.getUniqueID();
Notes
- iOS: This is
IDFV
so it will change if all apps from the current apps vendor have been previously uninstalled. - android: Prior to Oreo, this id (ANDROID_ID) will always be the same once you set up your phone.
getUserAgent()
Gets the device User Agent.
Examples
const userAgent = DeviceInfo.getUserAgent();
getVersion()
Gets the application version.
Examples
const version = DeviceInfo.getVersion();
is24Hour()
Tells if the user preference is set to 24-hour format.
Examples
const is24Hour = DeviceInfo.is24Hour();
isEmulator()
Tells if the application is running in an emulator.
Examples
const isEmulator = DeviceInfo.isEmulator();
isPinOrFingerprintSet()
Tells if a PIN number or a fingerprint was set for the device.
Examples
DeviceInfo.isPinOrFingerprintSet()(isPinOrFingerprintSet => {
if (!isPinOrFingerprintSet) {
...
}
}
Notes
- Since the device setting for PIN/Fingerprint can be modified while the app is still open, this is available via callback instead of as a constant.
- iOS: Not supported for iOS < 9
isTablet()
Tells if the device is a tablet.
Examples
const isTablet = DeviceInfo.isTablet();
Troubleshooting
When installing or using react-native-device-info
, you may encounter the following problems:
[android] - Unable to merge dex / Multiple dex files / Problems with `com.google.android.gms`
react-native-device-info
uses com.google.android.gms:play-services-gcm
to provide [getInstance()][#getinstance].
This can lead to conflicts when building the Android application.
If you're using a different version of com.google.android.gms:play-services-gcm
in your app, you can define the
googlePlayServicesVersion
gradle variable in your build.gradle
file to tell react-native-device-info
what version
it should require.
If you're using a different library that conflicts with com.google.android.gms:play-services-gcm
, you can simply
ignore this dependency in your gradle file:
compile(project(':react-native-device-info')) {
exclude group: 'com.google.android.gms'
}
[ios] - ld: library not found for -lRNDeviceInfo-tvOS
Seems to be a bug caused by react-native link
. You can manually delete libRNDeviceInfo-tvOS.a
in Xcode -> [Your iOS build target] -> Build Phrases -> Link Binary with Libraries
.
[tests] - Cannot run my test suite when using this library
react-native-device-info
contains native code, and needs to be mocked.
Here's how to do it with jest for example:
// in your package.json:
"jest": {
"setupFiles": [
"./testenv.js"
],
// testenv.js:
jest.mock('react-native-device-info', () => {
return {
getModel: jest.fn(),
};
});
Release Notes
See the CHANGELOG.md.
react-native-web
As a courtesy to developers, this library was made compatible in v0.17.0 with react-native-web by providing an empty polyfill in order to avoid breaking builds.
Only getUserAgent() will return a correct value. All other API methods will return an "empty" value of its documented return type: 0
for numbers, ''
for strings, false
for booleans.