iOS SDK

Add Sanad to your iOS app.

The iOS SDK opens a Sanad session from your app, reads the device's signals in the background and sends them to Lean, so your backend can read the session's assessment. It works the same way as the Android SDK: the same session rules and the same diagnostics codes.

📘

Prerequisites

Before you add the SDK, complete the setup in Getting started:

  • Ask your Lean representative to enable Sanad for your application.
  • Get your SDK key from the Application Dashboard.
  • Authentication: the access token your backend uses to read assessments.

This page covers only the SDK integration in your app.

1. Add the package

The SDK is distributed through Swift Package Manager. In Xcode, go to File > Add Package Dependencies… and use the URL below:

https://github.com/leantechnologies/lean-device-ios

Or add it to your Package.swift:

dependencies: [
    .package(url: "https://github.com/leantechnologies/lean-device-ios", exact: "1.0.0"),
],
targets: [
    .target(
        name: "YourApp",
        dependencies: [.product(name: "LeanDevice", package: "lean-device-ios")]
    ),
]

You can then import it into your Swift files:

import LeanDevice

2. Info.plist entries

The SDK declares no Info.plist keys and no entitlements of its own. Add these to your app to get the related signals. Without them, the signal is reported as unavailable, and nothing breaks.

KeyWhat it adds
NSLocationWhenInUseUsageDescriptionNeeded for any location signal, and for the SDK's location prompt (allowPrompts)
LSApplicationQueriesSchemes with cydia, sileo, zbra, filza and undecimusLets the SDK check whether known jailbreak apps are installed
NSFaceIDUsageDescriptionRecommended. The SDK checks whether Face ID or Touch ID is set up, and never asks your user to authenticate

For example, this adds the location message your user sees in the permission dialog (write your own wording), and the URL schemes of well-known jailbreak apps (Cydia, Sileo, Zebra, Filza and unc0ver), which iOS requires before the SDK can check whether they're installed:

<key>NSLocationWhenInUseUsageDescription</key>
<string>We use your location to help protect your account.</string>
<key>LSApplicationQueriesSchemes</key>
<array>
    <string>cydia</string>
    <string>sileo</string>
    <string>zbra</string>
    <string>filza</string>
    <string>undecimus</string>
</array>

3. Start the SDK

Call LeanDevice.initialize once. Start it as early as you can, for example on your splash screen or when your app launches, and at the latest before your login screen. The first signals then reach Lean straight away, so an assessment is ready by the time your user logs in. It's named initialize because init is reserved in Swift.

import SwiftUI
import LeanDevice

@main
struct YourApp: App {
    var body: some Scene {
        WindowGroup {
            ContentView()
                .task {
                    let sessionId = try? await LeanDevice.initialize(
                        LeanDeviceConfig(sdkKey: "lean_device_ae_sandbox_…")
                    )
                    // Keep sessionId to send to your backend. Nil means there is no session:
                    // carry on without Sanad, and see LeanDevice.diagnostics().
                }
        }
    }
}
import UIKit
import LeanDevice

class ViewController: UIViewController {
    override func viewDidLoad() {
        super.viewDidLoad()
        Task {
            let sessionId = try? await LeanDevice.initialize(
                LeanDeviceConfig(sdkKey: "lean_device_ae_sandbox_…")
            )
            // Keep sessionId to send to your backend.
        }
    }
}

initialize opens a session with Lean and returns its id. Then, by default, it reads the device's signals once in the background and sends them to Lean.

It throws only LeanDeviceError with the code missing_sdk_key, when the key is blank. Every other failure returns nil, and LeanDevice.diagnostics().lastError tells you why.

Configuration

ParameterDefaultDescription
sdkKeyRequiredYour SDK key. It also selects the Lean environment
allowPromptsfalseLets the SDK show the "while using the app" location dialog, once per install. Needs NSLocationWhenInUseUsageDescription
collectOnInittrueRead and send signals as soon as the session opens
onSessionChangenilReserved for a future release

4. Collect again before sensitive actions

Call collect() right before each later sensitive action, such as a payment or a change of payout details, so Lean assesses the device as it is at that moment:

func confirmPaymentTapped() {
    LeanDevice.collect()   // returns at once; the work runs in the background
    // ... then send your payment request with the session id
}

A session sends at most 10 sets of signals, counting the one from initialize. After that, lastError is envelope_cap.

5. Send the session id to your backend

Send the session_id to your backend with the login request, so your backend can read the assessment before it decides on the login, and add friction such as an OTP if there are red flags. Your backend keeps it for the rest of the session, so later requests don't need to carry it again.

📘

Tip: link the session to your customer

Once your user logs in, consider linking the session to their Lean customer. Sanad can then also compare the device with that customer's other devices and locations.

6. Check your integration

Run your app on a real iPhone with a sandbox key, and log the diagnostics after initialize:

let d = LeanDevice.diagnostics()
print("state=\(d.state) session=\(d.sessionId ?? "-") error=\(d.lastError ?? "-") sdk=\(d.sdkVersion)")

You should see the state active, a session id and no error. On the simulator, emulator_detected fires, as it should. The codes are explained in Troubleshooting.

How the SDK recognises the device

  • The SDK's own key is created in the Secure Enclave. On a device without a working Secure Enclave, there's no session, and lastError is key_unavailable.
  • The device token that Lean issues is kept in the Keychain, on this device only and never synchronised. Keychain items survive a reinstall, so a reinstalled app is still recognised as the same device. Erasing the phone removes them.

Did this page help you?