YourGPT Helpdesk

Find answers to your questions

How to integrate YourGPT Chatbot widget in an iOS application

Step-by-Step guide to integrate YourGPT widget in an iOS application

Integrate AI-powered chatbot functionality into your iOS applications with the YourGPT iOS SDK. This comprehensive guide covers installation via CocoaPods and Swift Package Manager, complete implementation steps, and best practices for seamless chatbot integration in your native iOS apps.


What is YourGPT iOS SDK?

The YourGPT iOS SDK is a native Swift package that brings intelligent AI chatbot capabilities to your iOS applications. Whether you're building customer support features, interactive assistants, or conversational interfaces, this SDK provides a seamless integration experience optimized for iOS devices.

Prerequisites

Before you begin, ensure you have:

  • Xcode 13.0 or later installed

  • iOS 13.0+ as your deployment target

  • A YourGPT account with a widget UID (sign up here)

  • Basic knowledge of iOS development and Swift

  • CocoaPods or Swift Package Manager configured in your project

Finding Your Widget UID

To obtain your widget UID:

  1. Log in to your YourGPT dashboard

  2. Go to Integration section

  3. Copy the widget UID from the integration section

  4. Replace "your-widget-uid" in the code with your actual UID


Installation Methods

Add this to your Podfile:

pod 'YourGPTSDK', '~> 1.0'

Then run:

pod install

Swift Package Manager

Add the package dependency in Xcode:

  1. File → Add Package Dependencies

  2. Enter: https://github.com/YourGPT/yourgpt-widget-sdk-ios.git

  3. Select version 1.0.0

Or add to Package.swift:

dependencies: [
    .package(url: "https://github.com/YourGPT/yourgpt-widget-sdk-ios.git", from: "1.0.0")
]

Implementation Steps

To use the YourGPT chatbot widget in your iOS application, follow these steps or simply copy and paste the code below into your application's respective files.

For better organization and reusability, create a wrapper class to manage the SDK:

import UIKit
import YourGPTSDK
import Combine

@available(iOS 13.0, *)
class YourGPTWrapper: NSObject {
    
    static let shared = YourGPTWrapper()
    
    private var cancellables = Set<AnyCancellable>()
    private var chatbotViewController: YourGPTChatbotViewController?

    // State observer
    var onStateChange: ((YourGPTSDKState) -> Void)?
    
    private override init() {
        super.init()
        setupSDKObserver()
    }
    
    private func setupSDKObserver() {
        YourGPTSDK.core.$state
            .receive(on: DispatchQueue.main)
            .sink { [weak self] state in
                self?.onStateChange?(state)
            }
            .store(in: &cancellables)
    }
    
    func initializeSDK(widgetUid: String) async throws {
        let config = YourGPTConfig(widgetUid: widgetUid)
        try await YourGPTSDK.initialize(config: config)
    }
    
    func openChatbot(from presentingViewController: UIViewController, delegate: YourGPTChatbotDelegate?) {
        guard YourGPTSDK.isReady else {
            showAlert(on: presentingViewController, title: "SDK Not Ready", message: "Please wait for the SDK to initialize.")
            return
        }
        
        chatbotViewController = YourGPTSDK.createChatbotViewController()

        chatbotViewController?.delegate = delegate

        // Add close button
        let closeButton = UIBarButtonItem(
            barButtonSystemItem: .close,
            target: self,
            action: #selector(closeChatbot)
        )
        chatbotViewController?.navigationItem.rightBarButtonItem = closeButton

        let navigationController = UINavigationController(rootViewController: chatbotViewController!)
        navigationController.modalPresentationStyle = .overCurrentContext
        navigationController.modalTransitionStyle = .coverVertical

        presentingViewController.present(navigationController, animated: true)
    }
    
    @objc private func closeChatbot() {
        chatbotViewController?.dismiss(animated: true) { [weak self] in
            self?.chatbotViewController = nil
        }
    }

    func dismissChatbot() {
        chatbotViewController?.dismiss(animated: true) { [weak self] in
            self?.chatbotViewController = nil
        }
    }

