> 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)
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://docs.connect.ai/sdk-integration-guides/swift-ios.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
