3. Initialization

SDK Initialization Steps

Initialization sets up internal SDK components to allow the creation of a Sentiance user on the device, and perform detections in the background.

Only a limited set of SDK methods are allowed to be invoked before initializing the SDK.

1. Initialize in the AppDelegate Class

Initialization must be done in the application:didFinishLaunchingWithOptions: method of your AppDelegate class. If you don't already have a custom AppDelegate set up, first create a new class that extends Application.

class AppDelegate: NSObject, UIApplicationDelegate {
    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil) -> Bool {   
        return true
    }
}

2. Import the SDK in your source file.

@import SENTSDK;

3. Initialize the SDK

In the application:didFinishLaunchingWithOptions: method of your AppDelegate class, call initializeWithOptions:launchOptions:.

AppDelegate.swift
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil) -> Bool {
    // If you monitor crashes, for example by using a third-party crash reporting library,
    // you can pass the previous launch crash status to the Sentiance SDK. The SDK uses this
    // information to detect instability during startup and may take measures to stabilize the app.

    // Example using Firebase Crashlytics:
    // let appCrashedOnPreviousLaunch = Crashlytics.crashlytics().didCrashDuringPreviousExecution()
    let appCrashedOnPreviousLaunch: Bool = false
    let options = SENTOptions(for: .appLaunch, appCrashedOnPreviousLaunch: appCrashedOnPreviousLaunch)

    let result = Sentiance.shared.initialize(options: options)
    if result.isSuccessful {
        NSLog("Initialization succeeded")
    }
    return true
}

Initialization will normally succeed, unless an unexpected error or exception is encountered. In case of failure, you can check the reason via the returned result.

circle-exclamation

Last updated