Integration with React Native

v3.0

The SDK can be integrated into React Native projects.

Start the SDK

The automatic interceptor is not supported in React Native, so any request from the JavaScript code has to be handled manually. Here is an example:

1PXPolicy *policy = [[PXPolicy alloc] init];
2policy.urlRequestInterceptionType = PXPolicyUrlRequestInterceptionTypeNone;
3NSError *error = nil;
4[PerimeterX startWithAppId:@"<APP_ID>" delegate:self policy:policy error:&error];
5if (error != nil) {
6 NSLog(@"failed to start. error: %@", error);
7}

Create Native Module

Create a native module which called PerimeterXModule, as described here

Pass the SDK’s HTTP headers to the JavaScript code

Create a function that pass the HTTP headers from the SDK to the JavaScript code. Here is an example:

1RCT_REMAP_METHOD(getHTTPHeaders,
2 resolver:(RCTPromiseResolveBlock)resolve
3 rejecter:(RCTPromiseRejectBlock)reject) {
4 NSDictionary<NSString *, NSString *> *headers = [PerimeterX headersForURLRequestForAppId:nil];
5 NSData *data = [NSJSONSerialization dataWithJSONObject:headers options:0 error:nil];
6 NSString *json = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
7 resolve(@[json]);
8}

In your JavaScript code, call this function for every URL request to add those HTTP headers. Here is an example:

1const headers = await PerimeterXModule.getHTTPHeaders();
2const obj = JSON.parse(headers);
3const url = 'https://my.request.com'
4const response = await fetch(url, {
5 method: 'GET',
6 headers: obj,
7});

If you wish to reduce the number of times there is a bridge between the JavaScript and native, you can add a listener that will be called when the SDK’s headers were changed. Than, you pass those new headers to the JavaScipt side. You may cache those headers for future requests, but you must make sure to update them.

In the AppDelegate, implement the PerimeterXDelegate/perimeterxHeadersWereUpdated(headers:forAppId:) function. You should pass those headers to your native module.

1- (void)perimeterxHeadersWereUpdatedWithHeaders:(NSDictionary<NSString *,NSString *> *)headers forAppId:(NSString *)appId {
2 [perimeterxModule handleUpdatedHeaders:headers];
3}

In the native module, implement the handleUpdatedHeaders function. You should send the event ("pxNewHeaders") to the JavaScript side.

1- (void)handleUpdatedHeaders:(NSDictionary<NSString *,NSString *> *)headers {
2 NSData *data = [NSJSONSerialization dataWithJSONObject:headers options:0 error:nil];
3 NSString *json = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
4 [self sendEventWithName:@"pxNewHeaders" body:json];
5}

In your JavaScript code, listen to this event.

  1. Import the native module.

    1import {NativeModules, NativeEventEmitter} from 'react-native';
    2import type {Message} from 'react-native/Libraries/LogBox/Data/parseLogBoxLog';
    3const {PerimeterXModule} = NativeModules;
  2. Create new NativeEventEmitter.

    1const pxEventEmitter = new NativeEventEmitter(PerimeterXModule);
  3. Add listener to the event and store the SDK’s headers in variable.

    1var pxHeader = null;
    2
    3const onAddNewHeaders = headers => {
    4 const obj = JSON.parse(headers);
    5 console.log(`[PX] got new px headers from event: ${JSON.stringify(obj)}`);
    6 pxHeader = obj;
    7};
    8
    9const subscriptionPxNewHeaders = pxEventEmitter.addListener(
    10 'PxNewHeaders',
    11 onAddNewHeaders,
    12);
  4. Add those headers to your URL request.

    1const url = 'https://my.request.com'
    2const response = await fetch(url, {
    3 method: 'GET',
    4 headers: pxHeader,
    5});
Always include the SDK's headers

Those SDK’s headers must be included in all your URL requests. Sending requests without them could affect the user experience.

Handle the block response

You have to provide the SDK with the response. After receiving an error in the response, pass the information to SDK:

1const result = await PerimeterXModule.handleResponse(
2 JSON.stringify(json),
3 response.status,
4 url,
5);
6/*
7check the result:
8 'false' - not handled by the SDK
9 'solved' - challenge solved
10 'cancelled' - challenge cancelled
11*/
12if (result === 'solved') {
13 // challenge solved. you may retry the request here
14} else if (result === 'false') {
15 // request finished successfully
16} else if (result === 'cancelled') {
17 // challenge cancelled
18}
Integration with C++

If your project is built with C++ (appdelegate.mm) then you should do the following: 1. Open Xcode Build Settings. 2. Targets -> Build Settings -> Apple Clang - Custom Compiler Flags > Other C++ Flags 3. Add “-fcxx-modules” flag.