Skip to main content
Version: Next

Integration into Application Lifecycle

Description of ChatCenterUI SDK integration process with synchronization of main application (App Lifecycle) and SDK lifecycles.

Overall Interaction Scheme

The diagram below demonstrates how application lifecycle events should trigger corresponding SDK methods for typical integration.


1. Application Launch

SDK initialization should occur as early as possible, typically in application(_:didFinishLaunchingWithOptions:) method. This is critical for correct operation and minimizing possible integration errors.

Creating ChatCenterUISDK instance happens very quickly and no network requests are performed during the process.

File: AppDelegate.swift

import ChatCenterUI

var chatCenterSDK: ChatCenterUISDK?

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

// 1. Server connection configuration
let chatTransportConfig = ChatTransportConfig(
rest: "https://your-server.edna.ru",
webSocket: "wss://your-server.edna.ru",
dataStore: "https://your-server.edna.ru/files"
)

// 2. Chat configuration
let chatConfig = ChatConfig(transportConfig: chatTransportConfig)

// 3. SDK Initialization
// providerUid — unique application ID provided by edna
chatCenterSDK = ChatCenterUISDK(
providerUid: "YOUR_PROVIDER_UID",
chatConfig: chatConfig
)

return true
}

2. User Authorization

After your application authorizes the user (e.g., login to authorized zone completed), you need to inform the SDK about user data. This allows loading chat history for specific client.

File: AuthService.swift / LoginViewModel.swift

func handleSuccessfulLogin(userId: String, userName: String?) {
guard let chatCenterSDK = (UIApplication.shared.delegate as? AppDelegate)?.chatCenterSDK else { return }

// Form user object
let chatUser = ChatUser(
identifier: userId, // stable ID in your system
name: userName, // optional
data: ["segment": "premium"] // optional, arbitrary attributes displayed in client profile
)

// Set user in SDK
chatCenterSDK.authorize(user: chatUser)
}

3. Opening Chat

To display chat interface, SDK provides ready-made UIViewController. It can be shown modally or pushed to your navigation stack, etc.

File: MainViewController.swift

@objc func openSupportChat() {
guard let chatCenterSDK = (UIApplication.shared.delegate as? AppDelegate)?.chatCenterSDK else { return }

// Get chat controller
let result = Result { try chatCenterSDK.getChat() }

switch result {
case let .success(chatController):
// Open chat screen

// Option A: Push (if NavigationController exists)
self.navigationController?.pushViewController(chatController, animated: true)

// Option B: Modal
// self.present(chatController, animated: true, completion: nil)
case let .failure(error):
// Error handling
switch error {
case ChatCenterUIError.userNotAuthorized:
print("User not set for chat")
case ChatCenterUIError.chatNotOpened:
print("Chat not opened")
default:
print("Chat opening error: \(error)")
}
}
}

4. User Logout

When user logs out of application account, you need to terminate SDK session. This removes local data and unsubscribes device from push notifications for this user.

File: ProfileViewModel.swift

func performLogout() {
guard let chatCenterSDK = (UIApplication.shared.delegate as? AppDelegate)?.chatCenterSDK else { return }

do {
// Clear user data and close connection
try chatCenterSDK.logout()
print("Chat SDK: Logged out successfully")
} catch {
print("Chat SDK: Logout error: \(error)")
}

// Application authorization screen transition logic follows
}

If there is an authorized zone in the application (closed by PIN code entry) and it's necessary to terminate current session when exiting it, you can use deauthorizeUser() method for local user removal and stopping SDK network activity.

func performLogout() {
guard let chatCenterSDK = (UIApplication.shared.delegate as? AppDelegate)?.chatCenterSDK else { return }
// Local user removal and connection closure
chatCenterSDK.deauthorizeUser()

// Application authorization screen transition logic follows
}
note

When re-entering authorized zone, user must be set