To integrate the Mobile SDKs into a hybrid React Native application, there are three main steps:
-
iOS Integration
-
Android Integration
-
Platform Selection Integration
Provide native support for your solution by integrating the iOS SDK into your React Native application. This allows you to use our services without having to create API calls.
To integrate the iOS SDK into React Sative apps, create a wrapper around the iOS SDK and then integrate it in each native project.
There are three ways to integrate the iOS SDK with React Native App:
-
Universal Static Library (libKountDataCollector.a)
Refer to How to Integrate the Device Data Collector into Mobile SDKs using iOS Universal Static Library.
-
XCFramework (KountDataCollector.xcframework)
Refer to How to Integrate the Device Data Collector XCFramework Package into an iOS Application.
-
Kount CocoaPods
Refer to How to Integrate the Device Data Collector CocoaPods Package into an iOS Application.
-
Open the iOS project within your React Native application in Xcode.
-
In Finder, go to the project folder, iOS folder, and then open
.xcworkspace (if pods)/.xcodeproj
.
-
Import
KDataCollector.h
andKountAnalyticsViewController.h
inAppDelegate.h
-
Add the following code in the
Appdelegate.m
file.[[KDataCollector sharedCollector] setDebug:YES]; // TODO Set your Merchant ID [[KDataCollector sharedCollector] setMerchantID:@"900900"]; // TODO Set the location collection configuration [[KDataCollector sharedCollector]setLocationCollectorConfig:KLocationCollectorConfigRequestPermission]; // For a released app, you'll want to set this to KEnvironmentProduction [[KDataCollector sharedCollector] setEnvironment:KEnvironmentTest]; [[KountAnalyticsViewController sharedInstance] setEnvironmentForAnalytics: [KDataCollector.sharedCollector environment]];
-
Create a bridging-header file or import the SDK directly.
-
Bridging-header file
Create a new file called
KountBridgeModule.h
and add the following code.#import <React/RCTBridgeModule.h> #import "KDataCollector.h" #import "KountAnalyticsViewController.h" @interface KountBridgeModule: NSObject <RCTBridgeModule> -(void) collect; @end
Create the implementation file
KountBridgeModule.m
in the same folder as the.h
file and add the following code.#import "KountBridgeModule.h" @implementation KountBridgeModule RCT_EXPORT_MODULE(KountBridgeModule); RCT_EXPORT_METHOD(collect){ dispatch_async(dispatch_get_main_queue(), ^{ NSString *sessionID = nil; // By default set analyticsSwitch to false [[KountAnalyticsViewController sharedInstance] collect:sessionID analyticsSwitch:false completion:^(NSString * _Nonnull sessionID, BOOL success, NSError * _Nullable error) { if(success) { NSLog(@"Collection Successful"); } else { if (error != nil) { NSLog(@"Collection failed with error:%@",error.description); } else { NSLog(@"Collection failed without error"); } } }]; }); } RCT_EXPORT_BLOCKING_SYNCHRONOUS_METHOD(getAppID) { NSString *sessionID = [KountAnalyticsViewController getAppSessionID]; return sessionID; } @end
-
Import the SDK
Import the SDK directly into the project file.
#import <React/RCTBridgeModule.h> @import KountDataCollector; @interface KountBridgeModule: NSObject <RCTBridgeModule> -(void) collect; @end
-
You can integrate the SDK by creating a bridging header or directly importing the SDK into the Swift file.
Create a bridging header
-
Import
KDataCollector.h
andKountAnalyticsViewController.h
in<ProjectName>-Bridging-Header.h
. -
Add the following code in the
Appdelegate.swift
file.// Set the location collection configuration KDataCollector.shared().locationCollectorConfig = KLocationCollectorConfig.requestPermission KDataCollector.shared().debug = true // Set your Merchant ID KDataCollector.shared().merchantID = "900900" // For a released app, you'll want to set this to KEnvironmentProduction KDataCollector.shared().environment = KEnvironment.test KountAnalyticsViewController().setEnvironmentForAnalytics(KDataCollector.shared().environment)
-
Create a new file called
KountBridgeModule.swift
and add the following code.import UIKit @objc(KountBridgeModule) class KountBridgeModule: NSObject { @objc func collect() { DispatchQueue.main.async { KountAnalyticsViewController().collect(KountAnalyticsViewController.getAppSessionID(), analyticsSwitch: false) { (sessionID, success, error) in if (success) { print("IDK",KountAnalyticsViewController.getAppSessionID()) print("Collection Successful") } else { if((error) != nil) { print("Collection failed with error",error?.localizedDescription as Any) } else { print("Collection failed without error") } } } }
-
Create a new file called
KountBridgeModule.m
and add the following code to make native code access available to.js
files.#import <Foundation/Foundation.h> #import "React/RCTBridgeModule.h" @interface RCT_EXTERN_MODULE(KountBridgeModule, NSObject) RCT_EXTERN_METHOD(collect) + (BOOL)requiresMainQueueSetup { return YES; } RCT_EXPORT_BLOCKING_SYNCHRONOUS_METHOD(getAppID) { NSString *sessionID = [KountAnalyticsViewController getAppSessionID]; return sessionID; } @end
Import the SDK
Import the SDK into the AppDelegate Swift file by using only one of the following methods:
-
Add the SDK import to the file.
import KountDataCollector
-
Add the following code into your
<ProjectName>-bridiging-header.h
file.#import <React/RCTBridgeModule.h> @import KountDataCollector; @interface KountBridgeModule: NSObject <RCTBridgeModule> -(void) collect; @end
Integrate the Android SDK into your React Native application to collect additional end-user information. This assists in detecting suspicious devices and preventing fraud.
You have two project integration options. Choose the one that best fits your project. We recommend using the Gradle option if you do not have a preference.
-
Gradle
Refer to How to Integrate the Device Data Collector into Mobile Apps using Android Gradle.
-
Android Library JAR
Refer to How to Integrate the Device Data Collector into Android Applications using the Library JAR.
Note
If android:allowbackup attribute
is false in your <application> tag, add tools:replace="android:allowBackup"
to your application tag.
-
In the main activity of your application, initialize the collector and assign the <MERCHANT_ID> you received. Optionally, you can provide a sessionID if you need to generate your own. If not, the SDK will generate one for you. Send this sessionID back to your severs in order to make calls to the API.
Add the following code in your MainActivity:
import com.kount.api.analytics.AnalyticsCollector; import com.kount.api.analytics.AnalyticsConstants; @Override protected void onCreate(Bundle savedInstanceState) { ... // REQUIRED SECTION AnalyticsCollector.setMerchantId("999999"); // END REQUIRED SECTION AnalyticsConstants.collectDeviceDataForSession=true //For production need to add AnalyticsCollector.ENVIRONMENT_PRODUCTION AnalyticsCollector.setEnvironment(AnalyticsCollector.ENVIRONMENT_TEST); // OPTIONAL SESSION_ID SECTION final String deviceSessionID UUID.randomUUID().toString(); AnalyticsCollector.setSessionId(deviceSessionID); // END OPTIONAL SESSION_ID SECTION ... }
-
To support Location Collection in Android API 23 and later, add the following permissions to your main activity, onCreate method:
if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) { if (ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.ACCESS_FINE_LOCATION)) { ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, AnalyticsCollector.REQUEST_PERMISSION_LOCATION); } else { ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, AnalyticsCollector.REQUEST_PERMISSION_LOCATION); } }
-
Create the
KountModule.java
file inside of theandroid/app/src/main/java/com/your-app-name/
folder. This file contains your native module Java class.Add the following code:
import androidx.annotation.NonNull; import com.facebook.react.bridge.ReactApplicationContext; import com.facebook.react.bridge.ReactContextBaseJavaModule; import com.facebook.react.bridge.ReactMethod; import com.kount.api.analytics.AnalyticsCollector; public class KountModule extends ReactContextBaseJavaModule { private static ReactApplicationContext reactContext; KountModule(ReactApplicationContext context) { super(context); reactContext = context; } @ReactMethod public void collect() { AnalyticsCollector.collectDeviceDataForSession(reactContext,(sessionId)-> { Log.d("TAG", "Client success completed with sessionId "+sessionId); Log.d("TAG", "DDC status is " +AnalyticsCollector.getCollectionStatus()); callback.invoke(sessionId,AnalyticsCollector.getCollectionStatus().name()); return null; },(sessionId, error)-> { Log.d("TAG", "client failed with sessionId $error, $sessionId"); Log.d("TAG", "DDC status is " +AnalyticsCollector.getCollectionStatus()); callback.invoke(sessionId,AnalyticsCollector.getCollectionStatus().name()); return null; }); } @NonNull @Override public String getName() { return "KOUNT_MODULE"; } }
-
Register the Module: To add your Native Module to ReactPackage, first create a new Java Class named
KountModulePackage.java
that implements ReactPackage inside theandroid/app/src/main/java/com/your-app-name/
folder:Then add the following content:
import androidx.annotation.NonNull; import com.facebook.react.ReactPackage; import com.facebook.react.bridge.NativeModule; import com.facebook.react.bridge.ReactApplicationContext; import com.facebook.react.uimanager.ViewManager; import java.util.ArrayList; import java.util.Collections; import java.util.List; public class KountModulePackage implements ReactPackage { @NonNull @Override public List<NativeModule> createNativeModules(@NonNull ReactApplicationContext reactContext) { List<NativeModule> modules = new ArrayList<>(); modules.add(new KountModule(reactContext)); return modules; } @NonNull @Override public List<ViewManager> createViewManagers(@NonNull ReactApplicationContext reactContext) { return Collections.emptyList(); } }
-
Open your
MainApplication.java
file in the following path:android/app/src/main/java/com/your-app-name/MainApplication.java
and locate theReactNativeHost’s getPackages()
method and add your package to the packages list.@Override protected List<ReactPackage> getPackages() { @SuppressWarnings("UnnecessaryLocalVariable") List<ReactPackage> packages = new PackageList(this).getPackages(); packages.add(new KountModulePackage()); return packages; }
-
Create a new JavaScript file named
KountModule.js
in your project root folder and add the following code.import { NativeModules } from "react-native"; const {KOUNT_MODULE}=NativeModules; export default KOUNT_MODULE;
Once both the Android and iOS portions of the SDK are set up, you can add the component to the React Native App.
-
In the React Native application, go to
App.js
and add the following code.import React from 'react'; import { Text, View, NativeModules, Platform } from 'react-native'; import KOUNT_MODULE from './KountModule'; import { Button } from 'react-native'; import { Component } from 'react'; const {KountBridgeModule} = NativeModules;
-
Review these examples for how to call the methods based on platforms. These methods are the onPress events of buttons.
export default class App extends Component { /Platform specific code onPressCollect = () => { if (Platform.OS == 'ios') { console.log("Started"); KountBridgeModule.collect(); this.sessionID = KountBridgeModule.getAppID(); } else if (Platform.OS == 'android') { KOUNT_MODULE.collect(); } } render() { return ( <View style={{paddingTop: 50}}> <Button title="Collect" onPress={ this.onPressCollect} /> </View> ) } }
-
Run the React Native app.
It should capture the data points for the Device Data Collector in both iOS and Android platforms.