For the complete documentation index, see llms.txt. This page is also available as Markdown.

CLI

If you are building your app without using a Framework.

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 ensureInitialized() before interacting with the SDK from JS/TS.

Steps

1

Initialize the Sentiance SDK for iOS

The correct way to natively initialize on iOS is to do it inside the application:didFinishLaunchingWithOptions: method of the AppDelegate class

AppDelegate.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)
  }
}

Launch the iOS app from Xcode and confirm that the log shows “Sentiance SDK Initialization Success”.

2

Initialize the Sentiance SDK for Android

Add the following code inside the onCreate() method of your Application class just after the loadReactNative and ApplicationLifecycleDispatcher function calls:

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:

adb logcat | grep Sentiance
3

Ensure the SDK is initialized from JS/TS code

Because initialization runs asynchronously, await ensureInitialized() before your first SDK call to make sure the SDK is ready. It resolves once the SDK is initialized, and throws an SdkInitializationError if initialization failed or was never triggered.

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

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.

Last updated