> For the complete documentation index, see [llms.txt](https://popin.gitbook.io/popin-developer-hub/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://popin.gitbook.io/popin-developer-hub/integrating-popin-sdk-in-ios.md).

# Integrating Popin SDK in iOS

## Integrating Popin SDK in iOS

### On this page

* Introduction
* Step 1: Add Popin iOS SDK Dependency
* Step 2: Initialize Popin SDK
* Step 3: Create a Call Button
* Step 4: Implement Call Button Functionality
* Step 5: PopinConnectingViewController

***

### Introduction

This guide explains how to integrate the **Popin iOS SDK** into your iOS application to enable video calling functionality.

The SDK is designed for straightforward integration and provides pre-built components such as **`PopinConnectingViewController`**, which handles call connection states and provides real-time feedback to users during the call lifecycle.

***

### Step 1: Add Popin iOS SDK Dependency

The Popin iOS SDK can be added to your project using **Swift Package Manager (SPM)**.

#### Using Swift Package Manager

1. Open your project in **Xcode**.
2. Navigate to **File → Add Packages…**
3. Enter the Popin SDK repository URL:

```
https://github.com/Springr-Creatives/PopinIOSSDK
```

4. Select the latest stable version.
5. Add the package to your app target.

Once added, the Popin SDK will be available throughout your project.

***

### Step 2: Initialize Popin SDK

Initialize the Popin SDK when your application launches.\
A **`token`** is required to authenticate and authorize SDK usage.

#### AppDelegate (UIKit lifecycle)

```swift
import PopinSDK

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

    let token = "<YOUR_TOKEN_HERE>"
    Popin.initialize(token: token)

    return true
}
```

#### SceneDelegate (iOS 13+)

```swift
import PopinSDK

func scene(
    _ scene: UIScene,
    willConnectTo session: UISceneSession,
    options connectionOptions: UIScene.ConnectionOptions
) {

    let token = "<YOUR_TOKEN_HERE>"
    Popin.initialize(token: token)
}
```

> **Note:**\
> The token should be securely retrieved from your backend or configuration system and should not be hardcoded in production builds.

***

### Step 3: Create a Call Button

Add a button to your view controller that users can tap to start a video call.

#### Using Storyboard

1. Drag a **UIButton** onto your view.
2. Set the button title to **“Start Call”**.
3. Create an `@IBOutlet` and `@IBAction` connection.

#### Using Programmatic UI (Swift)

```swift
let callButton = UIButton(type: .system)
callButton.setTitle("Start Call", for: .normal)
callButton.addTarget(self, action: #selector(startCallTapped), for: .touchUpInside)
```

***

### Step 4: Implement Call Button Functionality

When the user taps the call button, display a connecting screen and initiate the video call.

#### Example: `ViewController.swift`

```swift
import UIKit
import PopinSDK

class ViewController: UIViewController {

    @objc func startCallTapped() {

        // Show the "Connecting..." screen
        let connectingVC = PopinConnectingViewController()
        present(connectingVC, animated: true)

        // Start the video call and listen for events
        Popin.shared.startCall(listener: PopinEventsListener(
            onCallStart: {
                // Call started connecting
            },
            onQueuePositionChanged: { position in
                // Update UI with queue position if applicable
            },
            onAllExpertsBusy: {
                connectingVC.dismiss(animated: true)
            },
            onCallConnected: {
                connectingVC.dismiss(animated: true)
            },
            onCallFailed: {
                connectingVC.dismiss(animated: true)
            },
            onCallDisconnected: {
                // Handle call disconnection
            }
        ))
    }
}
```

***

### Explanation of Call Events

| Event                           | Description                                             |
| ------------------------------- | ------------------------------------------------------- |
| **onCallStart**                 | Triggered when the SDK begins connecting the call.      |
| **onQueuePositionChanged(Int)** | Provides queue position updates if the user is waiting. |
| **onAllExpertsBusy**            | Called when no agents or experts are available.         |
| **onCallConnected**             | Fired when the video call successfully connects.        |
| **onCallFailed**                | Called when the call cannot be established.             |
| **onCallDisconnected**          | Triggered when the call ends or is disconnected.        |

***

### Step 5: PopinConnectingViewController

`PopinConnectingViewController` is a pre-built UI component that informs users that the call is being connected. It typically displays a **“Connecting…”** or **“Please wait”** message.

#### Showing the Connecting Screen

```swift
let connectingVC = PopinConnectingViewController()
present(connectingVC, animated: true)
```

You may customize this screen by subclassing `PopinConnectingViewController` or adjusting its UI configuration options to match your application’s branding.

***
