SendBirdUIKit 3.26.0

SendBirdUIKit 3.26.0

Jed GyeongTez ParkSDK-AutomationCelineDamon.Park维护。



  • Tez、Celine、Damon、Jed、Minhyuk、Young 和 Kai 创作

Sendbird UIKit for iOS

Platform Languages CocoaPods Swift Package Manager Carthage compatible Commercial License

我们推出了 Sendbird UIKit 的新版本。第 3 版功能有一个新的模块化架构,具有更细粒度的组件,为您提供了更强大的定制灵活性,以便自定义您网页和移动应用。查看我们的迁移指南并下载我们的示例

v3 版本正式发布后,将 master 分支的名称更改为 main 分支,并将 main 分支的内容更改为 v3。如果您必须继续使用 v2,请使用 main-v2 分支。

  • v3: main
  • v2: main-v2

目录

  1. 简介
  2. 开始之前
  3. 入门
  4. 实现指南
  5. UIKit 概览

简介

Sendbird UIKit for iOS 是一款开发工具包,具有用户界面,使得将标准聊天功能轻松快速地集成到新的或现有的客户端应用中变得容易。从整体主题到颜色和字体等个别样式,组件可以完全自定义,以创建具有您品牌身份的独特应用内聊天体验。

注意:目前,iOS 的 UIKit 支持群组频道和公开频道。

ThemeLight

此存储库包含 UIKit 源代码和 UIKit 示例,以及 UIKit 框架。

  • 源代码部分可以找到开源代码。请查看UIKit 开源指南以获取有关我们对开源立场的更多信息。
  • 示例是一个包含用 Swift 编写的各种关键功能自定义代码的聊天应用。

优点

  • 易于安装
  • 代码量最少,功能最全的聊天
  • 可自定义的组件、事件和视图
  • 可自定义用户列表,允许指定用户之间的聊天

关于 Sendbird UIKit for iOS 的更多信息

有关 Sendbird UIKit for iOS 的更多信息,请参阅《UIKit for iOS 文档》。如果您对我们软件的bug或功能请求有任何评论或问题,请访问《Sendbird 社区》。


开始前

本节展示了您需要检查的先决条件,以使用 Sendbird UIKit for iOS。

需求

Sendbird UIKit for iOS 的最低需求如下:

  • iOS 11+
  • Swift 5.0+
  • Sendbird Chat SDK for iOS 4.9.5+

入门指南

本节提供了您开始使用 Sendbird UIKit for iOS 所需的信息。

尝试示例应用程序

我们的示例应用程序拥有 Sendbird UIKit for iOS 的所有核心功能。从我们的 GitHub 仓库下载应用程序,在您构建自己的项目之前,先了解您可以如何使用实际的 UIKit。

创建项目

您可以通过创建项目开始使用。Sendbird UIKit 支持使用 Swift,因此您可以用您想要开发的任何语言创建和开展工作。

Create a project

安装 UIKit for iOS

UIKit for iOS 可以通过以下方式安装:CocoaPodsCarthageSwift Package Manager

注意:Sendbird UIKit for iOS 依赖于 Sendbird Chat SDK。

- Swift 包管理器

  1. 转到您的 Swift Package Manager 的 文件 选项卡,并选择 Swift 包。然后选择 添加依赖项...

  2. 如下将 SendbirdUIKit 添加到您的 包仓库

https://github.com/sendbird/sendbird-uikit-ios-spm.git
  1. 要添加包,选择 分支规则,输入 main 并点击 下一步.

- CocoaPods

  1. SendBirdUIKit 添加到 Xcode 中的 Podfile 如下所示
platform :ios, '11.0'
use_frameworks!

target YOUR_PROJECT_TARGET do
    pod 'SendBirdUIKit'
end
  1. 通过 CocoaPods 安装 SendbirdUIKit 框架。
$ pod install
  1. 通过 CocoaPods 更新 SendbirdUIKit 框架。
$ pod update

注意:CocoaPods 使用的是 SendBirdUIKit 的名称,而不是 SendbirdUIKit。

- Carthage

  1. 如下将 SendbirdUIKitSendBirdSDK 添加到您的 Cartfile
