Heapchat Docs
🚀 Go to dashboard for project info →

iOS Setup

Learn how to set up and integrate the Heapchat SDK into your iOS application

Prerequisites

  • Xcode 14.0 or later
  • iOS 15.0+ deployment target
  • Swift 5.5 or later
  • A Heapchat account with an API key

Installation

Using Swift Package Manager (SPM)

  1. In Xcode, select File > Add Package Dependencies...
  2. Enter the following URL in the search field:
https://github.com/InspireDevStdio/heapchat-swift-sdk
  1. Click Add Package

Configuration

Initialize the SDK

Add the following code to your main app file to configure Heapchat:

SampleApp.swift
import SwiftUI
import HeapchatSDK

@main
struct SampleApp: App {
    init() {
        Heapchat.shared.configure(apiKey: "YOUR_API_KEY")  
    }

    var body: some Scene {
        WindowGroup {
            ContentView()
        }
    }
}

Replace "YOUR_API_KEY" with your actual Heapchat API key from your dashboard.

Add the Chat Screen

Add the Heapchat screen to your app's view hierarchy:

ContentView.swift
import SwiftUI
import HeapchatSDK

struct ContentView: View {
    var body: some View {
        NavigationStack {
            HeapchatScreen() 
        }
    }
}

Add permissions

This is required for the image upload feature, otherwise the user will not be able to upload images.

Required Permissions

  • Privacy - Photo Library Usage Description: Add a message explaining why your app needs access to the photo library
    <key>NSPhotoLibraryUsageDescription</key>
    <string>We need access to your photo library to upload images to support</string>
  • Privacy - Camera Usage Description: Add a message explaining why your app needs access to the camera
    <key>NSCameraUsageDescription</key>
    <string>We need access to your camera to upload images to support</string>
  • Privacy - Microphone Usage Description: Add a message explaining why your app needs access to the microphone
    <key>NSMicrophoneUsageDescription</key>
    <string>We need access to your microphone to upload audio to support</string>

Authentication

You can authenticate the user in the Heapchat SDK for iOS using the following code:

LoginView.swift
import SwiftUI
import HeapchatSDK

struct LoginView: View {
    var body: some View {
        Button("Login") {
            Heapchat.shared.login(userId: "user_123")
            let heapchatUserData = UserDataModel(
                name: "John Doe",
                email: "john.doe@example.com",
                phone: "+1234567890"
            )
            Heapchat.shared.setCustomerData(heapchatUserData)
        }
    }
}

Passing Extra Custom Data

You can pass additional custom data (as a dictionary/HashMap) to Heapchat for each user. This is useful for storing extra attributes such as user preferences, account type, or any metadata you want to associate with the customer.

Use the setCustomData(_:) method after logging in the user:

LoginView.swift
import SwiftUI
import HeapchatSDK

struct LoginView: View {
    var body: some View {
        Button("Login") {
            ...
            // Pass extra custom data as a dictionary
            let customData: [String: String] = [
                "accountType": "premium",
                "referralCode": "REF12345",
            ]
            Heapchat.shared.setCustomData(customData) 
            ...
        }
    }
}