# Chat with iOS and Swift

> \[!CAUTION]
>
> Programmable Chat has been deprecated and is no longer supported. Instead, we'll be focusing on the next generation of chat: Twilio Conversations. Find out more about the [EOL process here](https://www.twilio.com/en-us/changelog/programmable-chat-end-of-life-notice).
>
> If you're starting a new project, please visit the [Conversations Docs](/docs/conversations) to begin. If you've already built on Programmable Chat, please visit our [Migration Guide](/docs/conversations/migrating-chat-conversations) to learn about how to switch.

> \[!WARNING]
>
> As the Programmable Chat API is set to [sunset in 2022](https://www.twilio.com/en-us/changelog/programmable-chat-end-of-life-notice), we will no longer maintain these chat tutorials.
>
> Please see our [Conversations API QuickStart](/docs/conversations/quickstart) to start building robust virtual spaces for conversation.

Ready to implement a chat application using Twilio Chat Client? Here is how it works at a high level:

1. [Programmable Chat](/en-us/messaging/conversations-api) is the core product we'll be using to handle all the chat functionality.
2. We use a server side app to generate [user access tokens](/docs/chat/identity#server-create-an-access-token) which contains all your Twilio account information. The Programmable Chat Client uses this token to connect with the API.

*[Properati built a web and mobile messaging app to help real estate buyers and sellers connect in real time. Learn more here.](https://customers.twilio.com/1234/properati/)*

For your convenience, we consolidated the source code for this tutorial in a single [GitHub repository](https://github.com/TwilioDevEd/twiliochat-swift). Feel free to clone it and tweak as required.

## Initialize the Programmable Chat Client

The only thing you need to create a client is an access token. This token holds information about your Twilio account and Programmable Chat API keys. We have created a web version of Twilio chat in different languages. You can use any of these to generate the token:

* [PHP - Laravel](https://github.com/TwilioDevEd/twiliochat-laravel)
* [C# - .NET MVC](https://github.com/TwilioDevEd/twiliochat-csharp)
* [Java - Servlets](https://github.com/TwilioDevEd/twiliochat-servlets)
* [JS - Node](https://github.com/TwilioDevEd/twiliochat-node)

> \[!NOTE]
>
> You must set up your access token URL in the `Keys.plist` file in the `resources` folder. The default URL is `http://localhost:8000/token`. You may need to change this. For instance, if you set up the Node.js version of the chat server, set this URL to `http://localhost:3000/token`.

```swift title="Fetch Access Token" description="twiliochat/MessagingManager.swift"
// !mark(100,101,102,103,104,105,106,107,108,109,110,111,112,113,114,115,116,117,118,119,120,121,122,123,124,125,126,127,128,129,130,131,132,133,134,135,97,98,99)
import UIKit

class MessagingManager: NSObject {

    static let _sharedManager = MessagingManager()

    var client:TwilioChatClient?
    var delegate:ChannelManager?
    var connected = false

    var userIdentity:String {
        return SessionManager.getUsername()
    }

    var hasIdentity: Bool {
        return SessionManager.isLoggedIn()
    }

    override init() {
        super.init()
        delegate = ChannelManager.sharedManager
    }

    class func sharedManager() -> MessagingManager {
        return _sharedManager
    }

    func presentRootViewController() {
        if (!hasIdentity) {
            presentViewControllerByName(viewController: "LoginViewController")
            return
        }

        if (!connected) {
            connectClientWithCompletion { success, error in
                print("Delegate method will load views when sync is complete")
                if (!success || error != nil) {
                    DispatchQueue.main.async {
                        self.presentViewControllerByName(viewController: "LoginViewController")
                    }
                }
            }
            return
        }

        presentViewControllerByName(viewController: "RevealViewController")
    }

    func presentViewControllerByName(viewController: String) {
        presentViewController(controller: storyBoardWithName(name: "Main").instantiateViewController(withIdentifier: viewController))
    }

    func presentLaunchScreen() {
        presentViewController(controller: storyBoardWithName(name: "LaunchScreen").instantiateInitialViewController()!)
    }

    func presentViewController(controller: UIViewController) {
        let window = UIApplication.shared.delegate!.window!!
        window.rootViewController = controller
    }

    func storyBoardWithName(name:String) -> UIStoryboard {
        return UIStoryboard(name:name, bundle: Bundle.main)
    }

    // MARK: User and session management

    func loginWithUsername(username: String,
                           completion: @escaping (Bool, NSError?) -> Void) {
        SessionManager.loginWithUsername(username: username)
        connectClientWithCompletion(completion: completion)
    }

    func logout() {
        SessionManager.logout()
        DispatchQueue.global(qos: .userInitiated).async {
            self.client?.shutdown()
            self.client = nil
        }
        self.connected = false
    }

    // MARK: Twilio client

    func loadGeneralChatRoomWithCompletion(completion:@escaping (Bool, NSError?) -> Void) {
        ChannelManager.sharedManager.joinGeneralChatRoomWithCompletion { succeeded in
            if succeeded {
                completion(succeeded, nil)
            }
            else {
                let error = self.errorWithDescription(description: "Could not join General channel", code: 300)
                completion(succeeded, error)
            }
        }
    }

    func connectClientWithCompletion(completion: @escaping (Bool, NSError?) -> Void) {
        if (client != nil) {
            logout()
        }

        requestTokenWithCompletion { succeeded, token in
            if let token = token, succeeded {
                self.initializeClientWithToken(token: token)
                completion(succeeded, nil)
            }
            else {
                let error = self.errorWithDescription(description: "Could not get access token", code:301)
                completion(succeeded, error)
            }
        }
    }

    func initializeClientWithToken(token: String) {
        DispatchQueue.main.async {
            UIApplication.shared.isNetworkActivityIndicatorVisible = true
        }
        TwilioChatClient.chatClient(withToken: token, properties: nil, delegate: self) { [weak self] result, chatClient in
            guard (result.isSuccessful()) else { return }

            UIApplication.shared.isNetworkActivityIndicatorVisible = true
            self?.connected = true
            self?.client = chatClient
        }
    }

    func requestTokenWithCompletion(completion:@escaping (Bool, String?) -> Void) {
        if let device = UIDevice.current.identifierForVendor?.uuidString {
            TokenRequestHandler.fetchToken(params: ["device": device, "identity":SessionManager.getUsername()]) {response,error in
                var token: String?
                token = response["token"] as? String
                completion(token != nil, token)
            }
        }
    }

    func errorWithDescription(description: String, code: Int) -> NSError {
        let userInfo = [NSLocalizedDescriptionKey : description]
        return NSError(domain: "app", code: code, userInfo: userInfo)
    }
}

// MARK: - TwilioChatClientDelegate
extension MessagingManager : TwilioChatClientDelegate {
    func chatClient(_ client: TwilioChatClient, channelAdded channel: TCHChannel) {
        self.delegate?.chatClient(client, channelAdded: channel)
    }

    func chatClient(_ client: TwilioChatClient, channel: TCHChannel, updated: TCHChannelUpdate) {
        self.delegate?.chatClient(client, channel: channel, updated: updated)
    }

    func chatClient(_ client: TwilioChatClient, channelDeleted channel: TCHChannel) {
        self.delegate?.chatClient(client, channelDeleted: channel)
    }

    func chatClient(_ client: TwilioChatClient, synchronizationStatusUpdated status: TCHClientSynchronizationStatus) {
        if status == TCHClientSynchronizationStatus.completed {
            UIApplication.shared.isNetworkActivityIndicatorVisible = false
            ChannelManager.sharedManager.channelsList = client.channelsList()
            ChannelManager.sharedManager.populateChannelDescriptors()
            loadGeneralChatRoomWithCompletion { success, error in
                if success {
                    self.presentRootViewController()
                }
            }
        }
        self.delegate?.chatClient(client, synchronizationStatusUpdated: status)
    }

    func chatClientTokenWillExpire(_ client: TwilioChatClient) {
        requestTokenWithCompletion { succeeded, token in
            if (succeeded) {
                client.updateToken(token!)
            }
            else {
                print("Error while trying to get new access token")
            }
        }
    }

    func chatClientTokenExpired(_ client: TwilioChatClient) {
        requestTokenWithCompletion { succeeded, token in
            if (succeeded) {
                client.updateToken(token!)
            }
            else {
                print("Error while trying to get new access token")
            }
        }
    }
}

```

Now it's time to synchronize your Twilio client.

## Synchronize the Programmable Chat Client

The `synchronizationStatusChanged` [delegate](https://media.twiliocdn.com/sdk/ios/chat/releases/0.16.0/docs/Protocols/TwilioChatClientDelegate.html) method will allow us to know when the client has loaded all the required information. You can change the default initialization values for the client using a [TwilioChatClientProperties](https://media.twiliocdn.com/sdk/ios/chat/releases/0.16.0/docs/Classes/TwilioChatClientProperties.html) instance as the `options` parameter in the previews step.

We need the client to be synchronized before trying to get the channel list (next step). Otherwise, calling [client.channelsList()](https://media.twiliocdn.com/sdk/ios/chat/releases/0.16.0/docs/Classes/TwilioChatClient.html#//api/name/channelsList) will return `nil`.

```swift title="Synchronize the Chat Client" description="twiliochat/MessagingManager.swift"
// !mark(156,157,158,159,160,161,162,163,164,165,166,167,168,169,170)
import UIKit

class MessagingManager: NSObject {

    static let _sharedManager = MessagingManager()

    var client:TwilioChatClient?
    var delegate:ChannelManager?
    var connected = false

    var userIdentity:String {
        return SessionManager.getUsername()
    }

    var hasIdentity: Bool {
        return SessionManager.isLoggedIn()
    }

    override init() {
        super.init()
        delegate = ChannelManager.sharedManager
    }

    class func sharedManager() -> MessagingManager {
        return _sharedManager
    }

    func presentRootViewController() {
        if (!hasIdentity) {
            presentViewControllerByName(viewController: "LoginViewController")
            return
        }

        if (!connected) {
            connectClientWithCompletion { success, error in
                print("Delegate method will load views when sync is complete")
                if (!success || error != nil) {
                    DispatchQueue.main.async {
                        self.presentViewControllerByName(viewController: "LoginViewController")
                    }
                }
            }
            return
        }

        presentViewControllerByName(viewController: "RevealViewController")
    }

    func presentViewControllerByName(viewController: String) {
        presentViewController(controller: storyBoardWithName(name: "Main").instantiateViewController(withIdentifier: viewController))
    }

    func presentLaunchScreen() {
        presentViewController(controller: storyBoardWithName(name: "LaunchScreen").instantiateInitialViewController()!)
    }

    func presentViewController(controller: UIViewController) {
        let window = UIApplication.shared.delegate!.window!!
        window.rootViewController = controller
    }

    func storyBoardWithName(name:String) -> UIStoryboard {
        return UIStoryboard(name:name, bundle: Bundle.main)
    }

    // MARK: User and session management

    func loginWithUsername(username: String,
                           completion: @escaping (Bool, NSError?) -> Void) {
        SessionManager.loginWithUsername(username: username)
        connectClientWithCompletion(completion: completion)
    }

    func logout() {
        SessionManager.logout()
        DispatchQueue.global(qos: .userInitiated).async {
            self.client?.shutdown()
            self.client = nil
        }
        self.connected = false
    }

    // MARK: Twilio client

    func loadGeneralChatRoomWithCompletion(completion:@escaping (Bool, NSError?) -> Void) {
        ChannelManager.sharedManager.joinGeneralChatRoomWithCompletion { succeeded in
            if succeeded {
                completion(succeeded, nil)
            }
            else {
                let error = self.errorWithDescription(description: "Could not join General channel", code: 300)
                completion(succeeded, error)
            }
        }
    }

    func connectClientWithCompletion(completion: @escaping (Bool, NSError?) -> Void) {
        if (client != nil) {
            logout()
        }

        requestTokenWithCompletion { succeeded, token in
            if let token = token, succeeded {
                self.initializeClientWithToken(token: token)
                completion(succeeded, nil)
            }
            else {
                let error = self.errorWithDescription(description: "Could not get access token", code:301)
                completion(succeeded, error)
            }
        }
    }

    func initializeClientWithToken(token: String) {
        DispatchQueue.main.async {
            UIApplication.shared.isNetworkActivityIndicatorVisible = true
        }
        TwilioChatClient.chatClient(withToken: token, properties: nil, delegate: self) { [weak self] result, chatClient in
            guard (result.isSuccessful()) else { return }

            UIApplication.shared.isNetworkActivityIndicatorVisible = true
            self?.connected = true
            self?.client = chatClient
        }
    }

    func requestTokenWithCompletion(completion:@escaping (Bool, String?) -> Void) {
        if let device = UIDevice.current.identifierForVendor?.uuidString {
            TokenRequestHandler.fetchToken(params: ["device": device, "identity":SessionManager.getUsername()]) {response,error in
                var token: String?
                token = response["token"] as? String
                completion(token != nil, token)
            }
        }
    }

    func errorWithDescription(description: String, code: Int) -> NSError {
        let userInfo = [NSLocalizedDescriptionKey : description]
        return NSError(domain: "app", code: code, userInfo: userInfo)
    }
}

// MARK: - TwilioChatClientDelegate
extension MessagingManager : TwilioChatClientDelegate {
    func chatClient(_ client: TwilioChatClient, channelAdded channel: TCHChannel) {
        self.delegate?.chatClient(client, channelAdded: channel)
    }

    func chatClient(_ client: TwilioChatClient, channel: TCHChannel, updated: TCHChannelUpdate) {
        self.delegate?.chatClient(client, channel: channel, updated: updated)
    }

    func chatClient(_ client: TwilioChatClient, channelDeleted channel: TCHChannel) {
        self.delegate?.chatClient(client, channelDeleted: channel)
    }

    func chatClient(_ client: TwilioChatClient, synchronizationStatusUpdated status: TCHClientSynchronizationStatus) {
        if status == TCHClientSynchronizationStatus.completed {
            UIApplication.shared.isNetworkActivityIndicatorVisible = false
            ChannelManager.sharedManager.channelsList = client.channelsList()
            ChannelManager.sharedManager.populateChannelDescriptors()
            loadGeneralChatRoomWithCompletion { success, error in
                if success {
                    self.presentRootViewController()
                }
            }
        }
        self.delegate?.chatClient(client, synchronizationStatusUpdated: status)
    }

    func chatClientTokenWillExpire(_ client: TwilioChatClient) {
        requestTokenWithCompletion { succeeded, token in
            if (succeeded) {
                client.updateToken(token!)
            }
            else {
                print("Error while trying to get new access token")
            }
        }
    }

    func chatClientTokenExpired(_ client: TwilioChatClient) {
        requestTokenWithCompletion { succeeded, token in
            if (succeeded) {
                client.updateToken(token!)
            }
            else {
                print("Error while trying to get new access token")
            }
        }
    }
}

```

We've initialized the Programmable Chat Client, now let's get a list of channels.

## Get the Channel Descriptor List

Our `ChannelManager` class takes care of everything related to channels. In the previous step, we waited for the client to synchronize channel information, and assigned an instance of [TCHChannels](https://media.twiliocdn.com/sdk/ios/chat/releases/0.16.0/docs/Classes/TCHChannels.html) to our `ChannelManager`.

Now we will get a list of light-weight channel descriptors to use for the list of channels in our application. We combine the channels the user has subscribed to (both public and private) with the list of publicly available channels. We do need to merge this list, and avoid adding duplicates. We also sort the channel list here alphabetically by the friendly name.

```swift title="Get Channel List" description="twiliochat/ChannelManager.swift"
// !mark(100,101,102,103,104,105,106,107,108,109,110,111,112,113,114,115,116,117,118,119,120,121,122,123,124,125,126,79,80,81,82,83,84,85,86,87,88,89,90,91,92,93,94,95,96,97,98,99)
import UIKit

protocol ChannelManagerDelegate {
    func reloadChannelDescriptorList()
}

class ChannelManager: NSObject {
    static let sharedManager = ChannelManager()

    static let defaultChannelUniqueName = "general"
    static let defaultChannelName = "General Channel"

    var delegate:ChannelManagerDelegate?

    var channelsList:TCHChannels?
    var channelDescriptors:NSOrderedSet?
    var generalChannel:TCHChannel!

    override init() {
        super.init()
        channelDescriptors = NSMutableOrderedSet()
    }

    // MARK: - General channel

    func joinGeneralChatRoomWithCompletion(completion: @escaping (Bool) -> Void) {

        let uniqueName = ChannelManager.defaultChannelUniqueName
        if let channelsList = self.channelsList {
            channelsList.channel(withSidOrUniqueName: uniqueName) { result, channel in
                self.generalChannel = channel

                if self.generalChannel != nil {
                    self.joinGeneralChatRoomWithUniqueName(name: nil, completion: completion)
                } else {
                    self.createGeneralChatRoomWithCompletion { succeeded in
                        if (succeeded) {
                            self.joinGeneralChatRoomWithUniqueName(name: uniqueName, completion: completion)
                            return
                        }

                        completion(false)
                    }
                }
            }
        }
    }

    func joinGeneralChatRoomWithUniqueName(name: String?, completion: @escaping (Bool) -> Void) {
        generalChannel.join { result in
            if ((result.isSuccessful()) && name != nil) {
                self.setGeneralChatRoomUniqueNameWithCompletion(completion: completion)
                return
            }
            completion((result.isSuccessful()))
        }
    }

    func createGeneralChatRoomWithCompletion(completion: @escaping (Bool) -> Void) {
        let channelName = ChannelManager.defaultChannelName
        let options = [
            TCHChannelOptionFriendlyName: channelName,
            TCHChannelOptionType: TCHChannelType.public.rawValue
            ] as [String : Any]
        channelsList!.createChannel(options: options) { result, channel in
            if (result.isSuccessful()) {
                self.generalChannel = channel
            }
            completion((result.isSuccessful()))
        }
    }

    func setGeneralChatRoomUniqueNameWithCompletion(completion:@escaping (Bool) -> Void) {
        generalChannel.setUniqueName(ChannelManager.defaultChannelUniqueName) { result in
            completion((result.isSuccessful()))
        }
    }

    // MARK: - Populate channel Descriptors

    func populateChannelDescriptors() {

        channelsList?.userChannelDescriptors { result, paginator in
            guard let paginator = paginator else {
                return
            }

            let newChannelDescriptors = NSMutableOrderedSet()
            newChannelDescriptors.addObjects(from: paginator.items())
            self.channelsList?.publicChannelDescriptors { result, paginator in
                guard let paginator = paginator else {
                    return
                }

                // de-dupe channel list
                let channelIds = NSMutableSet()
                for descriptor in newChannelDescriptors {
                    if let descriptor = descriptor as? TCHChannelDescriptor {
                        if let sid = descriptor.sid {
                            channelIds.add(sid)
                        }
                    }
                }
                for descriptor in paginator.items() {
                    if let sid = descriptor.sid {
                        if !channelIds.contains(sid) {
                            channelIds.add(sid)
                            newChannelDescriptors.add(descriptor)
                        }
                    }
                }


                // sort the descriptors
                let sortSelector = #selector(NSString.localizedCaseInsensitiveCompare(_:))
                let descriptor = NSSortDescriptor(key: "friendlyName", ascending: true, selector: sortSelector)
                newChannelDescriptors.sort(using: [descriptor])

                self.channelDescriptors = newChannelDescriptors

                if let delegate = self.delegate {
                    delegate.reloadChannelDescriptorList()
                }
            }
        }
    }


    // MARK: - Create channel

    func createChannelWithName(name: String, completion: @escaping (Bool, TCHChannel?) -> Void) {
        if (name == ChannelManager.defaultChannelName) {
            completion(false, nil)
            return
        }

        let channelOptions = [
            TCHChannelOptionFriendlyName: name,
            TCHChannelOptionType: TCHChannelType.public.rawValue
        ] as [String : Any]
        UIApplication.shared.isNetworkActivityIndicatorVisible = true;
        self.channelsList?.createChannel(options: channelOptions) { result, channel in
            UIApplication.shared.isNetworkActivityIndicatorVisible = false
            completion((result.isSuccessful()), channel)
        }
    }
}

// MARK: - TwilioChatClientDelegate
extension ChannelManager : TwilioChatClientDelegate {
    func chatClient(_ client: TwilioChatClient, channelAdded channel: TCHChannel) {
        DispatchQueue.main.async {
            self.populateChannelDescriptors()
        }
    }

    func chatClient(_ client: TwilioChatClient, channel: TCHChannel, updated: TCHChannelUpdate) {
        DispatchQueue.main.async {
            self.delegate?.reloadChannelDescriptorList()
        }
    }

    func chatClient(_ client: TwilioChatClient, channelDeleted channel: TCHChannel) {
        DispatchQueue.main.async {
            self.populateChannelDescriptors()
        }

    }

    func chatClient(_ client: TwilioChatClient, synchronizationStatusUpdated status: TCHClientSynchronizationStatus) {
    }
}

```

Let's see how we can listen to events from the chat client so we can update our app's state.

## Listen to Client Events

The Programmable Chat Client will trigger events such as [`channelAdded`](https://media.twiliocdn.com/sdk/ios/chat/releases/0.16.0/docs/Protocols/TwilioChatClientDelegate.html) or `channelDeleted` on our application. Given the creation or deletion of a channel, we'll reload the channel list in the reveal controller. If a channel is deleted and we were currently joined to that channel, the application will automatically join the general channel.

`ChannelManager` is a `TwilioChatClientDelegate`. In this class we implement the delegate methods, but we also allow `MenuViewController` class to be a delegate of ChannelManager, so it can listen to client events too.

```swift title="Listen for Client Events" description="twiliochat/ChannelManager.swift"
// !mark(122,123,124,125,126,127,128,129,130,131,132,133,134,135,136,137,138,139,140,141,142,143,144,145,146,147,148,149)
import UIKit

protocol ChannelManagerDelegate {
    func reloadChannelDescriptorList()
}

class ChannelManager: NSObject {
    static let sharedManager = ChannelManager()

    static let defaultChannelUniqueName = "general"
    static let defaultChannelName = "General Channel"

    var delegate:ChannelManagerDelegate?

    var channelsList:TCHChannels?
    var channelDescriptors:NSOrderedSet?
    var generalChannel:TCHChannel!

    override init() {
        super.init()
        channelDescriptors = NSMutableOrderedSet()
    }

    // MARK: - General channel

    func joinGeneralChatRoomWithCompletion(completion: @escaping (Bool) -> Void) {

        let uniqueName = ChannelManager.defaultChannelUniqueName
        if let channelsList = self.channelsList {
            channelsList.channel(withSidOrUniqueName: uniqueName) { result, channel in
                self.generalChannel = channel

                if self.generalChannel != nil {
                    self.joinGeneralChatRoomWithUniqueName(name: nil, completion: completion)
                } else {
                    self.createGeneralChatRoomWithCompletion { succeeded in
                        if (succeeded) {
                            self.joinGeneralChatRoomWithUniqueName(name: uniqueName, completion: completion)
                            return
                        }

                        completion(false)
                    }
                }
            }
        }
    }

    func joinGeneralChatRoomWithUniqueName(name: String?, completion: @escaping (Bool) -> Void) {
        generalChannel.join { result in
            if ((result.isSuccessful()) && name != nil) {
                self.setGeneralChatRoomUniqueNameWithCompletion(completion: completion)
                return
            }
            completion((result.isSuccessful()))
        }
    }

    func createGeneralChatRoomWithCompletion(completion: @escaping (Bool) -> Void) {
        let channelName = ChannelManager.defaultChannelName
        let options = [
            TCHChannelOptionFriendlyName: channelName,
            TCHChannelOptionType: TCHChannelType.public.rawValue
            ] as [String : Any]
        channelsList!.createChannel(options: options) { result, channel in
            if (result.isSuccessful()) {
                self.generalChannel = channel
            }
            completion((result.isSuccessful()))
        }
    }

    func setGeneralChatRoomUniqueNameWithCompletion(completion:@escaping (Bool) -> Void) {
        generalChannel.setUniqueName(ChannelManager.defaultChannelUniqueName) { result in
            completion((result.isSuccessful()))
        }
    }

    // MARK: - Populate channel Descriptors

    func populateChannelDescriptors() {

        channelsList?.userChannelDescriptors { result, paginator in
            guard let paginator = paginator else {
                return
            }

            let newChannelDescriptors = NSMutableOrderedSet()
            newChannelDescriptors.addObjects(from: paginator.items())
            self.channelsList?.publicChannelDescriptors { result, paginator in
                guard let paginator = paginator else {
                    return
                }

                // de-dupe channel list
                let channelIds = NSMutableSet()
                for descriptor in newChannelDescriptors {
                    if let descriptor = descriptor as? TCHChannelDescriptor {
                        if let sid = descriptor.sid {
                            channelIds.add(sid)
                        }
                    }
                }
                for descriptor in paginator.items() {
                    if let sid = descriptor.sid {
                        if !channelIds.contains(sid) {
                            channelIds.add(sid)
                            newChannelDescriptors.add(descriptor)
                        }
                    }
                }


                // sort the descriptors
                let sortSelector = #selector(NSString.localizedCaseInsensitiveCompare(_:))
                let descriptor = NSSortDescriptor(key: "friendlyName", ascending: true, selector: sortSelector)
                newChannelDescriptors.sort(using: [descriptor])

                self.channelDescriptors = newChannelDescriptors

                if let delegate = self.delegate {
                    delegate.reloadChannelDescriptorList()
                }
            }
        }
    }


    // MARK: - Create channel

    func createChannelWithName(name: String, completion: @escaping (Bool, TCHChannel?) -> Void) {
        if (name == ChannelManager.defaultChannelName) {
            completion(false, nil)
            return
        }

        let channelOptions = [
            TCHChannelOptionFriendlyName: name,
            TCHChannelOptionType: TCHChannelType.public.rawValue
        ] as [String : Any]
        UIApplication.shared.isNetworkActivityIndicatorVisible = true;
        self.channelsList?.createChannel(options: channelOptions) { result, channel in
            UIApplication.shared.isNetworkActivityIndicatorVisible = false
            completion((result.isSuccessful()), channel)
        }
    }
}

// MARK: - TwilioChatClientDelegate
extension ChannelManager : TwilioChatClientDelegate {
    func chatClient(_ client: TwilioChatClient, channelAdded channel: TCHChannel) {
        DispatchQueue.main.async {
            self.populateChannelDescriptors()
        }
    }

    func chatClient(_ client: TwilioChatClient, channel: TCHChannel, updated: TCHChannelUpdate) {
        DispatchQueue.main.async {
            self.delegate?.reloadChannelDescriptorList()
        }
    }

    func chatClient(_ client: TwilioChatClient, channelDeleted channel: TCHChannel) {
        DispatchQueue.main.async {
            self.populateChannelDescriptors()
        }

    }

    func chatClient(_ client: TwilioChatClient, synchronizationStatusUpdated status: TCHClientSynchronizationStatus) {
    }
}

```

Next, we need a default channel.

## Join the General Channel

This application will try to join a channel called "General Channel" when it starts. If the channel doesn't exist, it'll create one with that name. The scope of this example application will show you how to work only with public channels, but the Programmable Chat client allows you to create private channels and handle invitations.

Once you have joined a channel, you can register a class as the `TCHChannelDelegate` so you can start listening to events such as `messageAdded` or `memberJoined`. We'll show you how to do this in the next step.

```swift title="Join or Create a General Channel" description="twiliochat/ChannelManager.swift"
// !mark(20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64,65,66,67,68,69,70,71,72,73)
import UIKit

protocol ChannelManagerDelegate {
    func reloadChannelDescriptorList()
}

class ChannelManager: NSObject {
    static let sharedManager = ChannelManager()

    static let defaultChannelUniqueName = "general"
    static let defaultChannelName = "General Channel"

    var delegate:ChannelManagerDelegate?

    var channelsList:TCHChannels?
    var channelDescriptors:NSOrderedSet?
    var generalChannel:TCHChannel!

    override init() {
        super.init()
        channelDescriptors = NSMutableOrderedSet()
    }

    // MARK: - General channel

    func joinGeneralChatRoomWithCompletion(completion: @escaping (Bool) -> Void) {

        let uniqueName = ChannelManager.defaultChannelUniqueName
        if let channelsList = self.channelsList {
            channelsList.channel(withSidOrUniqueName: uniqueName) { result, channel in
                self.generalChannel = channel

                if self.generalChannel != nil {
                    self.joinGeneralChatRoomWithUniqueName(name: nil, completion: completion)
                } else {
                    self.createGeneralChatRoomWithCompletion { succeeded in
                        if (succeeded) {
                            self.joinGeneralChatRoomWithUniqueName(name: uniqueName, completion: completion)
                            return
                        }

                        completion(false)
                    }
                }
            }
        }
    }

    func joinGeneralChatRoomWithUniqueName(name: String?, completion: @escaping (Bool) -> Void) {
        generalChannel.join { result in
            if ((result.isSuccessful()) && name != nil) {
                self.setGeneralChatRoomUniqueNameWithCompletion(completion: completion)
                return
            }
            completion((result.isSuccessful()))
        }
    }

    func createGeneralChatRoomWithCompletion(completion: @escaping (Bool) -> Void) {
        let channelName = ChannelManager.defaultChannelName
        let options = [
            TCHChannelOptionFriendlyName: channelName,
            TCHChannelOptionType: TCHChannelType.public.rawValue
            ] as [String : Any]
        channelsList!.createChannel(options: options) { result, channel in
            if (result.isSuccessful()) {
                self.generalChannel = channel
            }
            completion((result.isSuccessful()))
        }
    }

    func setGeneralChatRoomUniqueNameWithCompletion(completion:@escaping (Bool) -> Void) {
        generalChannel.setUniqueName(ChannelManager.defaultChannelUniqueName) { result in
            completion((result.isSuccessful()))
        }
    }

    // MARK: - Populate channel Descriptors

    func populateChannelDescriptors() {

        channelsList?.userChannelDescriptors { result, paginator in
            guard let paginator = paginator else {
                return
            }

            let newChannelDescriptors = NSMutableOrderedSet()
            newChannelDescriptors.addObjects(from: paginator.items())
            self.channelsList?.publicChannelDescriptors { result, paginator in
                guard let paginator = paginator else {
                    return
                }

                // de-dupe channel list
                let channelIds = NSMutableSet()
                for descriptor in newChannelDescriptors {
                    if let descriptor = descriptor as? TCHChannelDescriptor {
                        if let sid = descriptor.sid {
                            channelIds.add(sid)
                        }
                    }
                }
                for descriptor in paginator.items() {
                    if let sid = descriptor.sid {
                        if !channelIds.contains(sid) {
                            channelIds.add(sid)
                            newChannelDescriptors.add(descriptor)
                        }
                    }
                }


                // sort the descriptors
                let sortSelector = #selector(NSString.localizedCaseInsensitiveCompare(_:))
                let descriptor = NSSortDescriptor(key: "friendlyName", ascending: true, selector: sortSelector)
                newChannelDescriptors.sort(using: [descriptor])

                self.channelDescriptors = newChannelDescriptors

                if let delegate = self.delegate {
                    delegate.reloadChannelDescriptorList()
                }
            }
        }
    }


    // MARK: - Create channel

    func createChannelWithName(name: String, completion: @escaping (Bool, TCHChannel?) -> Void) {
        if (name == ChannelManager.defaultChannelName) {
            completion(false, nil)
            return
        }

        let channelOptions = [
            TCHChannelOptionFriendlyName: name,
            TCHChannelOptionType: TCHChannelType.public.rawValue
        ] as [String : Any]
        UIApplication.shared.isNetworkActivityIndicatorVisible = true;
        self.channelsList?.createChannel(options: channelOptions) { result, channel in
            UIApplication.shared.isNetworkActivityIndicatorVisible = false
            completion((result.isSuccessful()), channel)
        }
    }
}

// MARK: - TwilioChatClientDelegate
extension ChannelManager : TwilioChatClientDelegate {
    func chatClient(_ client: TwilioChatClient, channelAdded channel: TCHChannel) {
        DispatchQueue.main.async {
            self.populateChannelDescriptors()
        }
    }

    func chatClient(_ client: TwilioChatClient, channel: TCHChannel, updated: TCHChannelUpdate) {
        DispatchQueue.main.async {
            self.delegate?.reloadChannelDescriptorList()
        }
    }

    func chatClient(_ client: TwilioChatClient, channelDeleted channel: TCHChannel) {
        DispatchQueue.main.async {
            self.populateChannelDescriptors()
        }

    }

    func chatClient(_ client: TwilioChatClient, synchronizationStatusUpdated status: TCHClientSynchronizationStatus) {
    }
}

```

Now let's listen for some channel events.

## Listen to Channel Events

We registered `MainChatViewController` as the `TCHChannelDelegate`, and here we implemented the following methods that listen to channel events:

* `messageAdded`: When someone sends a message to the channel you are connected to.
* `channelDeleted`: When someone deletes a channel.
* `memberJoined`: When someone joins the channel.
* `memberLeft`: When someone leaves the channel.
* `synchronizationStatusChanged`: When channel synchronization status changes.

As you may have noticed, each one of these methods includes useful objects as parameters. One example is the actual message that was added to the channel.

```swift title="Listen to Channel Events" description="twiliochat/MainChatViewController.swift"
// !mark(228,229,230,231,232,233,234,235,236,237,238,239,240,241,242,243,244,245,246,247,248,249,250,251,252,253,254,255,256,257,258,259,260,261,262,263)
import UIKit
import SlackTextViewController

class MainChatViewController: SLKTextViewController {
    static let TWCChatCellIdentifier = "ChatTableCell"
    static let TWCChatStatusCellIdentifier = "ChatStatusTableCell"

    static let TWCOpenGeneralChannelSegue = "OpenGeneralChat"
    static let TWCLabelTag = 200

    var _channel:TCHChannel!
    var channel:TCHChannel! {
        get {
            return _channel
        }
        set(channel) {
            _channel = channel
            title = _channel.friendlyName
            _channel.delegate = self

            if _channel == ChannelManager.sharedManager.generalChannel {
                navigationItem.rightBarButtonItem = nil
            }

            joinChannel()
        }
    }

    var messages:Set<TCHMessage> = Set<TCHMessage>()
    var sortedMessages:[TCHMessage]!

    @IBOutlet weak var revealButtonItem: UIBarButtonItem!
    @IBOutlet weak var actionButtonItem: UIBarButtonItem!

    override func viewDidLoad() {
        super.viewDidLoad()

        if (revealViewController() != nil) {
            revealButtonItem.target = revealViewController()
            revealButtonItem.action = #selector(SWRevealViewController.revealToggle(_:))
            navigationController?.navigationBar.addGestureRecognizer(revealViewController().panGestureRecognizer())
            revealViewController().rearViewRevealOverdraw = 0
        }

        bounces = true
        shakeToClearEnabled = true
        isKeyboardPanningEnabled = true
        shouldScrollToBottomAfterKeyboardShows = false
        isInverted = true

        let cellNib = UINib(nibName: MainChatViewController.TWCChatCellIdentifier, bundle: nil)
        tableView!.register(cellNib, forCellReuseIdentifier:MainChatViewController.TWCChatCellIdentifier)

        let cellStatusNib = UINib(nibName: MainChatViewController.TWCChatStatusCellIdentifier, bundle: nil)
        tableView!.register(cellStatusNib, forCellReuseIdentifier:MainChatViewController.TWCChatStatusCellIdentifier)

        textInputbar.autoHideRightButton = true
        textInputbar.maxCharCount = 256
        textInputbar.counterStyle = .split
        textInputbar.counterPosition = .top

        let font = UIFont(name:"Avenir-Light", size:14)
        textView.font = font

        rightButton.setTitleColor(UIColor(red:0.973, green:0.557, blue:0.502, alpha:1), for: .normal)

        if let font = UIFont(name:"Avenir-Heavy", size:17) {
            navigationController?.navigationBar.titleTextAttributes = [NSAttributedString.Key.font: font]
        }

        tableView!.allowsSelection = false
        tableView!.estimatedRowHeight = 70
        tableView!.rowHeight = UITableView.automaticDimension
        tableView!.separatorStyle = .none

        if channel == nil {
            channel = ChannelManager.sharedManager.generalChannel
        }
    }

    override func viewDidLayoutSubviews() {
        super.viewDidLayoutSubviews()

        // required for iOS 11
        textInputbar.bringSubviewToFront(textInputbar.textView)
        textInputbar.bringSubviewToFront(textInputbar.leftButton)
        textInputbar.bringSubviewToFront(textInputbar.rightButton)

    }

    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        scrollToBottom()
    }

    override func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: NSInteger) -> Int {
        return messages.count
    }

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        var cell:UITableViewCell

        let message = sortedMessages[indexPath.row]

        if let statusMessage = message as? StatusMessage {
            cell = getStatusCellForTableView(tableView: tableView, forIndexPath:indexPath, message:statusMessage)
        }
        else {
            cell = getChatCellForTableView(tableView: tableView, forIndexPath:indexPath, message:message)
        }

        cell.transform = tableView.transform
        return cell
    }

    func getChatCellForTableView(tableView: UITableView, forIndexPath indexPath:IndexPath, message: TCHMessage) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: MainChatViewController.TWCChatCellIdentifier, for:indexPath as IndexPath)

        let chatCell: ChatTableCell = cell as! ChatTableCell
        let date = NSDate.dateWithISO8601String(dateString: message.dateCreated ?? "")
        let timestamp = DateTodayFormatter().stringFromDate(date: date)

        chatCell.setUser(user: message.author ?? "[Unknown author]", message: message.body, date: timestamp ?? "[Unknown date]")

        return chatCell
    }

    func getStatusCellForTableView(tableView: UITableView, forIndexPath indexPath:IndexPath, message: StatusMessage) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: MainChatViewController.TWCChatStatusCellIdentifier, for:indexPath as IndexPath)

        let label = cell.viewWithTag(MainChatViewController.TWCLabelTag) as! UILabel
        let memberStatus = (message.status! == .Joined) ? "joined" : "left"
        label.text = "User \(message.statusMember.identity ?? "[Unknown user]") has \(memberStatus)"
        return cell
    }

    func joinChannel() {
        setViewOnHold(onHold: true)

        if channel.status != .joined {
            channel.join { result in
                print("Channel Joined")
            }
            return
        }

        loadMessages()
        setViewOnHold(onHold: false)
    }

    // Disable user input and show activity indicator
    func setViewOnHold(onHold: Bool) {
        self.isTextInputbarHidden = onHold;
        UIApplication.shared.isNetworkActivityIndicatorVisible = onHold;
    }

    override func didPressRightButton(_ sender: Any!) {
        textView.refreshFirstResponder()
        sendMessage(inputMessage: textView.text)
        super.didPressRightButton(sender)
    }

    // MARK: - Chat Service

    func sendMessage(inputMessage: String) {
        let messageOptions = TCHMessageOptions().withBody(inputMessage)
        channel.messages?.sendMessage(with: messageOptions, completion: nil)
    }

    func addMessages(newMessages:Set<TCHMessage>) {
        messages =  messages.union(newMessages)
        sortMessages()
        DispatchQueue.main.async {
            self.tableView!.reloadData()
            if self.messages.count > 0 {
                self.scrollToBottom()
            }
        }
    }

    func sortMessages() {
        sortedMessages = messages.sorted { (a, b) -> Bool in
            (a.dateCreated ?? "") > (b.dateCreated ?? "")
        }
    }

    func loadMessages() {
        messages.removeAll()
        if channel.synchronizationStatus == .all {
            channel.messages?.getLastWithCount(100) { (result, items) in
                self.addMessages(newMessages: Set(items!))
            }
        }
    }

    func scrollToBottom() {
        if messages.count > 0 {
            let indexPath = IndexPath(row: 0, section: 0)
            tableView!.scrollToRow(at: indexPath, at: .bottom, animated: true)
        }
    }

    func leaveChannel() {
        channel.leave { result in
            if (result.isSuccessful()) {
                let menuViewController = self.revealViewController().rearViewController as! MenuViewController
                menuViewController.deselectSelectedChannel()
                self.revealViewController().rearViewController.performSegue(withIdentifier: MainChatViewController.TWCOpenGeneralChannelSegue, sender: nil)
            }
        }
    }

    // MARK: - Actions

    @IBAction func actionButtonTouched(_ sender: UIBarButtonItem) {
        leaveChannel()
    }

    @IBAction func revealButtonTouched(_ sender: AnyObject) {
        revealViewController().revealToggle(animated: true)
    }
}

extension MainChatViewController : TCHChannelDelegate {
    func chatClient(_ client: TwilioChatClient, channel: TCHChannel, messageAdded message: TCHMessage) {
        if !messages.contains(message) {
            addMessages(newMessages: [message])
        }
    }

    func chatClient(_ client: TwilioChatClient, channel: TCHChannel, memberJoined member: TCHMember) {
        addMessages(newMessages: [StatusMessage(statusMember:member, status:.Joined)])
    }

    func chatClient(_ client: TwilioChatClient, channel: TCHChannel, memberLeft member: TCHMember) {
        addMessages(newMessages: [StatusMessage(statusMember:member, status:.Left)])
    }

    func chatClient(_ client: TwilioChatClient, channelDeleted channel: TCHChannel) {
        DispatchQueue.main.async {
            if channel == self.channel {
                self.revealViewController().rearViewController
                    .performSegue(withIdentifier: MainChatViewController.TWCOpenGeneralChannelSegue, sender: nil)
            }
        }
    }

    func chatClient(_ client: TwilioChatClient,
                    channel: TCHChannel,
                    synchronizationStatusUpdated status: TCHChannelSynchronizationStatus) {
        if status == .all {
            loadMessages()
            DispatchQueue.main.async {
                self.tableView?.reloadData()
                self.setViewOnHold(onHold: false)
            }
        }
    }
}

```

We've actually got a real chat app going here, but let's make it more interesting with multiple channels.

## Join Other Channels

The application uses [SWRevealViewController](https://github.com/John-Lluch/SWRevealViewController) to show a sidebar that contains a list of the channels created for that Twilio account.

When you tap on the name of a channel from the sidebar, that channel is set on the `MainChatViewController`. The `joinChannel` method takes care of joining to the selected channel and loading the messages.

```swift title="Join Other Channels" description="twiliochat/MainChatViewController.swift"
// !mark(132,133,134,135,136,137,138,139,140,141,142,143,144,145,146,147,148,149,150,151,152,153)
import UIKit
import SlackTextViewController

class MainChatViewController: SLKTextViewController {
    static let TWCChatCellIdentifier = "ChatTableCell"
    static let TWCChatStatusCellIdentifier = "ChatStatusTableCell"

    static let TWCOpenGeneralChannelSegue = "OpenGeneralChat"
    static let TWCLabelTag = 200

    var _channel:TCHChannel!
    var channel:TCHChannel! {
        get {
            return _channel
        }
        set(channel) {
            _channel = channel
            title = _channel.friendlyName
            _channel.delegate = self

            if _channel == ChannelManager.sharedManager.generalChannel {
                navigationItem.rightBarButtonItem = nil
            }

            joinChannel()
        }
    }

    var messages:Set<TCHMessage> = Set<TCHMessage>()
    var sortedMessages:[TCHMessage]!

    @IBOutlet weak var revealButtonItem: UIBarButtonItem!
    @IBOutlet weak var actionButtonItem: UIBarButtonItem!

    override func viewDidLoad() {
        super.viewDidLoad()

        if (revealViewController() != nil) {
            revealButtonItem.target = revealViewController()
            revealButtonItem.action = #selector(SWRevealViewController.revealToggle(_:))
            navigationController?.navigationBar.addGestureRecognizer(revealViewController().panGestureRecognizer())
            revealViewController().rearViewRevealOverdraw = 0
        }

        bounces = true
        shakeToClearEnabled = true
        isKeyboardPanningEnabled = true
        shouldScrollToBottomAfterKeyboardShows = false
        isInverted = true

        let cellNib = UINib(nibName: MainChatViewController.TWCChatCellIdentifier, bundle: nil)
        tableView!.register(cellNib, forCellReuseIdentifier:MainChatViewController.TWCChatCellIdentifier)

        let cellStatusNib = UINib(nibName: MainChatViewController.TWCChatStatusCellIdentifier, bundle: nil)
        tableView!.register(cellStatusNib, forCellReuseIdentifier:MainChatViewController.TWCChatStatusCellIdentifier)

        textInputbar.autoHideRightButton = true
        textInputbar.maxCharCount = 256
        textInputbar.counterStyle = .split
        textInputbar.counterPosition = .top

        let font = UIFont(name:"Avenir-Light", size:14)
        textView.font = font

        rightButton.setTitleColor(UIColor(red:0.973, green:0.557, blue:0.502, alpha:1), for: .normal)

        if let font = UIFont(name:"Avenir-Heavy", size:17) {
            navigationController?.navigationBar.titleTextAttributes = [NSAttributedString.Key.font: font]
        }

        tableView!.allowsSelection = false
        tableView!.estimatedRowHeight = 70
        tableView!.rowHeight = UITableView.automaticDimension
        tableView!.separatorStyle = .none

        if channel == nil {
            channel = ChannelManager.sharedManager.generalChannel
        }
    }

    override func viewDidLayoutSubviews() {
        super.viewDidLayoutSubviews()

        // required for iOS 11
        textInputbar.bringSubviewToFront(textInputbar.textView)
        textInputbar.bringSubviewToFront(textInputbar.leftButton)
        textInputbar.bringSubviewToFront(textInputbar.rightButton)

    }

    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        scrollToBottom()
    }

    override func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: NSInteger) -> Int {
        return messages.count
    }

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        var cell:UITableViewCell

        let message = sortedMessages[indexPath.row]

        if let statusMessage = message as? StatusMessage {
            cell = getStatusCellForTableView(tableView: tableView, forIndexPath:indexPath, message:statusMessage)
        }
        else {
            cell = getChatCellForTableView(tableView: tableView, forIndexPath:indexPath, message:message)
        }

        cell.transform = tableView.transform
        return cell
    }

    func getChatCellForTableView(tableView: UITableView, forIndexPath indexPath:IndexPath, message: TCHMessage) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: MainChatViewController.TWCChatCellIdentifier, for:indexPath as IndexPath)

        let chatCell: ChatTableCell = cell as! ChatTableCell
        let date = NSDate.dateWithISO8601String(dateString: message.dateCreated ?? "")
        let timestamp = DateTodayFormatter().stringFromDate(date: date)

        chatCell.setUser(user: message.author ?? "[Unknown author]", message: message.body, date: timestamp ?? "[Unknown date]")

        return chatCell
    }

    func getStatusCellForTableView(tableView: UITableView, forIndexPath indexPath:IndexPath, message: StatusMessage) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: MainChatViewController.TWCChatStatusCellIdentifier, for:indexPath as IndexPath)

        let label = cell.viewWithTag(MainChatViewController.TWCLabelTag) as! UILabel
        let memberStatus = (message.status! == .Joined) ? "joined" : "left"
        label.text = "User \(message.statusMember.identity ?? "[Unknown user]") has \(memberStatus)"
        return cell
    }

    func joinChannel() {
        setViewOnHold(onHold: true)

        if channel.status != .joined {
            channel.join { result in
                print("Channel Joined")
            }
            return
        }

        loadMessages()
        setViewOnHold(onHold: false)
    }

    // Disable user input and show activity indicator
    func setViewOnHold(onHold: Bool) {
        self.isTextInputbarHidden = onHold;
        UIApplication.shared.isNetworkActivityIndicatorVisible = onHold;
    }

    override func didPressRightButton(_ sender: Any!) {
        textView.refreshFirstResponder()
        sendMessage(inputMessage: textView.text)
        super.didPressRightButton(sender)
    }

    // MARK: - Chat Service

    func sendMessage(inputMessage: String) {
        let messageOptions = TCHMessageOptions().withBody(inputMessage)
        channel.messages?.sendMessage(with: messageOptions, completion: nil)
    }

    func addMessages(newMessages:Set<TCHMessage>) {
        messages =  messages.union(newMessages)
        sortMessages()
        DispatchQueue.main.async {
            self.tableView!.reloadData()
            if self.messages.count > 0 {
                self.scrollToBottom()
            }
        }
    }

    func sortMessages() {
        sortedMessages = messages.sorted { (a, b) -> Bool in
            (a.dateCreated ?? "") > (b.dateCreated ?? "")
        }
    }

    func loadMessages() {
        messages.removeAll()
        if channel.synchronizationStatus == .all {
            channel.messages?.getLastWithCount(100) { (result, items) in
                self.addMessages(newMessages: Set(items!))
            }
        }
    }

    func scrollToBottom() {
        if messages.count > 0 {
            let indexPath = IndexPath(row: 0, section: 0)
            tableView!.scrollToRow(at: indexPath, at: .bottom, animated: true)
        }
    }

    func leaveChannel() {
        channel.leave { result in
            if (result.isSuccessful()) {
                let menuViewController = self.revealViewController().rearViewController as! MenuViewController
                menuViewController.deselectSelectedChannel()
                self.revealViewController().rearViewController.performSegue(withIdentifier: MainChatViewController.TWCOpenGeneralChannelSegue, sender: nil)
            }
        }
    }

    // MARK: - Actions

    @IBAction func actionButtonTouched(_ sender: UIBarButtonItem) {
        leaveChannel()
    }

    @IBAction func revealButtonTouched(_ sender: AnyObject) {
        revealViewController().revealToggle(animated: true)
    }
}

extension MainChatViewController : TCHChannelDelegate {
    func chatClient(_ client: TwilioChatClient, channel: TCHChannel, messageAdded message: TCHMessage) {
        if !messages.contains(message) {
            addMessages(newMessages: [message])
        }
    }

    func chatClient(_ client: TwilioChatClient, channel: TCHChannel, memberJoined member: TCHMember) {
        addMessages(newMessages: [StatusMessage(statusMember:member, status:.Joined)])
    }

    func chatClient(_ client: TwilioChatClient, channel: TCHChannel, memberLeft member: TCHMember) {
        addMessages(newMessages: [StatusMessage(statusMember:member, status:.Left)])
    }

    func chatClient(_ client: TwilioChatClient, channelDeleted channel: TCHChannel) {
        DispatchQueue.main.async {
            if channel == self.channel {
                self.revealViewController().rearViewController
                    .performSegue(withIdentifier: MainChatViewController.TWCOpenGeneralChannelSegue, sender: nil)
            }
        }
    }

    func chatClient(_ client: TwilioChatClient,
                    channel: TCHChannel,
                    synchronizationStatusUpdated status: TCHChannelSynchronizationStatus) {
        if status == .all {
            loadMessages()
            DispatchQueue.main.async {
                self.tableView?.reloadData()
                self.setViewOnHold(onHold: false)
            }
        }
    }
}

```

If we can join other channels, we'll need some way for a super user to create new channels (and delete old ones).

## Create a Channel

We use an input dialog so the user can type the name of the new channel. The only restriction here is that the user can't create a channel called "General Channel". Other than that, creating a channel involves calling `createChannel` and passing a dictionary with the new channel information.

```swift title="Create a Channel" description="twiliochat/ChannelManager.swift"
// !mark(103,104,105,106,107,108,109,110,111,112,113,114,115,116,117,118,119,120,121)
import UIKit

protocol ChannelManagerDelegate {
    func reloadChannelDescriptorList()
}

class ChannelManager: NSObject {
    static let sharedManager = ChannelManager()

    static let defaultChannelUniqueName = "general"
    static let defaultChannelName = "General Channel"

    var delegate:ChannelManagerDelegate?

    var channelsList:TCHChannels?
    var channelDescriptors:NSOrderedSet?
    var generalChannel:TCHChannel!

    override init() {
        super.init()
        channelDescriptors = NSMutableOrderedSet()
    }

    // MARK: - General channel

    func joinGeneralChatRoomWithCompletion(completion: @escaping (Bool) -> Void) {

        let uniqueName = ChannelManager.defaultChannelUniqueName
        if let channelsList = self.channelsList {
            channelsList.channel(withSidOrUniqueName: uniqueName) { result, channel in
                self.generalChannel = channel

                if self.generalChannel != nil {
                    self.joinGeneralChatRoomWithUniqueName(name: nil, completion: completion)
                } else {
                    self.createGeneralChatRoomWithCompletion { succeeded in
                        if (succeeded) {
                            self.joinGeneralChatRoomWithUniqueName(name: uniqueName, completion: completion)
                            return
                        }

                        completion(false)
                    }
                }
            }
        }
    }

    func joinGeneralChatRoomWithUniqueName(name: String?, completion: @escaping (Bool) -> Void) {
        generalChannel.join { result in
            if ((result.isSuccessful()) && name != nil) {
                self.setGeneralChatRoomUniqueNameWithCompletion(completion: completion)
                return
            }
            completion((result.isSuccessful()))
        }
    }

    func createGeneralChatRoomWithCompletion(completion: @escaping (Bool) -> Void) {
        let channelName = ChannelManager.defaultChannelName
        let options = [
            TCHChannelOptionFriendlyName: channelName,
            TCHChannelOptionType: TCHChannelType.public.rawValue
            ] as [String : Any]
        channelsList!.createChannel(options: options) { result, channel in
            if (result.isSuccessful()) {
                self.generalChannel = channel
            }
            completion((result.isSuccessful()))
        }
    }

    func setGeneralChatRoomUniqueNameWithCompletion(completion:@escaping (Bool) -> Void) {
        generalChannel.setUniqueName(ChannelManager.defaultChannelUniqueName) { result in
            completion((result.isSuccessful()))
        }
    }

    // MARK: - Populate channel Descriptors

    func populateChannelDescriptors() {

        channelsList?.userChannelDescriptors { result, paginator in
            guard let paginator = paginator else {
                return
            }

            let newChannelDescriptors = NSMutableOrderedSet()
            newChannelDescriptors.addObjects(from: paginator.items())
            self.channelsList?.publicChannelDescriptors { result, paginator in
                guard let paginator = paginator else {
                    return
                }

                // de-dupe channel list
                let channelIds = NSMutableSet()
                for descriptor in newChannelDescriptors {
                    if let descriptor = descriptor as? TCHChannelDescriptor {
                        if let sid = descriptor.sid {
                            channelIds.add(sid)
                        }
                    }
                }
                for descriptor in paginator.items() {
                    if let sid = descriptor.sid {
                        if !channelIds.contains(sid) {
                            channelIds.add(sid)
                            newChannelDescriptors.add(descriptor)
                        }
                    }
                }


                // sort the descriptors
                let sortSelector = #selector(NSString.localizedCaseInsensitiveCompare(_:))
                let descriptor = NSSortDescriptor(key: "friendlyName", ascending: true, selector: sortSelector)
                newChannelDescriptors.sort(using: [descriptor])

                self.channelDescriptors = newChannelDescriptors

                if let delegate = self.delegate {
                    delegate.reloadChannelDescriptorList()
                }
            }
        }
    }


    // MARK: - Create channel

    func createChannelWithName(name: String, completion: @escaping (Bool, TCHChannel?) -> Void) {
        if (name == ChannelManager.defaultChannelName) {
            completion(false, nil)
            return
        }

        let channelOptions = [
            TCHChannelOptionFriendlyName: name,
            TCHChannelOptionType: TCHChannelType.public.rawValue
        ] as [String : Any]
        UIApplication.shared.isNetworkActivityIndicatorVisible = true;
        self.channelsList?.createChannel(options: channelOptions) { result, channel in
            UIApplication.shared.isNetworkActivityIndicatorVisible = false
            completion((result.isSuccessful()), channel)
        }
    }
}

// MARK: - TwilioChatClientDelegate
extension ChannelManager : TwilioChatClientDelegate {
    func chatClient(_ client: TwilioChatClient, channelAdded channel: TCHChannel) {
        DispatchQueue.main.async {
            self.populateChannelDescriptors()
        }
    }

    func chatClient(_ client: TwilioChatClient, channel: TCHChannel, updated: TCHChannelUpdate) {
        DispatchQueue.main.async {
            self.delegate?.reloadChannelDescriptorList()
        }
    }

    func chatClient(_ client: TwilioChatClient, channelDeleted channel: TCHChannel) {
        DispatchQueue.main.async {
            self.populateChannelDescriptors()
        }

    }

    func chatClient(_ client: TwilioChatClient, synchronizationStatusUpdated status: TCHClientSynchronizationStatus) {
    }
}

```

Cool, we now know how to create a channel, let's say that we created a lot of channels by mistake. In that case, it would be useful to be able to delete those unnecessary channels. That's our next step!

## Delete a Channel

Deleting a channel is easier than creating one. We'll use the `UITableView` ability to delete a cell. Once you have figured out what channel is meant to be deleted (from the selected cell index path), call the channel's method `destroy`.

```swift title="Delete a Channel" description="twiliochat/MenuViewController.swift"
// !mark(158,159,160,161,162,163,164,165,166,167,168,169,170,171,172,173,174)
import UIKit

class MenuViewController: UIViewController {
    static let TWCOpenChannelSegue = "OpenChat"
    static let TWCRefreshControlXOffset: CGFloat = 120

    @IBOutlet weak var tableView: UITableView!
    @IBOutlet weak var usernameLabel: UILabel!

    var refreshControl: UIRefreshControl!

    override func viewDidLoad() {
        super.viewDidLoad()

        let bgImage = UIImageView(image: UIImage(named:"home-bg"))
        bgImage.frame = self.tableView.frame
        tableView.backgroundView = bgImage

        usernameLabel.text = MessagingManager.sharedManager().userIdentity

        refreshControl = UIRefreshControl()
        tableView.addSubview(refreshControl)
        refreshControl.addTarget(self, action: #selector(MenuViewController.refreshChannels), for: .valueChanged)
        refreshControl.tintColor = UIColor.white

        self.refreshControl.frame.origin.x -= MenuViewController.TWCRefreshControlXOffset
        ChannelManager.sharedManager.delegate = self
        tableView.reloadData()
    }

    // MARK: - Internal methods

    func loadingCellForTableView(tableView: UITableView) -> UITableViewCell {
        return tableView.dequeueReusableCell(withIdentifier: "loadingCell")!
    }

    func channelCellForTableView(tableView: UITableView, atIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let menuCell = tableView.dequeueReusableCell(withIdentifier: "channelCell", for: indexPath as IndexPath) as! MenuTableCell

        if let channelDescriptor = ChannelManager.sharedManager.channelDescriptors![indexPath.row] as? TCHChannelDescriptor {
            menuCell.channelName = channelDescriptor.friendlyName ?? "[Unknown channel name]"
        } else {
            menuCell.channelName = "[Unknown channel name]"
        }

        return menuCell
    }

    @objc func refreshChannels() {
        refreshControl.beginRefreshing()
        tableView.reloadData()
        refreshControl.endRefreshing()
    }

    func deselectSelectedChannel() {
        let selectedRow = tableView.indexPathForSelectedRow
        if let row = selectedRow {
            tableView.deselectRow(at: row, animated: true)
        }
    }

    // MARK: - Channel

    func createNewChannelDialog() {
        InputDialogController.showWithTitle(title: "New Channel",
                                            message: "Enter a name for this channel",
                                            placeholder: "Name",
                                            presenter: self) { text in
                                                ChannelManager.sharedManager.createChannelWithName(name: text, completion: { _,_ in
                                                    ChannelManager.sharedManager.populateChannelDescriptors()
                                                })
        }
    }

    // MARK: Logout

    func promptLogout() {
        let alert = UIAlertController(title: nil, message: "You are about to Logout", preferredStyle: .alert)

        let cancelAction = UIAlertAction(title: "Cancel", style: .cancel, handler: nil)
        let confirmAction = UIAlertAction(title: "Confirm", style: .default) { action in
            self.logOut()
        }

        alert.addAction(cancelAction)
        alert.addAction(confirmAction)
        present(alert, animated: true, completion: nil)
    }

    func logOut() {
        MessagingManager.sharedManager().logout()
        MessagingManager.sharedManager().presentRootViewController()
    }

    // MARK: - Actions

    @IBAction func logoutButtonTouched(_ sender: UIButton) {
        promptLogout()
    }

    @IBAction func newChannelButtonTouched(_ sender: UIButton) {
        createNewChannelDialog()
    }

    // MARK: - Navigation

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        if segue.identifier == MenuViewController.TWCOpenChannelSegue {
            let indexPath = sender as! NSIndexPath

            let channelDescriptor = ChannelManager.sharedManager.channelDescriptors![indexPath.row] as! TCHChannelDescriptor
            let navigationController = segue.destination as! UINavigationController

            channelDescriptor.channel { (result, channel) in
                if let channel = channel {
                    (navigationController.visibleViewController as! MainChatViewController).channel = channel
               }
            }

        }
    }

    // MARK: - Style

    override var preferredStatusBarStyle: UIStatusBarStyle {
        return .lightContent
    }
}

// MARK: - UITableViewDataSource
extension MenuViewController : UITableViewDataSource {
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        if let channelDescriptors = ChannelManager.sharedManager.channelDescriptors {
            print (channelDescriptors.count)
            return channelDescriptors.count
        }
        return 1
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell: UITableViewCell

        if ChannelManager.sharedManager.channelDescriptors == nil {
            cell = loadingCellForTableView(tableView: tableView)
        }
        else {
            cell = channelCellForTableView(tableView: tableView, atIndexPath: indexPath as NSIndexPath)
        }

        cell.layoutIfNeeded()
        return cell
    }

    func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
        if let channel = ChannelManager.sharedManager.channelDescriptors?.object(at: indexPath.row) as? TCHChannel {
            return channel != ChannelManager.sharedManager.generalChannel
        }
        return false
    }

    func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle,
                   forRowAt indexPath: IndexPath) {
        if editingStyle != .delete {
            return
        }
        if let channel = ChannelManager.sharedManager.channelDescriptors?.object(at: indexPath.row) as? TCHChannel {
            channel.destroy { result in
                if (result.isSuccessful()) {
                    tableView.reloadData()
                }
                else {
                    AlertDialogController.showAlertWithMessage(message: "You can not delete this channel", title: nil, presenter: self)
                }
            }
        }
    }
}

// MARK: - UITableViewDelegate
extension MenuViewController : UITableViewDelegate {
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        tableView.deselectRow(at: indexPath, animated: true)
        performSegue(withIdentifier: MenuViewController.TWCOpenChannelSegue, sender: indexPath)
    }
}


// MARK: - ChannelManagerDelegate
extension MenuViewController : ChannelManagerDelegate {
    func reloadChannelDescriptorList() {
        tableView.reloadData()
    }
}

```

That's it! We've built an iOS application with Swift. Now you are more than prepared to set up your own chat application.

## Where to Next?

If you are an iOS developer working with Twilio, you might want to check out this other project:

[**Notifications Quickstart**](https://github.com/TwilioDevEd/notifications-quickstart-swift)

Twilio Notifications for iOS Quickstart using Swift

### Did this help?

Thanks for checking out this tutorial! If you have any feedback to share with us, we'd love to hear it. Tweet [@twilio](http://twitter.com/twilio) to let us know what you think.