github "sendbird/sendbird-uikit-ios"
github "sendbird/sendbird-chat-sdk-ios"
  1. 通过 Carthage 安装 SendbirdUIKit 框架。
$ carthage update --use-xcframeworks

注意: 使用 Carthage 构建或创建 SendbirdUIKit 框架,只能使用最新的 Swift 版本。如果您的 Swift 不是最新版本,则需要手动将框架复制到项目中。

  1. 进入您的 Xcode 项目的目标中,在 Frameworks 和 Libraries 部分的 General settings 选项卡。然后从 <YOUR_XCODE_PROJECT_DIRECTORY>/Carthage/Build 文件夹中拖动并放下 SendbirdUIKit.framework

注意: 如果您使用的是 Xcode 11.3 或更早版本构建项目,可能会发生错误。要修复这些错误,请参阅 处理由未知属性引起的错误

获取附件权限

Sendbird UIKit 提供了附加或保存文件的功能,例如照片、视频和文档。要使用这些功能,您需要从最终用户那里请求权限。

- 媒体附件权限

应用程序必须从最终用户那里获取权限,才能使用其相册中的照片资产或将资产保存到其库中。一旦获得权限,用户就可以发送图像或视频消息并保存媒体资产。

...
<key>NSPhotoLibraryUsageDescription</key>
    <string>$(PRODUCT_NAME) would like access to your photo library</string>
<key>NSCameraUsageDescription</key>
    <string>$(PRODUCT_NAME) would like to use your camera</string>
<key>NSMicrophoneUsageDescription</key>
    <string>$(PRODUCT_NAME) would like to use your microphone (for videos)</string>
<key>NSPhotoLibraryAddUsageDescription</key>
    <string>$(PRODUCT_NAME) would like to save photos to your photo library</string>
...

Media attachment permission

(可选) 文件附件权限

如果您想从 iCloud 附带文件,您必须启用 iCloud 功能。启用后,用户还可以通过 iCloud 发送带文件的消息。

转到您的 Xcode 项目标签页 “签名 & 权限”。然后,点击 + 权限 按钮,选择 “iCloud”。检查 “iCloud 文档”

Document attachment permission


实现指南

使用 APP_ID 初始化

为了使用 Chat SDK 的功能,您必须使用 APP_ID 初始化 SendbirdUIKit 实例。此步骤还会初始化 iOS 的 Chat SDK。

通过 AppDelegate 如下初始化 SendbirdUIKit 实例

// AppDelegate.swift

import SendbirdUIKit

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
    
    let APP_ID = "2D7B4CDB-932F-4082-9B09-A1153792DC8D"    // The ID of the Sendbird application which UIKit sample app uses.
    SendbirdUI.initialize(applicationId: APP_ID) {
        // Do something to display the start of the SendbirdUIKit initialization.
    } migrationHandler: {
        // Do something to display the progress of the DB migration.
    } completionHandler: { error in
        // Do something to display the completion of the SendbirdChat initialization.
    }
    
}

注意:在上文中,您应将 APP_ID 替换为您的 Sendbird 应用程序的 ID。

设置当前用户

在启动 Sendbird UIKit 之前,您必须在 SBUGlobal 中将用户信息设置为 currentUser。这些信息将在套件中用于各种任务。必须指定 userId 字段,而其他字段,如 nicknameprofileURL,是可选的,如果未指定,则使用默认值。

如下通过 AppDelegate 为 UIKit 设置 currentUser

注意:即使你没有使用 AppDelegate,在启动聊天服务前也应注册用户信息。

// AppDelegate.swift

import SendbirdUIKit

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
    
    // Case 1: USER_ID only
    SBUGlobals.currentUser = SBUUser(userId: {USER_ID})
    
    // Case 2: Specify all fields
    SBUGlobals.currentUser = SBUUser(userId: {USER_ID}, nickname:{(opt)NICKNAME} profileURL:{(opt)PROFILE_URL})
    
}

注意:如果未提前设置 currentUser,则会对您使用 UIKit 的限制。

频道列表

UIKit 允许你创建专门用于一对一聊天的频道,并可以列出一对一聊天频道,以便你可以轻松查看和管理。使用 SBUChannelListViewController 类,你可以为终端用户提供一个完整的聊天服务,包括列出频道功能。

