[Problem/Question]
I have an application in iOS that I would like to integrate chat into. I would like to leverage SendBirdUIKit for the UI.
I’ve asked ChatGTP how I can create a one to one chat experience. It tells me I need to use both SendBirdUIKit and the SendBirdSDK to achieve what I want to do. Here is what it is telling me:
1. Install SendBirdUIKit and SendBirdSDK:
You can install SendBirdUIKit and SendBirdSDK using CocoaPods or Swift Package Manager. Here, I will use CocoaPods:
Add the following to your Podfile
:
platform :ios, '10.0'
use_frameworks!
target 'YourAppTarget' do
pod 'SendBirdSDK'
pod 'SendBirdUIKit'
end
Then, run pod install
in your terminal to install the dependencies.
2. Import SendBirdUIKit:
In your ViewController or the file where you want to implement the chat functionality, import SendBirdUIKit:
import SendBirdUIKit
3. Initialize SendBirdSDK:
In your AppDelegate file, import SendBirdSDK and initialize it with your application ID within the didFinishLaunchingWithOptions
method:
import SendBirdSDK
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
SBDMain.initWithApplicationId("YOUR_APP_ID")
return true
}
Replace "YOUR_APP_ID"
with your actual SendBird application ID.
4. Connect to SendBird server:
Before you can create a chat, you need to connect to the SendBird server with a user. Here’s an example of how to connect:
func connectToSendBird(userId: String, completionHandler: @escaping (Result<SBDUser, Error>) -> Void) {
SBDMain.connect(withUserId: userId) { (user, error) in
if let error = error {
completionHandler(.failure(error))
} else if let user = user {
completionHandler(.success(user))
}
}
}
Call this function with a unique userId
to connect to the SendBird server.
5. Open one-on-one chat:
After connecting to the server, use the following code to create a one-on-one chat:
func openOneToOneChat(targetUserId: String) {
let channelListParams = SBDGroupChannelListQuery()
channelListParams?.limit = 10
channelListParams?.userIdsFilter = [targetUserId]
channelListParams?.memberStateFilter = .joined
channelListParams?.loadNextPage(completionHandler: { (channels, error) in
if let error = error {
print("Error loading channels: \(error)")
return
}
if let channel = channels?.first {
self.openChat(channel: channel)
} else {
self.createOneToOneChannel(targetUserId: targetUserId)
}
})
}
func createOneToOneChannel(targetUserId: String) {
let params = SBDGroupChannelParams()
params.isDistinct = true
params.addUserIds([targetUserId])
SBDGroupChannel.createChannel(with: params) { (channel, error) in
if let error = error {
print("Error creating channel: \(error)")
return
}
if let channel = channel {
self.openChat(channel: channel)
}
}
}
func openChat(channel: SBDGroupChannel) {
let channelVC = SBUChannelViewController(channel: channel)
let navigationController = UINavigationController(rootViewController: channelVC)
self.present(navigationController, animated: true, completion: nil)
}
Call the openOneToOneChat(targetUserId:)
function with the target user’s ID when you want to open a chat.
My question becomes… should I really be using both the UIKit and SDK to achieve this. My app has a little message user button next to each users profile. My goal is to make a function that passes the user information to sendbird and creates a one to one group channel then displays the chat box.
Important Note:
The lastest knowledge ChatGTP 4 has on Sendbird UI is version version 2.1.2. So what it is suggest may be outdated. I am using the latest version.
// If problem, please fill out the below. If question, please delete.
[UIKit Version]
Latest