Monitor connection status
Use the connection status APIs to react to reconnects, offline kicks, token failures, and other connection-state changes.
Listen for connection status changes
Register a ConnectionStatusHandler after SDK initialization and before connecting.
- Swift
- Objective-C
swift
import NexconnChatSDK
NCEngine.addConnectionStatusHandler(identifier: "MyConnectionHandler", handler: self)
Objective C
[NCEngine addConnectionStatusHandlerWithIdentifier:@"MyConnectionHandler" handler:self];
Implement the protocol. In the current SDK, the callback receives a ConnectionStatusChangedEvent wrapper, and the actual connection state is on event.status.
- Swift
- Objective-C
swift
import NexconnChatSDK
final class ConnectionMonitor: NSObject, ConnectionStatusHandler {
func onConnectionStatusChanged(_ event: ConnectionStatusChangedEvent) {
switch event.status {
case .connected:
print("Connected successfully.")
case .networkUnavailable:
print("Network unavailable. The SDK will reconnect automatically.")
case .tokenIncorrect:
// Request a new token from your server and reconnect.
break
case .kickedOfflineByOtherClient:
print("Kicked offline by another device.")
default:
print("Connection status changed: \(event.status.rawValue)")
}
}
}
Objective C
- (void)onConnectionStatusChanged:(NCConnectionStatusChangedEvent *)event {
switch (event.status) {
case NCConnectionStatusConnected:
NSLog(@"Connected successfully.");
break;
case NCConnectionStatusNetworkUnavailable:
NSLog(@"Network unavailable. The SDK will reconnect automatically.");
break;
case NCConnectionStatusTokenIncorrect:
// Request a new token from your server and reconnect
break;
case NCConnectionStatusKickedOfflineByOtherClient:
NSLog(@"Kicked offline by another device.");
break;
default:
NSLog(@"Connection status changed: %ld", (long)event.status);
break;
}
}
Remove the handler when it is no longer needed:
- Swift
- Objective-C
swift
import NexconnChatSDK
NCEngine.removeConnectionStatusHandler(forIdentifier: "MyConnectionHandler")
Objective C
[NCEngine removeConnectionStatusHandlerForIdentifier:@"MyConnectionHandler"];
Get the current connection status
Call getConnectionStatus() at any time to retrieve the current IM connection state:
- Swift
- Objective-C
swift
import NexconnChatSDK
let status = NCEngine.getConnectionStatus()
Objective C
NCConnectionStatus status = [NCEngine getConnectionStatus];
Handle status in the connect callback
When establishing a connection, handle NCError.errorCode in the error callback:
- Swift
- Objective-C
swift
import NexconnChatSDK
let params = ConnectParams(token: "your_token")
params.timeout = 30
NCEngine.connect(
params: params,
databaseOpenedHandler: { _, error in
if let error {
print("Database open failed: \(error.localizedDescription)")
}
},
completionHandler: { userId, error in
guard error == nil else {
switch error!.errorCode {
case 15:
print("Token is invalid or expired.")
case 14:
print("Connection timed out.")
default:
print("Connection failed: \(error!.errorCode)")
}
return
}
print("Connected successfully, userId: \(userId ?? "")")
}
)
Objective C
NCConnectParams *params = [[NCConnectParams alloc] initWithToken:@"your_token"];
params.timeout = 30;
[NCEngine connectWithParams:params
databaseOpenedHandler:^(BOOL isRecreated, NCError * _Nullable error) {
if (error == nil && isRecreated) {
NSLog(@"Local data was recreated.");
}
}
completionHandler:^(NSString * _Nullable userId, NCError * _Nullable error) {
if (error == nil) {
NSLog(@"Connected successfully, userId: %@", userId);
} else {
switch (error.errorCode) {
case 15:
// Request a new token and reconnect
break;
case 14:
// Connection timed out
break;
default:
NSLog(@"Connection failed: %ld", (long)error.errorCode);
break;
}
}
}];