In this tutorial, we'll build an in-app support system using Stream Chat's Swift SDK, which you can quickly integrate in new or existing apps. It's crucial for users that may need immediate information or assistance in completing a task. It's frequently present in banking, e-commerce, delivery, and ride sharing apps.
Those screenshots show a button leading to a support chat screen where the user can get information in real time from an agent. This is what we'll build.
If you get lost during this tutorial, you can check the completed project in this GitHub repo.
What is Stream Chat?
Build real-time chat in less time. Rapidly ship in-app messaging with our highly reliable chat infrastructure. Drive in-app conversion, engagement, and retention with the Stream Chat messaging platform API & SDKs.
Set up the Stream Chat dependency
To install the Stream Chat dependency, we'll use CocoaPods. If you prefer Carthage or Swift Package Manager, they're also supported.
In your project's folder, if you aren't already using CocoaPods, run pod init
and add StreamChat
to the Podfile
. It should look similar to this:
(dev.to might be showing a different snippet due to a bug, if so link is here: https://gist.github.com/cardoso/e8aa4c517436a203f1633a3ffd4124dd) - TIP: If you refresh the page, it may fix it!
After you do that, run pod install
, wait a bit for it to finish, and open the project via the .xcworkspace
that was created.
Configure the Stream Chat dashboard
Sign up at GetStream.io, create the application, and make sure to select development instead of production.
To make things simple for now, let's disable both auth checks and permission checks. Make sure to hit save. When your app is in production, you should keep these enabled.
You can see the documentation about authentication here and permissions here.
Now, save your Stream credentials, as we'll need them to power the chat in the app in the next step. Since we disabled auth and permissions, we'll only really need the key for now, but in production, you'll use the secret in your backend to implement proper authentication to issue user tokens for Stream Chat, so users can interact with your app securely.
As you can see, I've blacked out my keys. You should make sure to keep your credentials safe.
Configure Stream Chat SDK
Now that we've set up the project and Stream Chat dashboard let's configure the SDK's singleton with the Stream Chat App's key you got in the last step. The didFinishLaunchingWithOptions
function in AppDelegate.swift
should look similar to the following snippet.
(dev.to might be showing a different snippet due to a bug, if so link is here: https://gist.github.com/cardoso/a6cb823607a30b946e91000ad89fbd24)
That will configure the Client.shared
instance, which will be used by the Stream Chat UI components to make calls to the Stream Chat API and subscribe to events.
Create the Join Screen
Let's start building the "Join" screen. This screen consists of two UIButton
instances. One to join as a User, and the other to join as an Agent. This is, of course, an oversimplification to make this tutorial short and get to the chat features faster. In your complete app, you'll need proper registration, database, and all that. It's also likely that you'll want a separate app for the agents. For this tutorial, the screen will look similar to the screenshot below.
Go to the storyboard, select the default view controller, and click Editor > Embed In > Navigation Controller
. That will place it under a navigation controller, which we'll use to navigate to the next screen.
Make sure to rename ViewController
to JoinViewController
, so you don't get confused later on. You can do this easily by right-clicking on ViewController
in ViewController.swift
and selecting refactor
.
To make things simple, let's leave the storyboard like this and use only code from now on. To set up the two buttons, we need the following code in JoinViewController.swift
:
(dev.to might be showing a different snippet due to a bug, if so link is here: https://gist.github.com/cardoso/24d5c2613157f718bb7671e9b3819cf3)
That code sets up the views, the constraints, and the handlers we need. Let's start by extending JoinViewController
to define setupViews
:
(dev.to might be showing a different snippet due to a bug, if so link is here: https://gist.github.com/cardoso/df4f4c4adf866da9bec479074e6b8b27)
That code will create the buttons and add them to the controller's view. Next, we need to define constraints between the three. Let's do this by extending JoinViewController
to define setupConstraints
:
(dev.to might be showing a different snippet due to a bug, if so link is here: https://gist.github.com/cardoso/6e5b570fd838beba0a691a684c90e593)
That code will make sure the userButton
stays in the center of the screen and the agentButton
below it. Now we need to set up the handlers for when the user presses the buttons. Let's do this again by extending the controller to define setupHandlers
:
(dev.to might be showing a different snippet due to a bug, if so link is here: https://gist.github.com/cardoso/aeb496435868fe11756b5e6c2a0a6c0e)
That code will make it so when the user button is pressed, a UserViewController
is presented, and when the agent button is pressed a ChannelsViewController
, which is provided by Stream Chat, is presented for an agent to see the open support channels. Additionally, we're generating a random id for the user using the String extension below.
(dev.to might be showing a different snippet due to a bug, if so link is here: https://gist.github.com/cardoso/7dbaa88064539d43871e84bf4b154c1f)
In a real-world app this is normally defined in the registration and login processes.
We'll create UserViewController
in the next step.
Create the User Screen
Now, let's create the screen that represents our regular app and through which a regular user can access the support chat. There are many ways different apps use to access their support system, such as floating buttons and menu items. In this tutorial we'll introduce a button in the navigation bar for opening the support chat. It will look similar to the screenshot below.
The first step is to create a UserViewController.swift file and paste the code below.
(dev.to might be showing a different snippet due to a bug, if so link is here: https://gist.github.com/cardoso/83c9c37f9381864f46115b13f6dc851c)
That code sets up the views and the handlers we need. Let's start by extending JoinViewController
to define setupViews
:
(dev.to might be showing a different snippet due to a bug, if so link is here: https://gist.github.com/cardoso/25b05c84f80fc3e080c8434bae826866)
That code will place the support button at the top right in the navigation bar. However, we still need to define the handler for that button press. Let's do that by defining setupHandlers
:
(dev.to might be showing a different snippet due to a bug, if so link is here: https://gist.github.com/cardoso/e27c1e0f059fe75b568311a1027edf85)
That code will create a support channel and present a ChatViewController
, provided by Stream Chat, that will have all the behavior we need for the chat.
If you're an agent, you'll be able to see all support channels created by users and access the chat screen for each of them.
Wrapping up
Congratulations! You've built the basis of a functioning in-app support chat system with Stream Chat. I encourage you to browse through Stream Chat's docs and experiment with the project you just built.
Top comments (0)