> ## Documentation Index
> Fetch the complete documentation index at: https://docs.clariodesk.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Swift lifecycle

> The four host events that touch the SDK: wire each one and you're done.

Four host events interact with the SDK. The concepts are shared across the
SDKs ([read the overview](/concepts/identity-lifecycle)); this page gives the
exact Swift code.

## 1. App launch (every time, including already-logged-in users)

The most common miss: existing users who installed your app before you added
ClarioDesk never hit the login flow again. Hydrate from your own session on
launch and identify **unconditionally**. `identify()` is idempotent and cheap.

```swift theme={null}
import ClarioDesk

@main
struct MyApp: App {
  init() {
    ClarioDesk.initialize(.init(apiKey: "pk_live_…"))
  }

  var body: some Scene {
    WindowGroup {
      ContentView()
        .task {
          if let user = await yourAuth.currentUser() {
            try? await ClarioDesk.identify(
              externalId: user.id,
              email: user.email,
              traits: ["plan": user.plan]
            )
          }
        }
    }
  }
}
```

## 2. Fresh login or signup

Right after your auth succeeds:

```swift theme={null}
try await yourAuth.signIn(email, password)
try await ClarioDesk.identify(externalId: user.id, email: user.email)
```

This overwrites the label on the existing device row. The same device and
hardware key are reused; nothing new is registered.

## 3. Logout

```swift theme={null}
try await ClarioDesk.clearIdentity() // Verified identity
await yourAuth.signOut()
```

This preserves the device key while changing the signed identity epoch. In
label-only mode, `reset()` remains available for full installation isolation.

## 4. User switch (A → B without app restart)

```swift theme={null}
try await ClarioDesk.clearIdentity()
await yourAuth.switchTo(userB)
try await ClarioDesk.identify(identityMode: .host)
```

<Warning>
  Await clear before activating user B. `reset()` deprovisions this installation;
  it is not logout or customer erase.
</Warning>

## Checking state

```swift theme={null}
ClarioDesk.isInitialized  // Bool
ClarioDesk.isIdentified   // Bool
ClarioDesk.isAvailable    // Bool — false once the server declared a terminal
                          // state (device revoked / key expired / app
                          // suspended); use it to hide your support entry point
```

## What if I never call `identify()`?

Tickets still work; agents see a device id but no email, and the dashboard shows
an "Unverified device" badge. This is fine for anonymous feedback; otherwise,
wire case 1.
