Skip to navigation

visionOS integration (Sheet)

Learn how to integrate the HUMAN Mobile SDK to your visionOS app.

This guide is relevant for the current Mobile SDK. While we highly recommend using the latest version, if you are using a legacy integration, refer to the iOS or Android documentation.

You can integrate the SDK with your visionOS app to present the HUMAN Challenge in a sheet over the window the request left from. The sheet prevents the user from continuing to interact with that window until the challenge is solved.

Add the sheet integration code to every view that sends requests that might be blocked. You can learn how to integrate with this article.

Some features that are supported in iOS, including the Doctor App, Automatic Interceptor, and Hybrid App support, are not available in the SDK for visionOS.

Start the SDK

When starting the SDK, keep the following in mind:

  1. Start the SDK as soon as possible in your app’s flow. We recommend doing so in the App’s init function in visionOS. 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
  2. Start the SDK on the main thread.

Below is an example implementation for visionOS:

Don’t forget to replace <APP_ID> with your actual Application ID.

import UIKit
import HUMAN
@main
struct MyApp: App {
init() {
startHumanSDK()
}
func startHumanSDK() {
do {
let policy = HSPolicy()
try HumanSecurity.start(appId: "<APP_ID>", policy: policy)
}
catch {
print("Error: \(error)")
}
}
}

In particular, note the following:

  1. Initialization: We start the SDK as early as possible on the main thread to ensure all URL requests include the necessary HTTP headers.
  2. Policy Configuration: We create an HSPolicy instance to configure the SDK’s behavior.
  3. Starting the SDK: We call the start function with the following parameters:
    • Your Application ID. If you have multiple IDs, see our guide on how to handle them.
    • The HSPolicy object that we configured

Integrate with Bot Defender

1

Send your requests through the SDK and check for a blocked request

  1. Your application adds the SDK’s HTTP headers to the URL request. Include them on every request, and do not cache them. They contain a token with an expiration date, and the SDK keeps that token up to date.
  2. Add HSChallengeView as the first component in the body of each view that can send a blocked request. It presents the challenge.
  3. Call HSChallengeModelViewHelper.registerError(error:uuid:callback:) for every block error, and provide a unique identifier for that view. You may provide a callback that is called when the challenge is solved or canceled.
  4. Call HSChallengeModelViewHelper.unregister(uuid:) when the view disappears. This releases related resources and prevents a memory leak.

Below is an example implementation for visionOS:

import SwiftUI
import HUMAN
struct ContentView: View {
@State private var model = ViewModel()
var body: some View {
VStack {
HSChallengeView(uuid: model.challengeUUID)
HStack {
Button("Send request") {
model.performRequest()
}
.padding()
}
}
.onDisappear() {
HSChallengeModelViewHelper.unregister(uuid: model.challengeUUID)
}
}
}

Let’s talk about what we have in the code here:

  1. We add the HSChallengeView to the ContentView’s body.
  2. We call the HSChallengeModelViewHelper/unregister(uuid:) function when the ContentView is disappearing.
2

Handle blocked requests in your app

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:

{
"vid": "928d7ab3-9cf1-11ee-a624-b802520f369f",
"uuid": "fd01e6d6-9cf2-11ee-808c-acde48001122",
"page": "...",
"appId": "PXj9y4Q8Em",
"action": "captcha",
"collectorUrl": "https://collector-pxj9y4q8em.perimeterx.net"
}

This metadata is included in the block error returned by HumanSecurity/blockError(response:data:forAppId:). Pass that error to the SDK with HSChallengeModelViewHelper/registerError(error:uuid:callback:) and the view’s UUID. If you do not, the SDK will not present a challenge to the user. When handling blocked requests:

  • Treat blocked requests as a failure. Other windows in your app stay visible while the challenge is presented. If the request was triggered by a user’s action, make it clear that the user may try the same action again.
  • 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 out of the original request’s scope. Ensure your app handles this case correctly.
    • Challenge cancellation: The challenge could be 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.
3

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, call the HSBotDefender/setCustomParameters(parameters:forAppId:) only after the HumanSecurity/start(appId:policy:) function has been called.

Below is an example implementation for visionOS.

import HUMAN
func setCustomParametersForBotDefender() {
do {
var customParameters = [String: String]()
customParameters["custom_param1"] = "hello"
customParameters["custom_param2"] = "world"
try HumanSecurity.BD.setCustomParameters(parameters: customParameters, forAppId: "<APP_ID>")
}
catch {
print("Error: \(error)")
}
}

Integrate with Account Defender

1

Enable Account Defender in your app

To enable Account Defender, set the UserID to your current logged-in user in the SDK:

import HUMAN
func onUserLoggedIn(userID: String) {
do {
try HumanSecurity.AD.setUserId(userId: userID, forAppId: "<APP_ID>")
}
catch {
print("Error: \(error)")
}
}
2

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. Call HSAccountDefender/registerOutgoingUrlRequest(url:forAppId:) before you send the URL request.

import HUMAN
func sendUrlRequest(url: URL) {
do {
try HumanSecurity.AD.registerOutgoingUrlRequest(url: url.absoluteString, forAppId: "<APP_ID>")
}
catch {
print("Error: \(error)")
}
// Send the request...
}
3

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, use a dictionary.

Call the HSAccountDefender/setAdditionalData(parameters:forAppId:) only after the HumanSecurity/start(appId:policy:) function was already called.

import HUMAN
func setAdditionalDataForAccountDefender() {
do {
var additionalData = [String: String]()
additionalData["my_key1"] = "hello"
additionalData["my_key2"] = "world"
try HumanSecurity.AD.setAdditionalData(parameters: additionalData, forAppId: "<APP_ID>")
}
catch {
print("Error: \(error)")
}
}