> ## Documentation Index
> Fetch the complete documentation index at: https://cometchat-22654f5b-docs-audit-mechanical-fixes.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# Setup

> Install, configure, and initialize the CometChat iOS SDK with app credentials, region, dependencies, and setup steps.

<Accordion title="AI Integration Quick Reference">
  ```swift theme={null}
  // CocoaPods
  pod 'CometChatSDK', '4.1.0'

  // Swift Package Manager
  // https://github.com/cometchat/chat-sdk-ios.git
  ```

  ```swift theme={null}
  import CometChatSDK

  let appSettings = AppSettings.AppSettingsBuilder()
    .setRegion(region: "APP_REGION")
    .subscribePresenceForAllUsers()
    .autoEstablishSocketConnection(true)
    .build()

  CometChat.init(appId: "APP_ID", appSettings: appSettings, onSuccess: { _ in
    print("CometChat initialized")
  }, onError: { error in
    print("Init failed: \(error.errorDescription)")
  })
  ```

  **Prerequisites:** Xcode 12+, iOS 11+, credentials from [CometChat Dashboard](https://app.cometchat.com)
</Accordion>

## Prerequisites

| Requirement | Version      |
| ----------- | ------------ |
| Xcode       | 12 or above  |
| iOS         | 11 or higher |

Get your credentials from the [CometChat Dashboard](https://app.cometchat.com):

* App ID
* Region
* Auth Key (for development)

<Warning>
  **Auth Key** is for development/testing only. In production, generate **Auth Tokens** on your server using the REST API. Never expose Auth Keys in production client code.
</Warning>

## Installation

### CocoaPods

Create a `Podfile` in your project directory:

```bash theme={null}
$ pod init
```

Add the CometChat SDK to your Podfile:

```ruby theme={null}
platform :ios, '11.0'
use_frameworks!

target 'MyApp' do
  pod 'CometChatSDK', '4.1.0'
end
```

Install the dependency:

```bash theme={null}
$ pod install
```

To update to the latest version:

```bash theme={null}
$ pod update CometChatSDK
```

<Warning>
  CometChatSDK includes video calling components. We suggest you run on physical devices to avoid errors.
</Warning>

### Swift Package Manager

1. Open **Xcode**, go to the project's **General** settings tab and select the project under **Project** in the left column.
2. Go to the **Swift packages** tab and click on the **+** button.

<Frame>
  <img src="https://mintcdn.com/cometchat-22654f5b-docs-audit-mechanical-fixes/C_cPQYxvQBDBywyf/images/fa786752-1634032671-f0fc1c00c9f33cca47875131228648cb.png?fit=max&auto=format&n=C_cPQYxvQBDBywyf&q=85&s=3366833ec38a076c443f86452028c388" width="1919" height="1055" data-path="images/fa786752-1634032671-f0fc1c00c9f33cca47875131228648cb.png" />
</Frame>

3. Enter the repository URL [`https://github.com/cometchat/chat-sdk-ios.git`](https://github.com/cometchat/chat-sdk-ios.git), set dependency rule to `Up to Next Major Version` with version `4.1.0`, and click **Add Package**.

<Frame>
  <img src="https://mintcdn.com/cometchat-22654f5b-docs-audit-mechanical-fixes/TIjyXiMHb2A7oH7j/images/edbea27b-nh6l7iltomtl4me5y6yzvupevalnh891fcoemppo30e6ijlc2boq1pxyanc6ujnt-438a8e2fee0f935ba81fc7ee14dc5ab7.png?fit=max&auto=format&n=TIjyXiMHb2A7oH7j&q=85&s=f40c2db5e129eab116df7069ea956457" width="2170" height="1212" data-path="images/edbea27b-nh6l7iltomtl4me5y6yzvupevalnh891fcoemppo30e6ijlc2boq1pxyanc6ujnt-438a8e2fee0f935ba81fc7ee14dc5ab7.png" />
</Frame>

4. Ensure `CometChatSDK` is checked in the **Package Product** column and click **Add Package**.

<Frame>
  <img src="https://mintcdn.com/cometchat-22654f5b-docs-audit-mechanical-fixes/KCkOxYBmiffdHQgV/images/31711554-ehjnlsoxuq3n7awjtsdjkggol8jegnsjnwgisr4a8nkrr1gt6nfjyd6azzk5c57w-1b3966209f90b9a67a5ad7107b8c25ec.png?fit=max&auto=format&n=KCkOxYBmiffdHQgV&q=85&s=957097b3f7055910234a342524b9ac2d" width="1316" height="604" data-path="images/31711554-ehjnlsoxuq3n7awjtsdjkggol8jegnsjnwgisr4a8nkrr1gt6nfjyd6azzk5c57w-1b3966209f90b9a67a5ad7107b8c25ec.png" />
</Frame>

### Request Authorization

Add justification strings to your app's `Info.plist` for camera, microphone, and photo library access:

<Frame>
  <img src="https://mintcdn.com/cometchat-22654f5b-docs-audit-mechanical-fixes/2n7DD5y6r-nNza4C/images/0119d1bb-1623200477-7bdad6acb11779de4a98a154e785d56a.png?fit=max&auto=format&n=2n7DD5y6r-nNza4C&q=85&s=7fee2a2b23e4fd40ed120f47b142daa4" width="1020" height="362" data-path="images/0119d1bb-1623200477-7bdad6acb11779de4a98a154e785d56a.png" />
</Frame>

```xml theme={null}
<key>NSAppTransportSecurity</key>
  <dict>
    <key>NSAllowsArbitraryLoads</key>
    <true/>
  </dict>
<key>NSCameraUsageDescription</key>
  <string>$(PRODUCT_NAME) need access to the camera in order to update your avatar</string>
<key>NSPhotoLibraryUsageDescription</key>
  <string>$(PRODUCT_NAME) need access to the Photo Library in order to send Media Messages</string>
<key>NSMicrophoneUsageDescription</key>
  <string>$(PRODUCT_NAME) need access to the Microphone in order to connect Audio/Video call</string>
```

### Setup Bitcode

Set **Enable Bitcode** to `YES` in your target's build settings.

<Frame>
  <img src="https://mintcdn.com/cometchat-22654f5b-docs-audit-mechanical-fixes/tu3JK9T7iskLt8fa/images/477193c3-1631809611-db96539a98643ad55b7950d86d4b6557.png?fit=max&auto=format&n=tu3JK9T7iskLt8fa&q=85&s=47d7fa97bdf54eb0c6aaef237474cc94" width="1238" height="123" data-path="images/477193c3-1631809611-db96539a98643ad55b7950d86d4b6557.png" />
</Frame>

### Swift Standard Libraries

Set `"Always Embed Swift Standard Libraries"` to `Yes` in your target's build settings:

<Frame>
  <img src="https://mintcdn.com/cometchat-22654f5b-docs-audit-mechanical-fixes/R68qChTpDg6vLLxX/images/6ae89ab8-1623200430-15b0442a710f78a45dd01090dc6a32f1.png?fit=max&auto=format&n=R68qChTpDg6vLLxX&q=85&s=2551a6ce811195c3cc921fc22c873d58" width="861" height="191" data-path="images/6ae89ab8-1623200430-15b0442a710f78a45dd01090dc6a32f1.png" />
</Frame>

### Set Header Search Path

Set `Header Search Paths` to `$SDKROOT/usr/include/libxml2`.

<Frame>
  <img src="https://mintcdn.com/cometchat-22654f5b-docs-audit-mechanical-fixes/0Dzaagp0tKHyO4xb/images/57d60afc-1631809733-acc69d5f571a8aac9ac8683e9f1ed0eb.png?fit=max&auto=format&n=0Dzaagp0tKHyO4xb&q=85&s=dbc145f1e8ea0f266cb3d5415508910d" width="861" height="389" data-path="images/57d60afc-1631809733-acc69d5f571a8aac9ac8683e9f1ed0eb.png" />
</Frame>

## Initialization

The `init()` method initializes the SDK and must be called before any other CometChat method. Call it once at app startup, typically in `didFinishLaunchingWithOptions:` of your `AppDelegate`.

<Tabs>
  <Tab title="Swift">
    ```swift theme={null}
    import CometChatSDK

    class AppDelegate: UIResponder, UIApplicationDelegate {

      var window: UIWindow?
      let appId: String = "APP_ID"
      let region: String = "APP_REGION"

      func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {

        let mySettings = AppSettings.AppSettingsBuilder()
          .subscribePresenceForAllUsers()
          .setRegion(region: region)
          .autoEstablishSocketConnection(true)
          .build()

        CometChat.init(appId: appId, appSettings: mySettings, onSuccess: { (isSuccess) in
          if (isSuccess) {
            print("CometChat SDK initialized successfully.")
          }
        }) { (error) in
          print("CometChat SDK failed to initialize with error: \(error.errorDescription)")
        }
        return true
      }
    }
    ```
  </Tab>

  <Tab title="Objective-C">
    ```objc theme={null}
    #import <CometChatSDK_CometChatSDK-Swift.h>

    @implementation AppDelegate

    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

      NSString *region = @"APP_REGION";
      NSString *appID = @"APP_ID";

      AppSettingsBuilder *appSettingBuilder = [[AppSettingsBuilder alloc] init];
      AppSettings *appSettings = [[[appSettingBuilder subscribePresenceForAllUsers] setRegionWithRegion:region] build];

      [[CometChat alloc] initWithAppId:appID appSettings:appSettings onSuccess:^(BOOL isSuccess) {
        NSLog(isSuccess ? @"CometChat Initialize Success" : @"CometChat Initialize Failed");
      } onError:^(CometChatException * error) {
        NSLog(@"Error %@", [error errorDescription]);
      }];
      return YES;
    }

    @end
    ```
  </Tab>
</Tabs>

Replace `APP_ID` and `APP_REGION` with your credentials from the [Dashboard](https://app.cometchat.com).

<Warning>
  `CometChat.init()` must be called before any other SDK method. Calling `login()`, `sendMessage()`, or registering listeners before `init()` will fail.
</Warning>

### Parameters

| Parameter   | Type        | Description                                        |
| ----------- | ----------- | -------------------------------------------------- |
| appId       | String      | Your CometChat App ID                              |
| appSettings | AppSettings | Configuration object built with AppSettingsBuilder |

### AppSettings Options

| Method                              | Description                                          | Default  |
| ----------------------------------- | ---------------------------------------------------- | -------- |
| `setRegion(region:)`                | Region where your app was created (`us`, `eu`, `in`) | Required |
| `subscribePresenceForAllUsers()`    | Subscribe to presence events for all users           | —        |
| `subscribePresenceForRoles(roles:)` | Subscribe to presence for specific roles             | —        |
| `subscribePresenceForFriends()`     | Subscribe to presence for friends only               | —        |
| `autoEstablishSocketConnection(_:)` | Let SDK manage WebSocket connections                 | `true`   |
| `overrideAdminHost(adminHost:)`     | Custom admin URL (dedicated deployment)              | —        |
| `overrideClientHost(clientHost:)`   | Custom client URL (dedicated deployment)             | —        |

### Presence Subscription

Choose how to subscribe to user presence (online/offline status):

```swift theme={null}
// All users
AppSettings.AppSettingsBuilder()
  .subscribePresenceForAllUsers()
  .setRegion(region: region)
  .build()

// Specific roles
AppSettings.AppSettingsBuilder()
  .subscribePresenceForRoles(roles: ["admin", "moderator"])
  .setRegion(region: region)
  .build()

// Friends only
AppSettings.AppSettingsBuilder()
  .subscribePresenceForFriends()
  .setRegion(region: region)
  .build()
```

See [User Presence](/sdk/ios/user-presence) for more details.

### WebSocket Connection

By default, the SDK manages WebSocket connections automatically. To manage them manually:

```swift theme={null}
let mySettings = AppSettings.AppSettingsBuilder()
  .setRegion(region: region)
  .autoEstablishSocketConnection(false)
  .build()
```

See [Managing WebSocket Connections](/sdk/ios/managing-web-socket-connections-manually) for manual control.

***

## Next Steps

<CardGroup cols={2}>
  <Card title="Authentication" icon="lock" href="/sdk/ios/authentication-overview">
    Log in users with Auth Key or Auth Token
  </Card>

  <Card title="Send Messages" icon="paper-plane" href="/sdk/ios/send-message">
    Send your first message
  </Card>
</CardGroup>
