This tutorial teaches you to integrate Sendbird's Chat SwiftUI in your iOS application.
This is what your app will look like when you've finished this tutorial:
Left: channel list view. Right: channel view.
Before you start, you need the following:
Next, create a user in your Sendbird application:
Let's start by creating a new Xcode project:
SwiftUI
as the Interface.You can install Sendbird Chat SwiftUI using either Swift Package Manager or CocoaPods.
https://github.com/sendbird/sendbird-swiftui-ios.git
Podfile
in your project directory if you don't have onepod 'SendbirdSwiftUI'
pod install
in your terminalNow, let's initialize Sendbird Chat SwiftUI in your app:
Add the following code to your MyApp.swift
file. Make sure you replace the APP_ID
, USER_ID
, and the user's ACCESS_TOKEN
.
import SwiftUI
import SendbirdSwiftUI
@main
struct MyApp: App {
init() {
setupSendbird()
setupCurrentUser()
}
var body: some Scene {
WindowGroup {
ContentView()
}
}
}
private extension MyApp {
func setupSendbird() {
let APP_ID = "APP_ID" // Specify your Sendbird application ID.
SendbirdUI.initialize(
applicationId: APP_ID
) { params in
// This is the builder block where you can modify the initParameter.
//
// [example]
// params.needsSynchronous = false
} startHandler: {
// Initialization of SendbirdSwiftUI has started.
// We recommend showing a loading indicator once started.
} migrationHandler: {
// DB migration has started.
} completionHandler: { error in
// If DB migration is successful, proceed to the next step.
// If DB migration fails, an error exists.
// We recommend hiding the loading indicator once done.
}
}
func setupCurrentUser() {
// Set current user.
SBUGlobals.currentUser = SBUUser(userId: "USER_ID")
SBUGlobals.accessToken = "ACCESS_TOKEN"
}
}
Note: You can find the user's Access token in the Sendbird Dashboard under your Application > Users > your user > Access token. For this tutorial, you are using the user access token as a way of authentication. For actual implementation, it is highly recommended to refer to this authentication guide to implement a more secure way of authentication.
Now, let's display the channel list.
Note: If your app doesn't require a Channel List view, you can skip this step and go on to step 7: Display Channel.
ContentView.swift
import SwiftUI
import SendbirdSwiftUI
struct ContentView: View {
var body: some View {
GroupChannelListView()
}
}
MyApp.swift
, update the body
to use ContentView
:var body: some Scene {
WindowGroup {
ContentView()
}
}
Press the build button to launch the app on the simulator. The list of channels associated with the indicated user will be displayed as the starting screen, as shown below.
To directly display the channel view without a channel list:
ContentView.swift
import SwiftUI
import SendbirdSwiftUI
struct ContentView: View {
var body: some View {
GroupChannelView(channelURL: "CHANNEL_URL")
}
}
MyApp.swift
, update the body
to use ContentView
:var body: some Scene {
WindowGroup {
ContentView()
}
}
Press the build button to launch the app on the simulator. The specified channel will be displayed as the starting screen, as shown below.
You've now successfully integrated Sendbird Chat SwiftUI in your iOS application. You've learned how to:
CreateGroupChannelView
, GroupChannelSettingsView
or CreateOpenChannelView
, OpenParticipantListView
, and more.