This tutorial explains how to implement Sendbird’s UIKit in an iOS project using SwiftUI.
Before you start, make sure you have:
Please note that this tutorial assumes basic familiarity with Swift.
So let’s dive in!
First, add the Sendbird UIKit dependency to your Cocoapod .podspec file (for instructions on using Carthage, see this page):
pod 'SendbirdUIKit'
Make sure the minimum platform version is 10.3 or higher:
platform :ios, '10.3'
Then run `pod install`.
Because the Sendbird UIKit was originally created with the Apple UIKit, you’ll need to create a UIViewControllerRepresentable struct that returns a UIViewController class.
3a. Add an Objective-C function to create an instance of UINavigationController with Sendbird’s SBUCHannelListViewController as the root view controller.
3b. Call this function within a `viewDidAppear` override block.
4a. Set the UIViewControllerType typealias to the ChannelListViewController class above.
4b. Have the required makeUIViewController function return an instance of the ChannelListViewController class.
4c. Add the required updateUIViewController function. You can leave it empty if needed.
In the default `ContentView.swift` file, we’ll be doing the following in the init() block:
*Your application ID can be found or created through the Sendbird dashboard:
**Note that if the app ID is invalid or we encounter a connection error, an instance of an SBDUser will not be returned in the completion handler.
If you use a valid application ID, then build it and run it onto a simulator or device, you will see the following screen:
UIKit is now up and running in your SwiftUI application!
Use the button to create a new Group Channel with another test user. You will need at least two users in your app before any will appear in an SBUCreateChannelViewController (or SBUInivteUserViewController or SBUMemberListViewController) view instance that is automatically created by UIKit:
Select a few users with whom to create a channel. Then click the “Create” button to open up an instance of an SBUChannelViewController view automatically. Once you’ve sent a few messages among your users, you will see something like:
All the code presented here can be found in this demo repo.
Congratulations! You have now implemented the Sendbird UIKit in your iOS project.
In this tutorial, we covered how to:
If you need additional information or guidance, don’t forget to check out our docs.
Happy chat building!
Stay up-to-date on the latest technical content from Sendbird.
Thank You for registering!