Native (recommended implementation)
After you add the SDK to your project, you need to start the SDK and integrate with Bot Defender, Account Defender, or both. You can learn how to do so with this guide.
This guide covers the easiest, recommended implementation for native apps. If you need another implementation method, see our other guides:
Choosing the right implementation
Important notice about iOS
The SDK uses a custom URL protocol in order to manipulate your URL requests. The SDK creates a new URLSession object to load your URL requests. As a result, there are few limitations that you should be aware of before the integration:
- If your implement the
urlSession(_ session: URLSession, didReceive challenge: URLAuthenticationChallenge, completionHandler: @escaping @Sendable (URLSession.AuthChallengeDisposition, URLCredential?) -> Void)in yourURLSessionDelegate, then this function will not be called and the default OS logic will apply in your URL request. - The SDK’s
URLSessionobject is configured withURLSessionConfiguration.default. Any custom configuration on your side will not apply’ - The SDK does not support caching in your URL requests.
Prerequisites
- Admin access to the HUMAN Console, which you’ll need to:
- Retrieve your Application ID. You can find this by navigating to Platform Settings > Applications and copying the ID. If you need to use multiple IDs, see Handle multiple Application IDs.
- Set the token expiration and validity.
- An integrated HUMAN Enforcer that is enabled and in active blocking mode. Refer to our Enforcer guides for more information.
- Android: Requires your app to use OkHttp, which supports Interceptors.
- iOS: Requires your app to use
URLSessionor any third-party library based on it.
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.
- The SDK must intercept URL requests from your app, and your app needs to specify which domains should be intercepted to the SDK. The SDK checks if the URL ends with one of the specified domains. For example, if you set
example.com, the SDK intercepts calls to bothwww.example.comandapi.example.com. If you don’t do this, then all domains will be intercepted instead.
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 early as possible on the main thread to ensure all URL requests include the necessary HTTP headers.
- Policy Configuration: We create an
HSPolicyinstance to configure the SDK’s behavior. In this case:- We set the
HSAutomaticInterceptorPolicy/interceptorTypeproperty toHSAutomaticInterceptorType/intercept. This turns on the Automatic Interception feature, which lets the SDK manipulate URL requests and handle their responses by itself. - We set our server’s domain. This tells the SDK to intercept URL requests only for this domain.
- We set the
- Starting the SDK: We call the
HumanSecurity.start(appId:policy:)function with the following parameters:- The
Applicationinstance (Android only) - Your Application ID. If you have multiple IDs, see our guide on how to handle them.
- The
HSPolicyobject that we configured
- The
Integrate with Bot Defender
Send your requests through the SDK and check for a blocked request
First, import the SDK and configure your application to send requests through the SDK. This is the basic flow:
- The URL request is sent through the SDK.
- The SDK adds its HTTP headers to your URL requests. This is done automatically—there is no need to manually add these headers.
- If Bot Defender blocks the request, the SDK presents a challenge to the user.
- While the SDK is presenting that challenge, it returns a custom error to your request handler.
- Your application checks that error to confirm the request was blocked.
Below are example implementations for both Android and iOS:
Android
iOS
The HTTP client must have the HSInterceptor as the last interceptor in the list.
Handle blocked requests in your app
Your application should handle blocked requests as a failure. However, if the user solves or cancels the challenge presented by the SDK, your application’s interface will be shown again. In those cases, you may need to indicate to the user that they can continue to interact with your application normally. For example, if the user was attempting to access a certain page before the challenge, then when they return to that same page after solving the challenge, you may want to show a message indicating that they can now continue on to that page.
Set custom parameters (optional)
Custom parameters are additional custom values that you can set yourself to enrich the data you receive from HUMAN. You can have up to 10 custom parameters, and they must follow the pattern custom_paramN, where N is a number between 1 and 10. To set custom parameters:
- Android: Use a
HashMap<String, String> - iOS: Use a
Dictionary<String, String>
Call HSBotDefender/setCustomParameters(parameters:forAppId:) only after the HumanSecurity/start(appId:policy:) function has been called.
Below are example implementations for both Android and iOS:
Android
iOS
Integrate with Account Defender
Enable Account Defender in your app
To enable Account Defender, set the UserID of your current logged-in user in the SDK. Below are example implementations for both Android and iOS:
Android
iOS
Notify HUMAN's backend on outgoing URL requests from the app
For Account Defender to protect the user’s account, your app has to provide the SDK with outgoing URL requests. While the SDK’s interceptor is enabled, the SDK collects your outgoing URL requests automatically. You do not need to manually call the SDK’s API to collect these requests.
Set additional data (optional)
Additional data are additional custom values that you can set yourself to enrich the data you receive from HUMAN. Note that these are separate from custom parameters and are unique to Account Defender. Additional data must follow the pattern my_keyN, where N is a number. To set additional data:
- Android: Use a
HashMap<String, String> - iOS: Use a
Dictionary<String, String>
Call the HSAccountDefender/setAdditionalData(parameters:forAppId:) only after the HumanSecurity/start(appId:policy:) function has been called.
Below are example implementations for both Android and iOS: