AppIntegrityModuleApi

class AppIntegrityModuleApi

Entry point to the app integrity features of Unblu: app update checks, device security checks, App Attest based device integrity verification, screen capture protection and device binding.

Access it through the shared AppIntegrityModuleApi.instance.

  • Returns a DeviceBindingApi that talks to the Unblu server over REST. Call once and reuse.

    Pass the same UnbluClientConfiguration you will later hand to UnbluClient. Building the configuration does not initialize the SDK, so this can run first. The binding reads everything it needs from it — unbluBaseUrl, entryPath, preferencesStorage (so it reuses / persists the shared device id), customCookies (always sent), and authenticationChallengeDelegate (+ the PKCS#12 the host configured) for mutual TLS (mTLS) — which keeps it consistent with the initialized SDK.

    Declaration

    Swift

    func getDeviceBindingApi(clientConfiguration: UnbluClientConfiguration, username: String, password: String, restPath: String = "/rest/v4") -> DeviceBindingApi

    Parameters

    clientConfiguration

    The configuration you will reuse for UnbluClient. Single source of truth for server URL, entry path, preferences storage, custom cookies and TLS handling.

    username

    Username for the dedicated device-binding REST login.

    password

    Password for username.

    restPath

    REST API prefix. Defaults to /rest/v4.

  • The UserDefaults key under which the App Attest key ID is stored.

    Declaration

    Swift

    static let keyIdKey: String
  • The shared instance.

    Declaration

    Swift

    static let instance: AppIntegrityModuleApi
  • Checks the App Store for an available update and compares it with the current app version.

    Workflow:

    1. Retrieves the current app version and bundle identifier from the app’s info dictionary.
    2. Constructs the App Store lookup URL using the bundle identifier.
    3. Makes a network request to fetch the app details from the App Store.
    4. Parses the response to extract the latest available version and compare it with the current version.
    5. Invokes the completion handler with the update status and relevant App Store details.

    Declaration

    Swift

    func checkForAppUpdate(completion: @escaping (Bool, String?, String?) -> Void)

    Parameters

    completion

    A closure invoked upon completion of the update check.

  • Opens the App Store page for the application to prompt the user to update.

    Workflow:

    1. Constructs the App Store URL using the provided app ID.
    2. Attempts to open the URL in the App Store app.
    3. Invokes the completion handler with the success status of opening the App Store.

    Declaration

    Swift

    func enforceAppUpdate(appID: String, completion: @escaping (Bool) -> Void)

    Parameters

    appID

    The unique identifier of the app in the App Store.

    completion

    A closure invoked with a Bool indicating whether the App Store opened successfully.

  • Checks the security policies of the device, including passcode and biometric authentication.

    • Determines if a passcode or biometric authentication is enabled on the device.
    • Identifies the type of biometric authentication available (Face ID, Touch ID, or none).

    Declaration

    Swift

    func checkDeviceSecurityPolicy() -> (isPasscodeEnabled: Bool, isBiometricEnabled: Bool, biometricType: LABiometryType?)

    Return Value

    A tuple containing:

    • isPasscodeEnabled (Bool): true if either passcode or biometric authentication is enabled.
    • isBiometricEnabled (Bool): true if biometric authentication (Face ID or Touch ID) is enabled.
    • biometricType (LABiometryType?): The type of biometric authentication available on the device (faceID, touchID, or none).
  • Checks whether the app is running in an emulator (simulator) or under a debugger.

    • Detects if the app is being traced (debugged).
    • Detects if the app is running in an emulator.

    Declaration

    Swift

    func isRunningInEmulatorOrDebugger() -> Bool

    Return Value

    true if the app is running in an emulator or is being debugged, otherwise false.

  • Enables background mode overlay to obscure the app’s content when sent to the background.

    Declaration

    Swift

    func enableBackgroundModeOverlay()
    • Disables the background mode overlay and removes observers to prevent memory leaks.

    Declaration

    Swift

    func disableBackgroundModeOverlay()
  • Asks Apple to attest to the validity of a generated cryptographic key.

    • challenge: A Data object containing a nonce.
    • completion: A closure invoked upon completion of the assertion generation.
    • The first Data? is a statement from Apple about the validity of the key associated with keyId.
    • The second Data? is the key ID as Data.

    Declaration

    Swift

    func attestKey(challenge: String, completion: @escaping (Data?, Data?) -> Void)

    Parameters

    challenge

    A Data object containing a nonce.

    completion

    A closure invoked upon completion of the assertion generation.

  • Generates an assertion to verify the legitimacy of an instance of your app running on a device.

    This function should be called:

    • After attestKey to obtain the keyId.
    • After UnbluClient.getChallenge to obtain the nonce.

    The result should be passed to UnbluClient.verifyAssertion for further validation.

    Declaration

    Swift

    func generateAssertion(keyId: String, challenge: Data?, completion: @escaping (Data?) -> Void)

    Parameters

    keyId

    A unique identifier for the attestation key that was created or provided during app attestation.

    challenge

    A Data object containing a nonce used to generate the assertion.

    completion

    A closure invoked upon completion of the assertion generation.

  • Generates a token that identifies the current device.

    The app you use to generate the token must be linked to your developer account. Otherwise, the token generation request will fail.

    The result should be passed to UnbluClient.verifyDeviceToken for further validation.

    Declaration

    Swift

    func generateDeviceToken(completion: @escaping (String?) -> Void)

    Parameters

    completion

    A string containing a base64-encoded ephemeral token that uniquely identifies the current device.

  • Verify the device token generated by generateDeviceToken using the provided agent client.

    Declaration

    Swift

    func verifyDeviceToken(unbluAgent: any UnbluAgentClient, token: String, completion: @escaping (Bool) -> Void)

    Parameters

    unbluAgent

    The agent client used to call backend verification endpoints.

    token

    The device token to verify.

    completion

    A closure that is called with true if the token is valid.

  • Retrieves a cryptographic nonce (challenge) using the provided agent client.

    Declaration

    Swift

    func getChallenge(unbluAgent: any UnbluAgentClient, completion: @escaping (String) -> Void)

    Parameters

    unbluAgent

    The agent client used to request the nonce.

    completion

    A closure that is called with the nonce challenge String.

  • Verifies an attestation object using the provided challenge and key identifier.

    Declaration

    Swift

    func verifyAttestation(unbluAgent: any UnbluAgentClient, attestationObject: Data, challenge: Data, keyId: Data, completion: @escaping (Bool) -> Void)

    Parameters

    unbluAgent

    The agent client used to call backend verification endpoints.

    attestationObject

    The attestation Data to verify.

    challenge

    The original challenge Data used during attestation.

    keyId

    The key identifier Data associated with the attestation.

    completion

    A closure that is called with true if the attestation is valid.

  • Verifies an assertion object using the provided challenge and key identifier.

    Declaration

    Swift

    func verifyAssertion(unbluAgent: any UnbluAgentClient, assertionObject: Data, challenge: Data, keyId: Data, completion: @escaping (Bool) -> Void)

    Parameters

    unbluAgent

    The agent client used to call backend verification endpoints.

    assertionObject

    The assertion Data to verify.

    challenge

    The original challenge Data used during assertion.

    keyId

    The key identifier Data associated with the assertion.

    completion

    A closure that is called with true if the assertion is valid.

  • Callbacks required for verifying device integrity with your backend.

    See more

    Declaration

    Swift

    struct DeviceIntegrityVerifier
  • Shared verifier used by handleDeviceIntegrityChecks(timeout:).

    Declaration

    Swift

    static var integrityVerifier: AppIntegrityModuleApi.DeviceIntegrityVerifier?
  • Runs the device integrity flow using the shared verifier and blocks until it completes or times out.

    Declaration

    Swift

    static func handleDeviceIntegrityChecks(timeout: TimeInterval = 20) -> Bool
  • Runs the device integrity flow with the verifier provided and blocks until it completes or times out.

    Declaration

    Swift

    static func handleDeviceIntegrityChecks(verifier: AppIntegrityModuleApi.DeviceIntegrityVerifier, timeout: TimeInterval = 20) -> Bool
  • Runs the device integrity flow asynchronously and calls completion with the result.

    Example:

    let unbluAgent = Unblu.createAgentClient(withConfiguration: condig)
    let api = AppIntegrityModuleApi.instance
    
    let verifier = AppIntegrityModuleApi.DeviceIntegrityVerifier(
        verifyDeviceToken: { token, completion in
            api.verifyDeviceToken(unbluAgent: unbluAgent, token: token, completion: completion)
        },
        getChallenge: { completion in
            api.getChallenge(unbluAgent: unbluAgent, completion: completion)
        },
        verifyAttestation: { attestation, challenge, keyId, completion in
            api.verifyAttestation(
                unbluAgent: unbluAgent,
                attestationObject: attestation,
                challenge: challenge,
                keyId: keyId,
                completion: completion
            )
        },
        verifyAssertion: { assertion, challenge, keyId, completion in
            api.verifyAssertion(
                unbluAgent: unbluAgent,
                assertionObject: assertion,
                challenge: challenge,
                keyId: keyId,
                completion: completion
            )
        }
    )
    
    AppIntegrityModuleApi.handleDeviceIntegrityChecks(verifier: verifier) { isOk in
    }
    

    Declaration

    Swift

    static func handleDeviceIntegrityChecks(verifier: AppIntegrityModuleApi.DeviceIntegrityVerifier, completion: @escaping (Bool) -> Void)
  • Runs the device integrity flow using an UnbluAgentClient.

    Steps: 1) Generate a device token and verify it with the backend. 2) Fetch a challenge from the backend. 3) If a keyId exists, generate an assertion and verify it. 4) Otherwise, attest a new key, verify the attestation, then generate and verify the assertion.

    Declaration

    Swift

    static func handleDeviceIntegrityChecks(unbluAgent: any UnbluAgentClient, completion: @escaping (Bool) -> Void)

    Parameters

    unbluAgent

    The agent client used to call backend verification endpoints.

    completion

    Called with true if all verification steps succeed.

  • Allows checking if the device identifier needs to be generated, as it may not be present in the secure store.

    Declaration

    Swift

    var isFirstLaunch: Bool { get }
  • Retrieves the unique device identifier stored in the system. This identifier is generated and stored if it doesn’t already exist. Even if the app is reinstalled, this identifier will remain the same.

    Declaration

    Swift

    func getDeviceIdentifier() -> String
  • Generates a new RSA key pair if none exists in the Keychain.

    Declaration

    Swift

    func generateKeyPair() -> String?
  • Signs a given challenge string using the stored private key.

    Declaration

    Swift

    func signBindingChallenge(challenge: String) -> String?
  • Deletes both private and public keys from the Keychain. After deleting, you can generate a new key pair to re-bind the app.

    Declaration

    Swift

    func resetKeyPair() -> Bool
  • Returns the existing device-binding public key from the Keychain, encoded as base64 X.509 SubjectPublicKeyInfo. This is the key the device registers with the server during device binding.

    The returned value matches the encoding produced by generateKeyPair(). Returns nil if no key pair is currently stored.

    Declaration

    Swift

    func encodedExistingPublicKey() -> String?
  • Overlays a secure text field on the main window to disable screen capture and recording.

    Declaration

    Swift

    func disableMainWindowCaptureAndRecording()
  • Presents a full-screen SwiftUI QR scanner over the current key window’s topmost view controller. The scanner auto-dismisses on first decoded QR code or when the user taps Cancel.

    Declaration

    Swift

    func scanQRCode(hint: String = "Align the QR code within the frame", cancelTitle: String = "Cancel", success: @escaping (String) -> Void, fail: @escaping (String) -> Void)

    Parameters

    hint

    Overlay text shown below the aim frame. Pass a localized string; defaults to English. Pass an empty string to hide it.

    cancelTitle

    Title of the cancel button. Pass a localized string; defaults to English.

    success

    Called on the main thread with the raw QR payload string.

    fail

    Called on the main thread with a description of why the scan didn’t yield a result (camera denied/unavailable, cancelled by user, no view controller available to present the scanner).