Skip to main content

Overview

Permutive does not provide an official React Native library, but you can utilise our native Android and iOS SDKs from within your React Native application. This involves creating Android and iOS Native Modules for communicating from JS to our Native SDKs. The full guide from React Native can be found here. The functions in the modules created for both platforms must share equivalent function signatures to allow the JS code to call a single function which will work on both platforms.

Android Native Module

The Android Native Module uses our native Android SDK, the full documentation for this can be found here. It’s recommended to use Android Studio for developing the Android native parts of your React Native application. It provides code syntax feedback, errors and code completion making it much easier to write your Android code. You can download the latest version here. Start by importing the project into Android Studio from your android/app folder.

Add the Permutive Dependency

To start with, add the Permutive dependency to your Android app module in your React Native project. To do this add the dependency to the build.gradle which you can find in the android/app folder in your project.

Create the Permutive Native Module file

Then create a native module for Permutive in the Android app source code, this should be at android/app/src/main/java/<your-app-package>/. In this example the file is called PermutiveModule.kt. This file provides the bridging between the JS functions and the Native Android functions. It extends ReactContextBaseJavaModule. Each function you would like to expose in JS must be annotated with @ReactMethod. The bridging code can only return and receive specific types when interfacing with JS, for full details see the React Native official documentation for Android Native Modules here. The following sample code demonstrates a Native Module which exposes a subset of the Permutive SDK functionality to JS:
PermutiveModule.kt

Register the Native Module

On Android you need to register the Native Module with the Android application to allow the JS code to call it. To do this create a ReactPackage for your application where you can add your new Native Module. First create a file in your android/app/src/main/java/<your-app-package>/ folder, in this example it’s called PermutiveAppPackage.kt. Create a class which extends ReactPackage and override the createNativeModules method and add your Native Module to the list returned by the function. The following sample code demonstrates this:
PermutiveAppPackage.kt
You then need to add this package to the list of packages returned by the ReactNativeHost’s getPackages function. This can be found in the MainApplication file in your android/app/src/main/java/<your-app-package>/ folder. The following sample code shows this:
MainApplication.kt
Your Android native code is now callable from JS! How to do this is explained in a section below.

iOS Native Module

The iOS Native Module sample uses our native iOS SDK, the full documentation for this can be found here. It’s recommended to use Xcode for developing the iOS native parts of your React Native application. It provides code syntax feedback, errors and code completion making it much easier to write your iOS code. You can download the latest version here. Start by importing the iOS part of the React Native project into Xcode. The easiest way to do this is by navigating to your React Native project folder and opening the ios/<your-app-name>.xcworkspace file.

Add the Permutive dependency

Add the Permutive iOS dependency to your project using Swift Package Manager. To do this, go to File -> Add Package Dependencies. Then enter https://github.com/permutive-engineering/permutive-ios-spm in the Search or Enter Package URL bar, on the top right, then select Add Package, select Package Product Permutive_iOS and add to your React Native app Target, then select Add Package.

Create the Native Module files

This guide uses Swift files which interact with the Permutive SDK and Objective-C bridging to connect these to JS. Add a new Swift file to your project in the ios/ folder. This should be the same name as your Android module file if you’ve already created that, for this sample we’ll use ‘PermutiveModule’ again so we create PermutiveModule.swift. Your project should have an Objective-C bridging header file which you can find at ios/<your-app-name>/<your-app-name>-Bridging-Header.h. You need to import the ReactBridgeModule in that file as shown here:
<your-app-name>-Bridging-Header.h
You’ll also need an Objective-C file which is used to expose the Swift methods to JS. Create a new Objective-C file with the same name as your Native Module file in the ios/ folder. In this sample that is PermutiveModule.m. The Swift class needs to be annotated as an Objective-C exposed class and the methods need to be annotated too. Only specific types can be used when receiving parameters and returning from functions for communicating with JS. For full details on this see the official React Native iOS Native Modules documentation here. This sample shows a Swift module with the equivalent functionality to the Android Native Module:
PermutiveModule.swift
To be able to call this code from JS it requires bridging through Objective-C code. To do this within the Objective-C native module file you created before, you import the same RCTBridgeModule and create an RCT_EXTERN_MODULE interface which references your Swift class. Two Objective-C functions are used to expose the Swift methods in this example:
  • RCT_EXTERN_METHOD - Exposes an asynchronous method to JS. It takes an argument in the following form: functionName:(Parameter1Type)parameter1InternalName :(Parameter2Type)parameter2InternalName.
  • RCT_EXTERN__BLOCKING_SYNCHRONOUS_METHOD - Exposes a synchronous method to JS. It takes arguments in the same form as RCT_EXTERN_METHOD.
The following sample shows the Objective-C code used to expose the sample Swift class:
PermutiveModule.m
Your iOS code is now ready to be called from JS!

Calling Native code from JS

To consume your native code from JS you can create a wrapper module around your native module. Create a new JS file in your app/ folder with the same name as your Native Module files. In this sample that is PermutiveModule.js with the following content:
PermutiveModule.js
You can then import this module into your React files and call into the native functions. This sample shows calling into the PermutiveModule in the useEffect function to initialize the Permutive SDK, track a Pageview, set the latest cohorts to some state and also listen for changes:
You can utilise these functions to implement Permutive within your app. If you require more functionality you can add it by creating new functions in the React Native Modules for each platform which call the relevant Permutive SDK functions. Your React Native app is now ready to finish your Permutive integration.