﻿
# SDK Termination

This article describes how to terminate the libSoftphone SDK gracefully on iOS.

## Overview

Termination cleanly shuts down the SDK by attempting to end active calls and unregister all SIP accounts. It is asynchronous: request termination, retain the returned handle, and wait for the completion handler before finishing the SDK shutdown lifecycle.

Termination does not delete persisted account configuration. For a user logout, first delete every account belonging to the user and then terminate the SDK. See [Logging Out](../libsoftphone-android-ios/getting-started-ios.md#logging-out).

During shutdown, the SDK also synchronizes registration state with SIPIS. Because logout deletes the accounts first, this synchronization deletes their corresponding SIPIS instances.

## Requesting Termination

Call `requestTermination(within:completionHandler:)` on the SDK state. The timeout is expressed in seconds; 10–20 seconds is typical.

```swift
private var terminationHandle: ResourceHandle2?

func terminateSoftphone() {
    let softphone = SoftphoneBridge.instance()
    guard let state = softphone.state() else {
        return
    }

    terminationHandle = state.requestTermination(within: 20) {
        // Termination has finished or its timeout was reached.
        // Finish the SDK shutdown lifecycle here.
    }
}
```

The SDK attempts to finish all termination tasks before the timeout. When the timeout is reached, the SDK is considered terminated even if every task has not finished.

!!! important

    Keep the returned `ResourceHandle2` alive through the completion handler and for as long as the SDK must remain terminated. Releasing it before completion cancels the request and can prevent termination from completing; releasing it after completion respawns the SDK.

    iOS applications should not call `exit(0)`. Use the completion handler to update application state and finish the SDK lifecycle.

## Releasing the Handle Before Completion

Releasing the handle before the completion handler runs cancels the request:

```swift
terminationHandle = nil
```

Do not use this as the normal way to resume SDK processing. Retain the handle through the completion handler, then explicitly respawn the SDK as described below.

## Using the SDK Again

After termination has completed, keep the handle alive for as long as the SDK should stay terminated. Before starting a new SDK session in the same process, explicitly respawn the SDK and then release the handle:

```swift
SoftphoneBridge.instance().state()?.respawn()
terminationHandle = nil
```

All remaining accounts register again after the SDK respawns.

The completion handler runs when cleanup finishes or the timeout is reached. If remote PBX or SIPIS contacts remain after a timeout, they can persist until their registration expires.

