Skip to main content

Send your first message

This guide walks you through the minimum steps to integrate the Chat SDK into an iOS app: install the SDK, initialize it, connect a user, and send a message.

Requirements

  • Xcode 14.0 or later
  • iOS 13.0 or later
  • CocoaPods 1.10.0 or later (if using CocoaPods)
  • Swift 5.0 or later (if using Swift Package Manager)

Before you start

  1. Register a developer account on the Nexconn Console. Registration automatically creates an app in the development environment.
  2. On the Key Management page, copy the App Key for the development environment.
tip

Each app has separate App Keys for development and production. Data is isolated between the two environments. Switch to the production App Key before releasing your app.

Step 1: Install the SDK

Add the following to your Podfile:

ruby
pod 'NexconnChat/Chat', '~> x.y.z'

Then run:

shell
pod install

Open the generated .xcworkspace file in Xcode.

Step 2: Initialize the SDK

Import the SDK and call NCEngine.initialize(_:) once in application:didFinishLaunchingWithOptions:, before using any other SDK features.

swift
import NexconnChatSDK

let params = InitParams(appKey: "Your_AppKey")
NCEngine.initialize(params)

If your app connects to a specific data center, set the area code:

swift
let params = InitParams(appKey: "Your_AppKey")
params.areaCode = .sg
NCEngine.initialize(params)
info

Find your App Key and data center region in the Nexconn Console under Service Management.

For all initialization options, see Initialize the SDK.

Step 3: Get an access token

An access token is a per-user authentication credential. Your app server must request it from the Nexconn Server API and pass it to the client.

For quick testing, use the API Debugger in the console to call the Register User endpoint and get a token for a test user.

warning

Never call the Server API to obtain tokens directly from client code. This requires embedding your App Secret in the app, which exposes it to decompilation. Always fetch tokens through your own app server.

Step 4: Connect to the server

Register a connection status handler before connecting, then call NCEngine.connect(params:databaseOpenedHandler:completionHandler:) with the user's access token.

swift
import NexconnChatSDK

final class MyConnectionHandler: NSObject, ConnectionStatusHandler {
func onConnectionStatusChanged(_ status: ConnectionStatus) {
print("Connection status: \(status.rawValue)")
}
}

let connectionHandler = MyConnectionHandler()
NCEngine.addConnectionStatusHandler(identifier: "MyApp", handler: connectionHandler)

let connectParams = ConnectParams(token: "user-access-token")
NCEngine.connect(
params: connectParams,
databaseOpenedHandler: { isRecreated, error in
guard error == nil else { return }
// Local database is ready — safe to show main UI
if isRecreated {
// Local data was cleared
}
},
completionHandler: { userId, error in
if error == nil {
// Connection successful
} else if error?.errorCode == 15 {
// Token expired — fetch a new one from your server and reconnect
}
}
)

The SDK has a built-in reconnection mechanism. Call connect only once per app lifecycle. For details and error codes, see Connect to the server.

Step 5: Send a message

Send a text message to another user:

swift
guard let channel = DirectChannel(channelId: "recipient-user-id") else {
return
}

let textContent = TextMessage(text: "Hello!")
let params = SendMessageParams(content: textContent)

channel.sendMessage(
params: params,
attachedHandler: { message in
// Saved to local database — update UI here
},
completionHandler: { message, error in
if let error {
print("Send failed: \(error.localizedDescription)")
} else {
print("Sent successfully, messageId: \(message?.messageId ?? "")")
}
}
)

What's next

TopicDescription
Import the SDKAll installation methods in detail
Initialize the SDKArea codes and advanced configuration options
Connect to the serverConnection error codes and usage patterns
Monitor connection statusHandle network changes in your UI
Send messagesImages, files, voice, and more