将以下代码放入你想要启动 UIKit 的任何位置。

import SendbirdUIKit

let groupChannelListVC = SBUGroupChannelListViewController()
let naviVC = UINavigationController(rootViewController: groupChannelListVC)
self.present(naviVC, animated: true)

注意:如果你已经使用了一个导航控制器,你可以使用 pushViewController 函数。

注意在此阶段,你可以通过运行你的客户端应用程序来确认服务是否正常运行。

频道

使用《SBUGroupChannelViewController》类,您可以构建基于频道的服务而不是基于列表的服务。

注意:要运行基于频道的服务,您应该有一个Channel对象或一个ChannelURL

使用以下代码来实现聊天服务。

import SendbirdUIKit

let channelVC = SBUGroupChannelViewController(channelURL: {CHANNEL_URL})
let naviVC = UINavigationController(rootViewController: channelVC)
present(naviVC, animated: true)

分发设置

UIKit以重量级二进制文件的形式分发,其中包含关于模拟器设备架构的信息。如果您计划将应用程序分发到App Store并希望在应用程序的构建过程中删除不必要的架构,请添加以下脚本。

转到Xcode项目中目标的“构建阶段”选项卡。然后,单击+并选择“新建运行脚本阶段”。追加此脚本。

APP_PATH="${TARGET_BUILD_DIR}/${WRAPPER_NAME}"

# This script loops through the frameworks embedded in the application and
# removes unused architectures.
find "$APP_PATH" -name '*.framework' -type d | while read -r FRAMEWORK
do
    FRAMEWORK_EXECUTABLE_NAME=$(defaults read "$FRAMEWORK/Info.plist" CFBundleExecutable)
    FRAMEWORK_EXECUTABLE_PATH="$FRAMEWORK/$FRAMEWORK_EXECUTABLE_NAME"
    echo "Executable is $FRAMEWORK_EXECUTABLE_PATH"
    
    EXTRACTED_ARCHS=()
    
    for ARCH in $ARCHS
    do
        echo "Extracting $ARCH from $FRAMEWORK_EXECUTABLE_NAME"
        lipo -extract "$ARCH" "$FRAMEWORK_EXECUTABLE_PATH" -o "$FRAMEWORK_EXECUTABLE_PATH-$ARCH"
        EXTRACTED_ARCHS+=("$FRAMEWORK_EXECUTABLE_PATH-$ARCH")
    done
    
    echo "Merging extracted architectures: ${ARCHS}"
    lipo -o "$FRAMEWORK_EXECUTABLE_PATH-merged" -create "${EXTRACTED_ARCHS[@]}"
    rm "${EXTRACTED_ARCHS[@]}"
    
    echo "Replacing original executable with thinned version"
    rm "$FRAMEWORK_EXECUTABLE_PATH"
    mv "$FRAMEWORK_EXECUTABLE_PATH-merged" "$FRAMEWORK_EXECUTABLE_PATH"
done

UIKit简介

UIKit为iOS管理其ViewController以及来自iOS Chat SDK的各种视图和数据。UIKit组件包括以下内容

组件 描述
SBUGroupChannelListViewController 管理分组频道列表的ViewController
SBUGroupChannelViewController 管理1对n组聊天的频道。ViewController
SBUOpenChannelViewController 管理公开聊天频道。ViewController
SBUCreateChannelViewController 创建频道的ViewController
SBUInviteUserViewController 邀请用户加入频道的ViewController
SBURegisterOperatorViewController 在频道中注册为操作员的ViewController
SBUUserListViewController 显示频道成员或参与者的列表。ViewController
SBUGroupChannelSettingsViewController 配置分组频道的ViewController
SBUOpenChannelSettingsViewController 配置公开聊天的ViewController
SBUModerationsViewController 管理频道的ViewController
SBUMessageSearchViewController 在频道中搜索消息的ViewController
SBUTheme 管理主题的单例。
SBUColorSet 管理颜色集的单例。
SBUFontSet 管理字体集的单例。
SendbirdUI 包含在使用Sendbird UIKit时所需的静态函数的类。
SBUGlobalSet 一个包含使用Sendbird UIKit时所需的静态属性的类。