This tutorial teaches you to integrate Sendbird UIKit for iOS in your 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:
Storyboard
as the Interface.You can install UIKit for iOS through Swift Package Manager, CocoaPods, or Carthage. We'll use Swift Package Manager in this codelab.
https://github.com/sendbird/sendbird-uikit-ios-spm.git
Now, let's initialize Sendbird UIKit in your app:
Add the following code to your AppDelegate.swift
file. Make sure you replace the APP_ID
, USER_ID
, and the user's ACCESS_TOKEN
.
// AppDelegate.swift
import UIKit
import SendbirdUIKit
@main
class AppDelegate: UIResponder, UIApplicationDelegate {
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
let APP_ID = "YOUR_APPLICATION_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: {
// This is the origin.
// Initialization of SendbirdUIKit 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.
}
SBUGlobals.currentUser = SBUUser(userId: "USER_ID")
SBUGlobals.accessToken = "ACCESS_TOKEN"
SendbirdUI.connect { user, error in
guard let user = user, error == nil else {
print("Failed to connect with error: \(error!)")
return
}
print("Successfully connected \(user.userId).")
}
return true
}
}
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.
// SceneDelegate.swift
import UIKit
import SendbirdUIKit
class SceneDelegate: UIResponder, UIWindowSceneDelegate {
var window: UIWindow?
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
guard let windowScene = (scene as? UIWindowScene) else { return }
window = UIWindow(windowScene: windowScene)
let channelListVC = SBUViewControllerSet.GroupChannelListViewController()
let navigationController = UINavigationController(rootViewController: channelListVC)
window?.rootViewController = navigationController
window?.makeKeyAndVisible()
}
}
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:
Add the following code to your ViewController.swift
file:
import UIKit
import SendbirdUIKit
import SendbirdChatSDK
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
let params = MessageListParams()
let channelVC = SBUGroupChannelViewController(
channelURL: "CHANNEL_URL",
messageListParams: params
)
let navigationController = UINavigationController(rootViewController: channelVC)
present(navigationController, animated: true)
}
}
Replace the SceneDelegate.swift
file with this code:
import UIKit
import SendbirdUIKit
class SceneDelegate: UIResponder, UIWindowSceneDelegate {
var window: UIWindow?
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
guard let windowScene = (scene as? UIWindowScene) else { return }
window = UIWindow(windowScene: windowScene)
// `SBUViewControllerSet.GroupChannelListViewController` is `SBUGroupChannelListViewController` by default.
let channelListVC = SBUViewControllerSet.GroupChannelListViewController.init()
let navigationController = UINavigationController(rootViewController: ViewController())
window?.rootViewController = navigationController
window?.makeKeyAndVisible()
}
// ...
}
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 UIKit for iOS in your application. You've learned how to: