App Lifecycle

Capture native iOS foreground and background transitions automatically or from explicit UIKit callbacks.

Lifecycle state tells Ansight whether the app was foregrounded, backgrounded, or unknown when telemetry and events were captured.

Automatic Capture

The aggregate iOS developer defaults enable lifecycle capture:

try AnsightRuntime.shared.initializeAndActivateAnsightSdk()

The SDK observes UIKit application notifications and records foreground/background transitions.

Manual Lifecycle State

Use manual calls when your app owns lifecycle routing or disables automatic capture.

AnsightRuntime.shared.setAppLifecycleState(.foreground)
AnsightRuntime.shared.setAppLifecycleState(.background)

AppDelegate Example

import AnsightCore
import UIKit

final class AppDelegate: NSObject, UIApplicationDelegate {
    func applicationDidBecomeActive(_ application: UIApplication) {
        AnsightRuntime.shared.setAppLifecycleState(.foreground)
    }

    func applicationDidEnterBackground(_ application: UIApplication) {
        AnsightRuntime.shared.setAppLifecycleState(.background)
    }
}

Duplicate assignments are ignored, so repeatedly setting the same state does not create extra lifecycle events.

Configure Automatic Capture

let options = try AnsightOptions.createBuilder()
    .withLifecycleCapture(
        AnsightLifecycleCaptureOptions(
            enabled: true,
            captureAppLifecycle: true,
            captureScreenViews: true
        )
    )
    .build()

Disable lifecycle capture by passing AnsightLifecycleCaptureOptions.disabled.