# 3. Initialization

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`.

```swift
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.

```objectivec
@import SENTSDK;
```

### 3. Initialize the SDK

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

{% code title="AppDelegate.swift" %}

```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
}
```

{% endcode %}

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.

```swift
if result.isSuccessful {
    NSLog("Initialization succeeded")
} else {
    NSLog("Initialization failed with reason \(result.failureReason)")
}
```

{% hint style="warning" %}
**Incorrect Initialization**

You must initialize the SDK during app startup, before `application:didFinishLaunchingWithOptions:` returns. Therefore, you must do it synchronously on the main thread. If you plan to add a remote flag to control the initialization (e.g. Firebase Remote Config), make sure the check is synchronous (e.g. utilizing a cached flag).
{% endhint %}


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.sentiance.com/a-complete-integration/ios-sdk/initialization.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
