- React Native 0.49.5
- Xcode 9.3 / Swift 4.1
- Android Studio 3.0+
npm install react-native-predict-io --save
- Follow official Cocoapods installation instructions
- Run
pod init
inside theios
directory to initialize Cocoapods
-
In
ios/Podfile
, add the following line above# Add new pods below this line
:eval(File.read('../node_modules/react-native-predict-io/ios/Podfile.template'))
-
Run
pod install
in theios
directory to install
- Follow the 'Configure Your Project' section of the iOS documentation to make sure your app is setup correctly to be able to receive background location updates
React Native doesn't correctly install the native predict.io SDK so we need to fix some minor issues with the iOS project.
- Open your
.xcworkspace
from theios
directory of your app, e.g. Example.xcworkspace - Inside the
Libraries
group in the project explorer, select and removeRNPredictIO
- Add
import io.predict.android.sdk.react.RNPredictIOPackage;
to the imports at the top of the file - Add
new RNPredictIOPackage()
to the list returned by thegetPackages()
method
include ':react-native-predict-io'
project(':react-native-predict-io').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-predict-io/android')
compile project(':react-native-predict-io')
-
In
android/app/src/main/AndroidManifest.xml
add the following permissions tags inside<application>
:<uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" /> <uses-permission android:name="com.google.android.gms.permission.ACTIVITY_RECOGNITION" /> <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" /> <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
-
Add the followiing to the top of the file:
buildscript { ext.kotlin_version = '1.2.40' repositories { jcenter() } dependencies { classpath 'com.android.tools.build:gradle:2.2.2' classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" } } apply plugin: 'kotlin-android'
-
Inside your
MainApplication.java
file inside theandroid
directory of your app:-
Find the
onCreate
method and add the following line to it:PredictIo.init(this)
-
import PredictIO from 'react-native-predict-io';
import {
NativeEventEmitter,
DeviceEventEmitter,
}
const PREDICT_IO_API_KEY = "<YOUR API KEY>";
async function startPredictIOSDK() {
if (Platform.OS === "android") {
const PermissionsAndroid = require('PermissionsAndroid');
const status = await PermissionsAndroid.request(
PermissionsAndroid.PERMISSIONS.ACCESS_FINE_LOCATION,
);
console.log("Android Location Permission => " + status)
}
else if (Platform.OS === "ios") {
const status = await navigator.geolocation.requestAuthorization()
console.log("iOS Location Permission => " + status)
}
PredictIO.start(PREDICT_IO_API_KEY, "highPower", (status) => {
console.log("PredictIO Error => " + status);
switch (status) {
case "success":
console.log("SUCCESS => PredictIO SDK started successfully!");
break;
case "invalidKey":
console.error("ERROR => PredictIO SDK API key is not valid!");
break;
case "killSwitch":
console.error("ERROR => PredictIO SDK kill switch has been activated!");
break;
case "wifiDisabled":
console.warn("WARNING => Wifi is disabled, negatively impacting location quality.");
break;
case "locationPermission":
console.error("ERROR => Location permission has not been granted (Android)");
break;
case "locationPermissionNotDetermined":
console.error("ERROR => Location permission: not yet determined (iOS)");
break;
case "locationPermissionWhenInUse":
console.error("ERROR => Location permission: when in use (iOS)");
break;
case "locationPermissionRestricted":
console.error("ERROR => Location permission: restricted (iOS)");
break;
case "locationPermissionDenied":
console.error("ERROR => Location permission: denied (iOS)");
break;
}
});
}
startPredictIOSDK();
// Listen for events
function handlePredictIOEvent(event) {
alert(`${event.type}\n${event.timestamp}\n${event.coordinate.latitude},${event.coordinate.longitude}`);
}
// Android Subscription
DeviceEventEmitter.addListener('PredictIOTripEvent', handlePredictIOEvent);
// iOS Subscription
const predictIOEventEmitter = new NativeEventEmitter(PredictIO);
const predictIOEventSubscription = predictIOEventEmitter.addListener('PredictIOTripEvent', handlePredictIOEvent);