Menu

Rate this page:

Thanks for rating this page!

We are always striving to improve our documentation quality, and your feedback is valuable to us. How could this documentation serve you better?

Chat with iOS and Objective-C

Download the Code

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

  1. Twilio Programmable Chat 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 which contains all your Twilio account information. The Chat Client uses this token to connect with the API
  3. Twilio Access Manager is the part of the SDK than handles access tokens and even refreshes them upon token expiration.

Properati built a web and mobile messaging app to help real estate buyers and sellers connect in real time. Learn more here.

Let's get started!

Initialize the Chat Client

The only thing you need to create a client is an access token. This token holds information about your Twilio account and 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:

We use AFNetworking to make a request to our server and get the access token.

Loading Code Sample...
      
      
      
      
      twiliochat/MessagingManager.m

      Fetch Access Token

      twiliochat/MessagingManager.m

      Now it's time to synchronize your Twilio Client.

      What's this "synchronizing" about?

      Synchronize the Chat Client

      In the previous step we initialized the Chat client with an Access Manager and set the client's delegate to the shared instance of our MessagingManager.

      The synchronizationStatusUpdated delegate 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 instance as the options parameter in the previews step.

      We need the client to be synchronized before trying to get the channel list. Otherwise, calling client.channelsList() will return nil.

      Loading Code Sample...
          
          
          
          
          twiliochat/MessagingManager.m

          Synchronize the Chat Client

          twiliochat/MessagingManager.m

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

          Get the Channel List

          Get the Channel 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 TCHChannelList to our ChannelManager. Now we must get an actual array of channels using the userChannelsWithCompletion and publicChannelsWithCompletion methods.

          Loading Code Sample...
              
              
              
              
              twiliochat/ChannelManager.m

              Get Channel List

              twiliochat/ChannelManager.m

              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

              Listen to Client Events

              The Chat Client will trigger events such as channelAdded 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.

              Loading Code Sample...
                  
                  
                  
                  
                  twiliochat/ChannelManager.m

                  Listen for Client Events

                  twiliochat/ChannelManager.m

                  Next, we need a default channel.

                  Join the General 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 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.

                  Loading Code Sample...
                      
                      
                      
                      
                      twiliochat/ChannelManager.m

                      Join or Create a General Channel

                      twiliochat/ChannelManager.m

                      Now let's listen for some channel events.

                      Listen to Channel Events

                      Listen to Channel Events

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

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

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

                      Loading Code Sample...
                          
                          
                          
                          
                          twiliochat/MainChatViewController.m

                          Listen to Channel Events

                          twiliochat/MainChatViewController.m

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

                          Join Other Channels

                          Join Other Channels

                          The application uses 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 setChannel method takes care of joining to the selected channel and loading the messages.

                          Loading Code Sample...
                              
                              
                              
                              
                              twiliochat/MainChatViewController.m

                              Join Other Channels

                              twiliochat/MainChatViewController.m

                              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

                              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 is as simple as calling createChannelWithOptions and passing a dictionary with the new channel information.

                              Loading Code Sample...
                                  
                                  
                                  
                                  
                                  twiliochat/ChannelManager.m

                                  Create a Channel

                                  twiliochat/ChannelManager.m

                                  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

                                  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), deleting it is as simple as calling the channel's method destroyWithCompletion.

                                  Loading Code Sample...
                                      
                                      
                                      
                                      
                                      twiliochat/MenuViewController.m

                                      Delete a Channel

                                      twiliochat/MenuViewController.m

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

                                      Where to next?

                                      Where to Next?

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

                                      Client Quickstart

                                      Twilio Client for iOS Quickstart using Objective-C

                                      Notifications Quickstart

                                      Twilio Notifications for iOS Quickstart using Objective-C

                                      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 to let us know what you think.

                                      Mario Celi Jose Oliveros Agustin Camino David Prothero Andrew Baker Jeff Linwood Kat King

                                      Need some help?

                                      We all do sometimes; code is hard. Get help now from our support team, or lean on the wisdom of the crowd browsing the Twilio tag on Stack Overflow.

                                      Loading Code Sample...