React Native Device Information Integration
This package provides device information for both Android and iOS devices. If you have push notifications enabled then this package can also provide the device token required to use this feature.
Installation
npm install @pod-point/react-native-device-info --save
iOS Setup Guide
- Open up your React Native project in XCode, this is the
.xcodeproj
file in theios
directory of your React Native project. - Open
node_modules/@pod-point/react-native-device-info/ios
in Finder and locate thePPTDeviceInfo.xcodeproj
package. Drag this file into the XCode project navigator. You can keep this in theLibraries
group along with all the other React Native packages. - Click on the root of your project in XCode, then select your project's main target. Click on Build Phases and
double check that the
libPPTDeviceInfo.a
product was added. If not, expand theProducts
group inPPTDeviceInfo.xcodeproj
and drag this file in.
Optional Extras
If you're using push notifications then you'll need to get hold of the device token. This token isn't immediately available to the app and extra configruation is required. If you need the device token, follow these steps:
- Click on the root of your project in XCode, then select your project's main target. Select Build Settings and then
search for Header Search Paths. Add
$(SRCROOT)/../node_modules/@pod-point/react-native-device-info
to the header search path list and make sure that it is set to recursive. - Ensure that your app delegate is correctly registering user notifications. You should have already completed this step if you're already using push notifications.
- In your
didRegisterForRemoteNotificationsWithDeviceToken
delegate, add the following line:[PPTDeviceInfoProvider provideDeviceToken:[deviceToken description]];
. This assumes that your delegate has called the device tokenNSObject
deviceToken
.
Android Setup Guide
- Open up your React Native project in Android Studio, this is the
android
directory in your React Native project. - Expand Gradle Scripts from within the project tree and open
settings.gradle
. Replace the line in the script which statesinclude ':app'
withinclude ':app', ':pptdeviceinfo'
(or append':pptdeviceinfo'
to the end of the include statement if you're already including other modules). - Add the following line to the end of
settings.gradle
:
project(':pptdeviceinfo').projectDir = new File(rootProject.projectDir, '../node_modules/@pod-point/react-native-device-info/android/library')
- Open up your
app
modulebuild.gradle
file and add the following line to the end of your dependancies section:
compile project(path: ':pptdeviceinfo')
- You should now be prompted to run a Gradle project sync so press Sync Now in the gold toolbar that should be visible.
- Open your projects
MainActivity
class and import the following package:
import com.podpoint.pptdeviceinfo.PPTDeviceInfoPackage;
- Find the line in your main activity class which has the following on it -
.addPackage(new MainReactPackage())
, add the following line below:
.addPackage(new PPTDeviceInfoPackage())
- Hit
Ctrl+R
and make sure the app runs!
General Usage
Import the package into react native using the following:
import DeviceInfo from '@pod-point/react-native-device-info';
Unfortunately the iOS and Android APIs differ slightly. This is because iOS and Android devices identify themselves differently; however many of the parameters returned are similar.
iOS Usage
The device info object will return the following properties:
{
appVersion: "1.0",
buildNumber: "1",
bundleId: "com.apple.UselessStocksApp",
deviceId: "iPhone7,2",
model: "iPhone 6",
systemManufacturer: "Apple",
systemName: "iPhone OS",
systemVersion: "9.1"
}
If you've optionally enabled the device token functionality then you can access it using the following callback.
If the transform token parameter is set to true then all whitespace and <>
charachters will be removed from the
token, if it is set to false then the token is returned in it's raw format.
let transformToken = true;
DeviceInfo.deviceToken(transformToken, (token, error) => {
console.log(token, error);
});
Should the device token be unavailable, the error object will provide you some useful information as to why. You may need to request the user to enable push notifications - this isn't something which is in the scope of this package so this functionality isn't available here.
Android Usage
Android device info is returned as an object, there are no callbacks when this package runs on Android hardware:
{
appVersion: "1.0",
buildNumber: 1,
bundleId: "com.google.calculator",
deviceToken: "APA91bFoi3lMMre9G3XzR1LrF4ZT82_15MsMdEICogXSLB8-MrdkRuRQFwNI5u8Dh0cI90ABD3BOKnxkEla8cGdisbDHl5cVIkZah5QUhSAxzx4Roa7b4xy9tvx9iNSYw-eXBYYd8k1XKf8Q_Qq1X9-x-U-Y79vdPq",
model: "Google Nexus 5 - 5.0.0 - API 21 - 1080x1920",
systemManufacturer: "Genymotion",
systemName: "Android",
systemVersion: "5.0",
hardwareId: "11bf04d174814945"
}