Native (advanced 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 advanced implementation for native apps. If you need another implementation method, see our other guides:
Choosing the right implementation
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.
- Specify which domains the SDK should intercept. If none are specified, all domains will be intercepted. TThe 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
automaticInterceptorPolicy.interceptorTypetoINTERCEPT_WITH_DELAYED_RESPONSEorINTERCEPT_AND_RETRY_REQUESTto enable the Automatic Interception feature. - We specify the domains your app will interact with.
- 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
Add SDK’s HTTP Headers to your URL requests
- Disable the request timeout so the call can wait until the challenge finishes. On iOS, set
timeoutIntervalto0. On Android, set the OkHttp call timeout to0, or the Ktor request timeout to infinite. SetHSAutomaticInterceptorPolicy.urlSessionRequestTimeoutwhen starting the SDK on iOS. That value is the timeout for the request the SDK sends. - Send the URL request through the SDK. On Android, add
HSInterceptoras the last interceptor. The SDK adds its HTTP headers to the request and keeps the token in those headers up to date. Do not cache the headers. - If Bot Defender blocks the request, the SDK presents a challenge and holds the call until the challenge ends.
- When
interceptorTypeisINTERCEPT_AND_RETRY_REQUESTand the challenge is solved, the SDK resends the request once before your handler runs. That retry can also be blocked, and the SDK does not send it again. A canceled challenge is not resent. - Your request handler then receives the result. On Android, check
errorTypeforCHALLENGE_WAS_SOLVEDorCHALLENGE_WAS_CANCELLED. On iOS, checkHSBotDefenderErrorDomainforchallengeWasSolvedorchallengeWasCancelled. After a retry, the handler receives the second response instead of the solved error.
For more on the third challenge outcome, see About the solvedWithError or SOLVED_WITH_ERROR Challenge outcome.
Below are example implementations for both Android and iOS:
Handle blocked requests
When the 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 entire JSON response to the SDK via the HSBotDefender/handleBlockResponse function. If not, the SDK won’t present a challenge to the user. When handling blocked requests:
- Treat blocked requests as a failure. After the challenge is solved:
- You may resend the request
- If the request was triggered by a user’s action, inform the user that they can retry the same action. 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.
- Use the handler callback to log analytics, create logs, and so on.
- (Optional) Use the handler callback to retry the original request when appropriate. Consider the following:
- Callback scope: The callback is called outside the original request’s scope. Ensure your app handles this correctly.
- Challenge cancellation: If the challenge is canceled by the user. Do not retry the request to avoid it being blocked again.
- Retry block possibility: The retry attempt may also be blocked. Do not assume it will succeed.
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
About the solvedWithError or SOLVED_WITH_ERROR Challenge outcome
In some cases, the user may complete the challenge, but the SDK may not complete the pass. This is typically caused by a post-challenge Collector refresh that failed or timed out, or session initialization that never completed. This is a distinct result from a failed or canceled challenge. In this case, the SDK will return:
- Android:
SOLVED_WITH_ERROR - iOS:
solvedWithError
Because the pass could not be confirmed, the next protected request may be blocked again. We recommend you display a connectivity message rather than retrying silently in a loop.
A cancellation reason never changes the cancelled / CANCELLED result and must not be read as a successful pass. It remains useful for telling a user-initiated dismissal apart from a load failure.
What is called where
Delegates and detailed or reason APIs still receive solvedWithError/SOLVED_WITH_ERROR. In 5.x the legacy single-argument handleBlockResponse callback folds to solved/SOLVED. Delayed interceptor bodies fold to challengeWasSolved/CHALLENGE_WAS_SOLVED, while non-delayed intercept returns
requestWasBlocked/REQUEST_WAS_BLOCKED. Use detailedCallback (iOS) or handleBlockResponseWithReason (Android) for the detailed outcome.
You will receive two delegate calls
When solvedWithError / SOLVED_WITH_ERROR occurs and you have implemented botDefenderChallengeSolvedWithError, the SDK calls it and
then also calls botDefenderChallengeSolved. iOS delegate methods are
non-throwing and both calls are issued sequentially. On Android both callbacks run inside one try block, so a throwing solved-with-error implementation skips the legacy callback.
This is one challenge, not two. Do not count two passes and do not trigger two retries.
Back press (Android only)
Dismissing the challenge with the hardware back button produces CANCELLED with the reason USER_DISMISSED_BACK_PRESS. It never produces SOLVED_WITH_ERROR.
This reason is Android only. iOS has no equivalent event.
Kotlin coroutine callers (Android only)
The suspend overload of handleBlockResponse returns the result without the reason. If you need the reason, use handleBlockResponseWithReason.
Existing delegate implementations (Android only)
Android botDefenderChallengeSolvedWithError has a default no-op
implementation, so delegates written before 5.1.0 continue to compile and behave as before. iOS exposes the same method as an optional protocol requirement.
Known limitation
SOLVED_WITH_ERROR was added to an existing result enum in a minor release. As a result, an exhaustive when written against 5.0 may now require a new case. This cannot be corrected within the 5.x line without breaking one group of integrations. It is addressed in the next major version.
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.
Below are example implementations for both Android and iOS:
Android
iOS
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: