Monitor connection status
The Chat UI SDK provides the NCChatUIConnectionStatusDelegate protocol to monitor IM connection status changes. By listening to these changes, your app can handle different business logic or display status notifications.
Add or remove delegate listeners
You should set up connection status listeners after initializing the Chat UI SDK with your AppKey but before connecting to IM. Multiple listeners are supported.
To prevent memory leaks, remove the delegate when you no longer need to monitor connection status.
Assign the delegate to a singleton object like AppDelegate to ensure it remains active throughout the app lifecycle.
/// Add delegate
[[NCChatUI shared] addConnectionStatusDelegate:self];
/// Remove delegate
[[NCChatUI shared] removeConnectionStatusDelegate:self];
Connection status change protocol
The NCChatUIConnectionStatusDelegate protocol is defined as:
@protocol NCChatUIConnectionStatusDelegate <NSObject>
- (void)onNCChatUIConnectionStatusChanged:(NCConnectionStatus)status;
@end
When the connection status changes, the SDK calls - (void)onNCChatUIConnectionStatusChanged:(NCConnectionStatus)status; to notify listeners of the current status. The NCConnectionStatus enum defines possible connection states. The following table explains key status codes your app should handle.
| Status code | Value | Description |
|---|---|---|
NCConnectionStatusKickedOfflineByOtherClient | 6 | Current user logged in from another device, kicking this device offline |
NCConnectionStatusTimeout | 14 | Auto-connection timed out. The SDK won't retry - your app should handle the timeout and manually reconnect |
NCConnectionStatusTokenIncorrect | 15 | Invalid access token. Typically occurs when the client SDK and app server use different App Keys, or when the token expired |
NCConnectionStatusDisconnException | 16 | Connection to server lost due to user ban |
NCConnectionStatusProxyUnavailable | 17 | Proxy service unavailable |
NCConnectionStatusUserAbandon | 19 | User account deactivated |