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:
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:
In your JavaScript code, call this function for every URL request to add those HTTP headers. Here is an example:
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.
In the native module, implement the handleUpdatedHeaders
function. You should send the event ("pxNewHeaders"
) to the JavaScript side.
In your JavaScript code, listen to this event.
-
Import the native module.
-
Create new
NativeEventEmitter
. -
Add listener to the event and store the SDK’s headers in variable.
-
Add those headers to your URL request.
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:
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.