> For the complete documentation index, see [llms.txt](https://docs.sentiance.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.sentiance.com/getting-started/sdk-integration/3.-sdk-initialization/react-native/cli.md).

# CLI

Initialization allows the SDK to perform detections in the background. You must always initialize the SDK before interacting with it. Only a limited set of SDK methods are safe to invoke on an uninitialized SDK.

Initialization requires making some changes inside the **native Android and iOS folders**. The SDK initializes **asynchronously**, so its native bootstrap may still be in progress after your app has started and your JavaScript/TypeScript code begins running. Because most SDK methods require an initialized SDK, always guard your calls by awaiting <mark style="color:$success;">`ensureInitialized()`</mark> before interacting with the SDK from JS/TS.

## Steps

{% stepper %}
{% step %}

### Initialize the Sentiance SDK for iOS

The correct way to natively initialize on iOS is to do it inside the <mark style="color:$success;">`application:didFinishLaunchingWithOptions:`</mark> method of the <mark style="color:$success;">`AppDelegate`</mark> class

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

```swift
import UIKit
import React
import React_RCTAppDelegate
import ReactAppDependencyProvider
import RNSentianceCore

@main
class AppDelegate: RCTAppDelegate {
  override func application(
    _ application: UIApplication,
    didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]? = nil
  ) -> Bool {
    // ...

    RNSentianceHelper().initializeAsync(launchOptions: launchOptions) { result, error in
      if error == nil {
        print("Sentiance SDK Initialization Success")
      } else {
        print("Sentiance SDK Initialization Failed: \(error!.failureReason)")
      }
    }

    return super.application(application, didFinishLaunchingWithOptions: launchOptions)
  }
}
```

{% endcode %}

Launch the iOS app from Xcode and confirm that the log shows **“Sentiance SDK Initialization Success”.**
{% endstep %}

{% step %}

### Initialize the Sentiance SDK for Android

Add the following code inside the <mark style="color:$success;">**`onCreate()`**</mark> method of your <mark style="color:$success;">**`Application`**</mark> class just after the <mark style="color:$success;">**`loadReactNative`**</mark> and <mark style="color:$success;">**`ApplicationLifecycleDispatcher`**</mark> function calls:

```kotlin
import android.util.Log
import com.sentiance.react.bridge.core.SentianceHelper

private const val TAG = "Sentiance"

override fun onCreate() {
    super.onCreate()
    // ...

    val sentianceHelper = SentianceHelper.getInstance(this)
    sentianceHelper.initializeAsync()?.addOnCompleteListener { operation ->
      if (operation.isSuccessful) {
        Log.d(TAG, "Sentiance SDK Initialization succeeded")
      } else {
        Log.e(TAG, "Sentiance SDK Initialization failed with reason " + operation.error?.failureReason)
      }
    }
}
```

Run the app, open a new Terminal window and verify logs by running the command:

```bash
adb logcat | grep Sentiance
```

{% endstep %}

{% step %}

### Ensure the SDK is initialized from JS/TS code

Because initialization runs asynchronously, <mark style="color:$success;">`await ensureInitialized()`</mark> before your first SDK call to make sure the SDK is ready. It resolves once the SDK is initialized, and throws an <mark style="color:$danger;">`SdkInitializationError`</mark> if initialization failed or was never triggered.

```typescript
import SentianceCore, {
  SdkInitializationError,
} from "@sentiance-react-native/core";

try {
  // Resolves once native async init completes (immediately if already initialized).
  await SentianceCore.ensureInitialized();
  console.log("Sentiance SDK is initialized");

  // Safe to interact with the SDK from here on.
  const userExists = await SentianceCore.userExists();
  console.log("Sentiance user exists:", userExists);
} catch (err) {
  if (!(err instanceof SdkInitializationError)) {
    console.error("Unexpected error while awaiting Sentiance SDK init", err);
  } else {
    switch (err.reason) {
      case "NOT_TRIGGERED":
        // The native initializer was never called from Application.onCreate / AppDelegate.
        console.error("Sentiance SDK init was never triggered natively");
        break;
      case "EXCEPTION_OR_ERROR":
        console.error("Sentiance SDK init hit an exception or error:", err.message);
        break;
        
      // Handle the other cases here  
    }
  }
}
```

{% endstep %}

{% step %}

### Next: User Creation

After having successfully initialized the Sentiance SDK, you must now create a user. To do so, follow the instructions on [this page](/getting-started/sdk-integration/4.-user-creation.md).
{% endstep %}
{% endstepper %}
