> For the complete documentation index, see [llms.txt](https://docs.connect.ai/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.connect.ai/sdk-integration-guides/swift-ios.md).

# Swift (iOS)

### Installation

To integrate `ConnectFramework` into your Swift project, add the SDK via Swift Package Manager (SPM) or manually include it in your project.

#### Swift Package Manager

1. In Xcode, go to **File > Add Packages**.
2. Enter the package repository URL for `ConnectFramework`.
3. Select a version or branch and press **Add Package**.

Then, import the framework where needed:

```swift
import ConnectFramework
```

***

### Usage

#### Initializing the SDK

Before making any API calls, initialize the SDK with your API key.

```swift
// Initialize the SDK with an API key
ConnectFramework.initialize(apiKey: "your_api_key_here")
```

***

### User Authentication

#### Logging in with a Unique ID

Logs the user in with a unique ID. You can pass `nil`, and a unique ID will be created automatically.

```swift
ConnectFramework.login(uniqueId: nil) { success in
    if success {
        print("User logged in successfully.")
    } else {
        print("Login failed or was cancelled.")
    }
}
```

#### Logging Out

Logs the user out entirely.

```swift
ConnectFramework.logout()
```

#### Checking if User is Connected

Returns `true` if the user is logged in and running the Connect.ai desktop client.

```swift
let isConnected = ConnectFramework.isConnected()
print("User is connected: \(isConnected)")
```

***

### Paywall Management

#### Creating a Paywall

Creates and presents a new paywall. The callback receives two parameters:

* `success` (`Bool`): `true` if the user completed a purchase, `false` otherwise.
* `productId` (`String?`): The product ID if a purchase occurred, otherwise `nil`.

```swift
ConnectFramework.createPaywall { success, productId in
    if success {
        print("User purchased product: \(productId ?? "Unknown")")
    } else {
        print("User did not complete the purchase.")
    }
}
```

#### Setting Extra JSON Data

Optional: Configures the user JSON settings used by the paywall.

```swift
let extraSettings = "{\"custom_key\": \"custom_value\"}"
ConnectFramework.setExtraJson(json: extraSettings)
```

#### Checking if User has Purchased

Returns `true` if the user has already made a purchase. You should not present a paywall if this returns `true`.

```swift
let hasPurchased = ConnectFramework.hasPurchased()
print("User has purchased: \(hasPurchased)")
```

***

### Analytics Tracking

#### Logging an Event

Logs an analytics event with a category and action.

```swift
ConnectFramework.logEvent(category: "user_action", action: "button_click")
```

#### Logging an Event with Additional Data

Logs an analytics event with a category, action, label, and numerical value.

```swift
ConnectFramework.logEvent(category: "user_action", action: "purchase", label: "premium_upgrade", value: 29.99)
```
