React Native integration (legacy)
You can learn how to integrate the SDK with a React Native project with this guide.
This integration is the legacy version. For the most recent version, see our recommended integration.
Add the -fcxxmodules flag (iOS C++ projects only)
If your project is built with C++ (AppDelegate.mm), do the following:
- Open Xcode Build Settings.
- Navigate to Targets > Build Settings >
Apple Clang - Custom Compiler Flags>Other C++ Flags. - Add the
-fcxx-modulesflag.
Add the Human Module
Android
iOS
- Create a native module called
HumanModule.
- Add the
HumanModuleto theHumanPackageand set thesharedproperty.
Start the SDK
When starting the SDK, keep the following in mind:
- Start the SDK as soon as possible in your app’s flow. We recommend doing so in the
Application’sonCreatefunction in Android or theAppDelegate’sdidFinishLaunchingWithOptionsfunction in iOS. If you don’t do this, then your application may send a URL request to your server before the SDK starts. This means the request will not include HTTP headers that indicate whether the request is legitimate, and as a result:- The Enforcer may block the request instead, and
- The SDK will not be able to present a challenge to the user
- Start the SDK on the main thread.
- Implement the
HumanDelegateto receive events from the SDK.
Below are example implementations for both Android and iOS:
Don’t forget to replace <APP_ID> with your actual Application ID.
Android
iOS
In particular, note the following:
- Initialization: We start the SDK as soon as possible and on the main thread to ensure all URL requests include the necessary HTTP headers.
- Policy configuration: We create a
HSPolicyinstance to configure the SDK’s behavior. In this case, we set theinterceptorTypeproperty toHSAutomaticInterceptorType.NONEto disable the automatic interception feature, which is not supported in React Native. - Start the SDK: We call the
HumanSecurity.start(appId, policy)function of the SDK with:- The
Applicationinstance (Android only). - Your Application ID. If you have multiple IDs, see our guide on how to handle them.
- The
- On iOS, we call the
startObservingfunction on theHumanModule.
Add HTTP headers and handle block response
In your JavaScript code:
- Import the
HumanModule. - Create a new native event emitter.
- Add a listener to the New Headers event.
- Add a listener to the Challenge Result event.
- Add the SDK’s HTTP headers to your URL requests.
- When a request is blocked, send the block response to the SDK.
- Handle the challenge’s result.
Pass the block response
When the HUMAN Enforcer blocks a request, it returns a JSON string in the response body with an HTTP status code of 403, which includes metadata for the SDK. For example:
Your app should pass the whole JSON to the SDK via the HSBotDefender/handleBlockResponse function. Otherwise, the SDK won’t present a challenge to the user.