WuKongIM iOS EasySDK is a lightweight iOS SDK that enables you to add real-time chat functionality to your iOS application in just 5 minutes. This guide will take you through the complete process from installation to sending your first message.
System Requirements: iOS 12.0 or higher, Xcode 12.0 or higher, Swift 5.0 or higher
// 2. Listen for various eventseasySDK.onConnect { result in print("Event: Connected!", result) // Connection successful, can start sending messages DispatchQueue.main.async { self.updateUI(connected: true) }}easySDK.onDisconnect { disconnectInfo in print("Event: Disconnected.", disconnectInfo) print("Disconnect code: \(disconnectInfo.code), reason: \(disconnectInfo.reason)") // Connection lost, update UI status DispatchQueue.main.async { self.updateUI(connected: false) }}easySDK.onMessage { message in print("Event: Message Received", message) // Handle received messages DispatchQueue.main.async { self.displayMessage(message) }}easySDK.onError { error in print("Event: Error Occurred", error.localizedDescription) // Handle errors, may need to update UI or reconnect DispatchQueue.main.async { self.handleError(error) }}// You can add multiple listeners for the same eventeasySDK.onMessage { message in print("Second listener also received message:", message.messageId) // Add different processing logic here}
In some cases, you may need to remove event listeners to avoid memory leaks or duplicate processing. iOS EasySDK provides methods to remove listeners.
Important Reminder: In iOS, removing event listeners requires maintaining references to the listeners. It’s recommended to use class properties to store listener references for later removal.
Built-in Auto Reconnection: iOS EasySDK has built-in intelligent reconnection mechanism, no need to manually implement reconnection logic. The SDK will automatically attempt to reconnect when the connection is lost.
Copy
// Proper connection status listeningeasySDK.onConnect { result in print("Connection successful:", result) // Update UI status, enable sending functionality DispatchQueue.main.async { self.updateConnectionUI(connected: true) }}easySDK.onDisconnect { disconnectInfo in print("Connection lost:", disconnectInfo) print("Disconnect code: \(disconnectInfo.code), reason: \(disconnectInfo.reason)") // Update UI status, disable sending functionality DispatchQueue.main.async { self.updateConnectionUI(connected: false) } // SDK will automatically attempt to reconnect, no manual handling needed}easySDK.onError { error in print("Error occurred:", error) // Handle based on error type DispatchQueue.main.async { switch error { case WuKongError.authFailed: // Authentication failed, need to get new token self.handleAuthError() case WuKongError.networkError: // Network error, show network prompt self.showNetworkError() default: // Other errors self.showGeneralError(error.localizedDescription) } }}