App lifecycle integration
Where in the app lifecycle to call SDK methods.
Overall interaction diagram
Integration stages
| # | Stage | SDK method | When to call | Details |
|---|---|---|---|---|
| 1 | SDK initialization | init(providerUid:appMarker:chatConfig:loggerConfig:) | application(_:didFinishLaunchingWithOptions:) | SDK initialization |
| 2 | APNS token registration | ChatCenterUISDK.setDeviceToken(_:) | application(_:didRegisterForRemoteNotificationsWithDeviceToken:) (asynchronous system callback, independent of step 1) | Notifications |
| 3 | User authorization | authorize(user:auth:) | After the user enters the authorized zone of the app | User management |
| 4 | Opening the chat | try getChat(userInfo:) → present / push | On user action (e.g., tap on "Support" button). Throws ChatCenterUIError.userNotAuthorized if called before authorize | Displaying the chat |
| 5 | User logout | try logout() or deauthorizeUser() | logout() — on full account logout (throws ChatCenterUIError.userNotAuthorized if the user is not authorized). deauthorizeUser() — when leaving the authorized zone (e.g., to a PIN code screen); does not throw errors | User management |
Thread safety
The SDK does not guarantee thread safety — call all methods from the main thread. Calling from a background thread may corrupt the SDK's internal state or trigger UIKit warnings.
| Method | Calling thread | Notes |
|---|---|---|
init(...) | Main thread | Registers observers for UIApplication notifications (didBecomeActive, didEnterBackground) and for an internal APNS token change notification — the SDK starts reacting to the app lifecycle and token changes immediately after init |
setDeviceToken(_:) | Main thread | Static method. Can be called before creating a ChatCenterUISDK instance |
authorize(user:auth:) | Main thread | Initiates REST requests for settings and history. WebSocket opens on the first viewDidAppear of the chat controller; it may open earlier if the server returns registerAtFirstStart |
getChat(userInfo:) | Main thread | Returns a UIViewController — UIKit requires the main thread. Throws ChatCenterUIError.userNotAuthorized |
send(message:) | Main thread | Throws ChatCenterUISendMessageError; see SDK errors |
getUnreadMessagesCount(completion:) | Main thread | Throws ChatCenterUIError.userNotAuthorized. The completion is called on the main thread |
logout() | Main thread | Throws ChatCenterUIError.userNotAuthorized if the user is not authorized or their identifier is empty |
deauthorizeUser() | Main thread | Does not throw errors |
prefill(message:) | Main thread | Stored until the first chat opening; an empty string clears it |
handleNotification(userInfo:) | Main thread | Throws ChatCenterUIError.chatNotOpened if the chat is not open. In the current version, performs no visible actions when the chat is open |
ChatCenterUISDKDelegate methods | Main thread (de facto) | In the current version, all delegate methods (didChangeUnreadMessages, didReceiveNetwork, didLog, chatCenterUI(chatCenter:didOpen url:) -> Bool — the in-chat link tap handler) are actually delivered on the main thread, but this is not guaranteed by contract. For reliability, wrap UI updates in DispatchQueue.main.async |