Skip to main content

React Native

Calling MPS's APIs from the Javascript side in a React native is no different than calling any other native methods, you only need to bridge the platforms.

note

The following code is an example and not a requirement.
Please follow ReactNative's guidelines

iOS

Start by creating a swift and header files to bridge the framework's swift declarations to Objective-C and ReactNative

MobileProtectionShieldiOS.swift
import Foundation
import MobileProtectionShield

@objc(MobileProtectionShieldiOS)
class MobileProtectionShieldiOS: NSObject {
private lazy var mps: MPS = {
MPS.load()
}()

@objc
static func requiresMainQueueSetup() -> Bool {
return false
}

//MARK: - Secrets (should be modified according to the defined values)

@objc(getExample:rejecter:)
func getExample(_ resolve: RCTPromiseResolveBlock, rejecter reject: RCTPromiseRejectBlock) {
resolve(mps.getExample())
}

//MARK: - MPS Constant methods

@objc(version:rejecter:)
func version(_ resolve: RCTPromiseResolveBlock, rejecter reject: RCTPromiseRejectBlock) {
resolve(MPS.Version)
}

@objc(validationCode:rejecter:)
func validationCode(_ resolve: RCTPromiseResolveBlock, rejecter reject: RCTPromiseRejectBlock) {
resolve(mps.code)
}

@objc(isJailbroken:rejecter:)
func isJailbroken(_ resolve: RCTPromiseResolveBlock, rejecter reject: RCTPromiseRejectBlock) {
resolve(mps.isJailbroken)
}

@objc(isBlockingSimulator:rejecter:)
func isBlockingSimulator(_ resolve: RCTPromiseResolveBlock, rejecter reject: RCTPromiseRejectBlock) {
resolve(mps.isBlockingSimulator)
}

@objc(isBeingReverseEngineered:rejecter:)
func isBeingReverseEngineered(_ resolve: RCTPromiseResolveBlock, rejecter reject: RCTPromiseRejectBlock) {
resolve(mps.isBeingReverseEngineered)
}
}
MobileProtectionShieldiOS.h
#import <Foundation/Foundation.h>
#import "React/RCTBridgeModule.h"

@interface
// Secrets (should be modified according to the defined values)
RCT_EXTERN_METHOD(getExample:(RCTPromiseResolveBlock) resolve rejecter:(RCTPromiseRejectBlock) reject)
// MPS constant methods
RCT_EXTERN_MODULE(MobileProtectionShieldiOS, NSObject)
RCT_EXTERN_METHOD(version:(RCTPromiseResolveBlock) resolve rejecter:(RCTPromiseRejectBlock) reject)
RCT_EXTERN_METHOD(validationCode:(RCTPromiseResolveBlock) resolve rejecter:(RCTPromiseRejectBlock) reject)
RCT_EXTERN_METHOD(isJailbroken:(RCTPromiseResolveBlock) resolve rejecter:(RCTPromiseRejectBlock) reject)
RCT_EXTERN_METHOD(isBlockingSimulator:(RCTPromiseResolveBlock) resolve rejecter:(RCTPromiseRejectBlock) reject)
RCT_EXTERN_METHOD(isBeingReverseEngineered:(RCTPromiseResolveBlock) resolve rejecter:(RCTPromiseRejectBlock) reject)
@end

Android

After importing the library and setting up the project configurations, create the bridging class.

MobileProtectionShieldAndroid.kt
class MobileProtectionShieldAndroid(private val reactContext: ReactApplicationContext) : ReactContextBaseJavaModule(reactContext) {
private var hardening: Hardening? = null

private fun getHardening(): Hardening {
hardening?.let {
return it
}
this.hardening = Hardening.getInstance(reactContext)
return hardening!!
}

override fun getName(): String {
return "MobileProtectionShieldAndroid"
}

// Secrets (should be modified according to the defined values)

@ReactMethod
fun getExample(promise: Promise) {
try {
_ = getHardening()
promise.resolve(Secrets.Example())
} catch (e: Exception) {
promise.reject("ERROR", e)
}
}

// MPS Constant methods

@ReactMethod
fun isRooted(promise: Promise) {
try {
val result = getHardening().isRooted()
promise.resolve(result)
} catch (e: Exception) {
promise.reject("ERROR", e)
}
}

@ReactMethod
fun isEmulator(promise: Promise) {
try {
val result = getHardening().isEmulator()
promise.resolve(result)
} catch (e: Exception) {
promise.reject("ERROR", e)
}
}

@ReactMethod
fun isDebugger(promise: Promise) {
try {
val result = getHardening().isDebugger()
promise.resolve(result)
} catch (e: Exception) {
promise.reject("ERROR", e)
}
}

@ReactMethod
fun isFrida(promise: Promise) {
try {
val result = getHardening().isFrida()
promise.resolve(result)
} catch (e: Exception) {
promise.reject("ERROR", e)
}
}

@ReactMethod
fun isMagisk(promise: Promise) {
try {
val result = getHardening().isMagisk()
promise.resolve(result)
} catch (e: Exception) {
promise.reject("ERROR", e)
}
}

@ReactMethod
fun isKeystoreSigned(promise: Promise) {
try {
val result = getHardening().isKeystoreSigned()
promise.resolve(result)
} catch (e: Exception) {
promise.reject("ERROR", e)
}
}

@ReactMethod
fun isInstallerAllowed(promise: Promise) {
try {
val result = getHardening().isInstallerAllowed()
promise.resolve(result)
} catch (e: Exception) {
promise.reject("ERROR", e)
}
}
}

React

Now that all the bridges to the native APIs are in place we can start using them from the javascript side.

const { MobileProtectionShieldAndroid, MobileProtectionShieldiOS } = NativeModules;

if (Platform.OS === 'android') {
// Secrets
const example = await MobileProtectionShieldAndroid.getExample() as string
// Environment
const isRooted = await MobileProtectionShieldAndroid.isRooted() as boolean,
const isEmulator = await MobileProtectionShieldAndroid.isEmulator() as boolean,
const isDebugger = await MobileProtectionShieldAndroid.isDebugger() as boolean,
const isFrida = await MobileProtectionShieldAndroid.isFrida() as boolean,
const isMagisk = await MobileProtectionShieldAndroid.isMagisk() as boolean,
const isKeystoreSigned = await MobileProtectionShieldAndroid.isKeystoreSigned() as boolean,
const isInstallerAllowed = await MobileProtectionShieldAndroid.isInstallerAllowed() as boolean
}

if (Platform.OS === 'ios') {
// Secrets
const example = await MobileProtectionShieldiOS.getExample() as string
// Environment
const version = await MobileProtectionShieldiOS.version() as string,
const validationCode = await MobileProtectionShieldiOS.validationCode() as string,
const isJailbroken = await MobileProtectionShieldiOS.isJailbroken() as boolean,
const isBeingReverseEngineered = await MobileProtectionShieldiOS.isBeingReverseEngineered() as boolean,
const isBlockingSimulator = await MobileProtectionShieldiOS.isBlockingSimulator() as boolean
}