How to create a Community Channel using Amity Chat SDK for iOS

Mark Worachote
Amity Developers
Published in
3 min readAug 20, 2023

--

In the digital era, fostering user engagement is a critical aspect of any online platform. One effective way to boost engagement is through the use of community channels for chat. These channels not only facilitate communication but also foster a sense of community among users. In this tutorial, we will delve into how to create these private community channels using the Amity SDK on iOS.

The Amity SDK provides a `ChannelRepository` function that supports the creation of various types of channels, including community channels. These channels serve as a space for group discussions, fostering interaction and engagement among users. The SDK offers two standard ways for creating channels. The first method involves specifying a specific channel ID during the channel creation process. The second method allows for the automatic generation of a unique channel ID.

Prerequisites:

Before we dive into the code, make sure you have the following:

  • The Amity SDK installed and set up in your iOS project.
  • An instance of `AmityCommunityChannelBuilder` and `channelRepository`.
  • If you haven’t already registered for an Amity account, we recommend following our comprehensive step-by-step guide here in the Amity Portal to create your new network.

Initialization and Authentication:

Before creating a community channel, you need to initialize the Amity SDK and authenticate the user. Here is how you can do it:

let client = try! AmityClient(apiKey: "api-key", region: .global)

After initializing the SDK, you need to authenticate the user:

Task { @MainActor in
do {
try await client.login(
userId: "<user-id>",
displayName: "<(optional)-display-name>",
authToken: "<(optional)-auth-token>",
sessionHandler: MySessionHandler()
)
print("login success")
} catch {
print("login failed \(error)")
}
}

Step-by-step guide to create a Community Channel:

Let’s start by creating a community channel. Here is the code snippet to do so:

do {
let builder = AmityCommunityChannelBuilder()
// Optional. If you don't provide channel id, sdk will create a unique channel id for you.
builder.setUserIds(["steve", "john"])
builder.setDisplayName("My Community Channel")
builder.setTags(["ch-comm", "ios-sdk"])
builder.setMetadata(["sdk_type": "ios"])
// Create a channel by passing the builder
let channel = try await channelRepository.createChannel(with: builder)
print("A channel is created: \(channel.channelId)")
} catch {
print("Unable to create a channel: \(error)")
}

In the above code:

1. We start by creating an instance of `AmityCommunityChannelBuilder`.
2. We then set the user IDs of the channel members using the `setUserIds` function. This is optional, and if not provided, the SDK will create a unique channel ID for you.
3. We set the display name of the channel using the `setDisplayName` function. This is the public name of the channel that will be visible to all users.
4. We set the tags for the channel using the `setTags` function. Tags are arbitrary strings that can be used for defining and querying for the channels.
5. We set the metadata for the channel using the `setMetadata` function. Metadata is used to support custom fields.
6. Finally, we create the channel by passing the builder to the `createChannel` function of the `channelRepository`. If the channel creation is successful, the channel ID is printed. If an error occurs, it is caught and printed.

After defining all the relevant parameters, you can add a UI button on your community page where a group chat between all the members of that community can take place.

Final Thoughts:

Creating community channels for chat on iOS using the Amity SDK is a powerful way to boost user engagement. These channels provide a private space for users to interact, share ideas, and build a sense of community. By understanding the different types of channels and how to use the `ChannelRepository` function, you can easily create and customize channels to suit your specific needs. Whether you’re building a social networking app, a live streaming platform, or a private chat application, the Amity SDK provides you with the tools and flexibility to create the perfect channels for your users.

--

--