Home
/
Chat
/
iOS

Send your first message

The Chat SDK simplifies chat features into an effortless and straightforward process. To send your first message, do the following steps:

Note: The methods in the following steps are all asynchronous, excluding the initWithApplicationId:. This means that when using asynchronous methods, your client app must receive success callbacks from Sendbird server through completion handlers in order to proceed to the next step. A good way to do this is the nesting of methods: Go to Step 4: Enter the channel to learn more about how to nest the enterChannelWithCompletionHandler: in the getChannelWithUrl:completionHandler: method.


Step 1: Initialize the Chat SDK

To allow the Chat SDK to respond to the connection and state changes in your iOS client app, you first initialize the Chat SDK in the app. Pass in the App ID of the Sendbird application you created earlier in the dashboard for the initialization.

Note: The initWithApplicationId: method of a SBDMain instance must be called once across your iOS client app. We recommend that you initialize the Chat SDK in the application:didFinishLaunchingWithOptions: method of the AppDelegate instance.

Objective-C
Swift
Light Color Skin
Copy
// Initialize SBDMain instance to use APIs in your app.
[SBDMain initWithApplicationId:APP_ID];
Light Color Skin
Copy
// Initialize SBDMain instance to use APIs in your app.
SBDMain.initWithApplicationId(APP_ID)

Step 2: Connect to Sendbird server

Connect a user to Sendbird server using their unique user ID. Any untaken user ID is automatically registered as a new user to your Sendbird application before being connected, while an existing ID is allowed to log in directly.

Note: Go to the Authentication page to learn more about authenticating with an access token.

Objective-C
Swift
Light Color Skin
Copy
// The USER_ID below should be unique to your Sendbird application.
[SBDMain connectWithUserId:USER_ID completionHandler:^(SBDUser * _Nullable user, SBDError * _Nullable error) {
    if (error != nil) {
        // Handle error.
    }

    // The user is connected to Sendbird server.
    ...
}];
Light Color Skin
Copy
// The USER_ID below should be unique to your Sendbird application.
SBDMain.connect(withUserId: USER_ID, completionHandler: { (user, error) in
    guard error == nil else {
        // Handle error.
    }

    // The user is connected to Sendbird server.
    ...
})

Step 3: Create a new open channel

Create an open channel. Once created, all users in your Sendbird application can easily participate the channel.

Note: In a similar fashion, you can create a group channel by inviting users as new members to the channel.

Objective-C
Swift
Light Color Skin
Copy
[SBDOpenChannel createChannelWithCompletionHandler:^(SBDOpenChannel * _Nullable openChannel, SBDError * _Nullable error) {
    if (error != nil) {
        // Handle error.
    }

    // An open channel is successfully created.
    // Through the "openChannel" parameter of the callback method,
    // you can get the open channel's data from the result object that Sendbird server has passed to the callback method.
    ...
}];
Light Color Skin
Copy
SBDOpenChannel.createChannel(completionHandler: { (openChannel, error) in
    guard error == nil else {
        // Handle error.
    }

    // An open channel is successfully created.
    // Through the "openChannel" parameter of the callback method,
    // you can get the open channel's data from the result object that Sendbird server has passed to the callback method.
    ...
})

Step 4: Enter the channel

Enter the channel to send and receive messages.

Objective-C
Swift
Light Color Skin
Copy
// The CHANNEL_URL below can be retrieved using the openChannel.channelUrl.
[SBDOpenChannel getChannelWithUrl:CHANNEL_URL completionHandler:^(SBDOpenChannel * _Nullable openChannel, SBDError * _Nullable error) {
    if (error != nil) {
        // Handle error.
    }

    // Call the instance method of the result object in the "openChannel" parameter of the callback method.
    [openChannel enterChannelWithCompletionHandler:^(SBDError * _Nullable error) {
        if (error != nil) {
            // Handle error.
        }

        // The current user successfully enters the open channel,
        // and can chat with other users in the channel by using APIs.
        ...
    }];
}];
Light Color Skin
Copy
// The CHANNEL_URL below can be retrieved using the openChannel.channelUrl.
SBDOpenChannel.getWithUrl(CHANNEL_URL, completionHandler: { (openChannel, error) in
    guard error == nil else {
        // Handle error.
    }

    // Call the instance method of the result object in the "openChannel" parameter of the callback method.
    openChannel?.enter(completionHandler: { (error) in
        guard error == nil else {
            // Handle error.
        }

        // The current user successfully enters the open channel as a participant,
        // and can chat with other users in the channel by using APIs.
        ...
    })
})

Step 5: Send a message to the channel

Finally, send a message to the channel. There are three types: a user message is a plain text, a file message is a binary file, such as an image or PDF, and an admin message is a plain text sent through the dashboard or Platform API.

Objective-C
Swift
Light Color Skin
Copy
[openChannel sendUserMessage:MESSAGE data:DATA customType:CUSTOM_TYPE completionHandler:^(SBDUserMessage * _Nullable userMessage, SBDError * _Nullable error) {
    if (error != nil) {
        // Handle error.
    }

    // The message is successfully sent to the channel.
    // The current user can receive messages from other users through the channel:didReceiveMessage: method of an event delegate.
    ...
}];
Light Color Skin
Copy
openChannel?.sendUserMessage(MESSAGE, data: DATA, customType: CUSTOM_TYPE, completionHandler: { (message, error) in
    guard error == nil else {
        // Handle error.
    }

    // The message is successfully sent to the channel.
    // The current user can receive messages from other users through the didReceiveMessage() method of an event delegate.
})