    var isReady: Bool {
        return YourGPTSDK.isReady
    }

    var currentState: YourGPTSDKState {
        return YourGPTSDK.core.state
    }
    
    private func showAlert(on viewController: UIViewController, title: String, message: String) {
        let alert = UIAlertController(title: title, message: message, preferredStyle: .alert)
        alert.addAction(UIAlertAction(title: "OK", style: .default))
        viewController.present(alert, animated: true)
    }
}

This wrapper class provides:

  • Singleton Pattern: Ensures single instance across your app

  • State Management: Observes SDK connection states using Combine

  • Async Initialization: Modern Swift concurrency support

  • Presentation Management: Handles chatbot view controller lifecycle

  • Delegate Support: Enables custom event handling

2: Initialize the SDK in Your View Controller

import UIKit
import YourGPTSDK

@available(iOS 13.0, *)
class ViewController: UIViewController {
    
    @IBOutlet weak var statusLabel: UILabel?
    @IBOutlet weak var openChatButton: UIButton?
    
    override func viewDidLoad() {
        super.viewDidLoad()
        setupUI()
        setupSDKObserver()
        initializeSDK()
    }
    
    private func setupUI() {
        title = "YourGPT iOS SDK Demo"
        view.backgroundColor = .systemBackground
        
        // Configure button
        openChatButton?.layer.cornerRadius = 8
        openChatButton?.isEnabled = false
        updateStatus("Initializing...")
    }
    
    private func setupSDKObserver() {
        YourGPTWrapper.shared.onStateChange = { [weak self] state in
            self?.updateUIForSDKState(state)
        }
    }
    
    private func initializeSDK() {
        Task {
            do {
                try await YourGPTWrapper.shared.initializeSDK(widgetUid: "your-widget-uid")
            } catch {
                await MainActor.run {
                    self.showAlert(title: "SDK Error", message: error.localizedDescription)
                }
            }
        }
    }
    
    private func updateUIForSDKState(_ state: YourGPTSDKState) {
        switch state.connectionState {
        case .connected:
            statusLabel?.textColor = .systemGreen
            openChatButton?.isEnabled = true
            updateStatus("Ready - SDK Connected!", color: .systemGreen)
        case .connecting:
            statusLabel?.textColor = .systemOrange
            openChatButton?.isEnabled = false
            updateStatus("Connecting...", color: .systemOrange)
        case .error:
            statusLabel?.textColor = .systemRed
            openChatButton?.isEnabled = false
            if let error = state.error {
                updateStatus("Error: \(error)", color: .systemRed)
            }
        case .disconnected:
            statusLabel?.textColor = .systemGray
            openChatButton?.isEnabled = false
            updateStatus("Disconnected", color: .systemGray)
        }
    }
    
    private func updateStatus(_ text: String, color: UIColor = .systemOrange) {
        statusLabel?.text = "SDK Status: \(text)"
        statusLabel?.textColor = color
    }
    
    @IBAction func openChatTapped(_ sender: UIButton? = nil) {
        YourGPTWrapper.shared.openChatbot(from: self, delegate: self)
    }
    
    private func showAlert(title: String, message: String) {
        let alert = UIAlertController(title: title, message: message, preferredStyle: .alert)
        alert.addAction(UIAlertAction(title: "OK", style: .default))
        present(alert, animated: true)
    }
}

This implementation includes:

  • Real-time Status Updates: Visual feedback for SDK connection states

  • Error Handling: Graceful handling of initialization errors

  • Button State Management: Enables chat button only when SDK is ready

  • Clean Architecture: Separation of concerns with wrapper pattern

Configuration

The YourGPTConfig only requires your widget UID:

let config = YourGPTConfig(widgetUid: "your-widget-uid")

Demo Screenshots

Here's how the YourGPT chatbot looks in action:





https://github.com/YourGPT/yourgpt-widget-sdk-ios


Was this article helpful?
©2025
Powered by YourGPT