Skip to main content

iOS Usage

The Lovina Chat SDK provides LovinaChatWidget (a UIView subclass) for UIKit and LovinaChatView (a UIViewRepresentable) for SwiftUI.

UIKit Integration

import UIKit
import LovinaChatSDK

class ChatViewController: UIViewController {

private let chatWidget = LovinaChatWidget()

override func viewDidLoad() {
super.viewDidLoad()

view.addSubview(chatWidget)
chatWidget.frame = view.bounds
chatWidget.autoresizingMask = [.flexibleWidth, .flexibleHeight]

// Listen for events
chatWidget.onEvent = { event in
switch event {
case .loaded(let authToken):
print("Widget loaded, token: \(authToken ?? "none")")
case .messageReceived(let message):
print("New message: \(message)")
case .unreadCountChanged(let count):
self.updateBadge(count)
case .closed:
self.dismiss(animated: true)
case .error(let code, let message):
print("Error [\(code)]: \(message)")
}
}

// Configure and load
chatWidget.configure(LovinaChatConfig(
websiteToken: "your-website-token",
baseUrl: "https://chat.lovina.app"
))
}

deinit {
chatWidget.destroy()
}
}

SwiftUI Integration

Use the LovinaChatView wrapper to embed the chat widget in SwiftUI:

import SwiftUI
import LovinaChatSDK

struct ChatScreen: View {
var body: some View {
LovinaChatView(
config: LovinaChatConfig(
websiteToken: "your-website-token",
baseUrl: "https://chat.lovina.app"
),
onEvent: { event in
switch event {
case .messageReceived(let message):
print("New message: \(message)")
default:
break
}
}
)
.edgesIgnoringSafeArea(.all)
}
}

User Identification

Pass user information to associate messages with a known contact:

let config = LovinaChatConfig(
websiteToken: "your-token",
baseUrl: "https://chat.lovina.app",
user: LovinaChatUser(
identifier: "user-12345",
name: "Jane Doe",
email: "jane@example.com",
phoneNumber: "+6281234567890",
identifierHash: "hmac-hash-for-verification",
customAttributes: [
"plan": "premium",
"signup_date": "2025-01-15"
]
)
)

White-Label Theming

Apply a custom brand color to the widget header and chat bubbles:

let config = LovinaChatConfig(
websiteToken: "your-token",
baseUrl: "https://chat.lovina.app",
customColor: UIColor(red: 0.39, green: 0.40, blue: 0.95, alpha: 1.0) // Indigo
)

The SDK automatically detects whether the custom color is light or dark and adjusts text contrast accordingly.

Session Management

Conversation sessions are persisted automatically in the Keychain. Returning users resume their existing conversation.

To clear a session and start fresh:

chatWidget.clearSession()
chatWidget.reload()

Lifecycle

The widget cleans up automatically in deinit. You may also call destroy() explicitly when the hosting view controller is dismissed:

deinit {
chatWidget.destroy()
}