实现收发消息
本文将指导你如何利用 RTM Objective-C SDK 构建简单的应用程序,内容涵盖了集成和开发的基础知识:开通声网账号、获取 SDK、发送消息、接收消息等。
准备工作
开始构建项目前,你需要先完成一些准备工作。检查你的浏览器是否满足平台支持中的最低版本要求,然后依次完成以下步骤。
1. 注册账号
使用声网服务的第一步是注册声网账号,点击注册账号前往控制台注册。如果你已经拥有声网账号,那么你可以直接登录控制台。
你可以在控制台中查看项目配置、用量分析、账单报表等所有项目相关的信息,请务必保管好你的声网账号和密码。
2. 创建项目
成功登录控制台后,按照以下步骤创建一个声网项目:
-
点击左侧导航栏的项目管理按钮进入项目管理页面。
-
在项目管理页面,点击创建项目按钮。
-
在弹出的对话框内输入项目名称,选择产品为云信令(原实时消息)、鉴权机制为调试模式:APP ID。

- 点击创建项目,新建的项目就会显示在项目管理页中。
3. 获取 App ID
每个项目会被分配一个 App ID 作为项目唯一标识。在项目管理页面找到你的项目,点击 App ID 一栏右侧的复制按钮进行复制,后续的步骤中我们将会使用到它。

构建项目
1. 项目配置
根据以下步骤配置你的项目:
-
在 Xcode 中创建一个 iOS 平台下的 Single View App,项目设置如下:
- Product Name 设为
RtmQuickstart
。 - Organization Identifier 设为
agora
。 - User Interface 选择 Storyboard。
- Language 选择 Objective-C。
- Product Name 设为
-
通过以下任意一种方式获取最新的 RTM Objective-C SDK 并导入你的项目。
- 使用 CDN
- 使用 Cocoapods
- 点击此处下载最新版本的 Objective-C SDK。
- 复制 SDK 包中的
/libs/AgoraRtmKit.xcframework
文件至项目路径下。 - 打开 Xcode,进入 TARGETS > Project Name > General > Frameworks, Libraries, and Embedded Content 菜单。
- 点击 + > Add Other… > Add Files 添加
AgoraRtmKit.xcframework
动态库,并确保添加的动态库 Embed 属性设置为 Embed & Sign。
添加完成后,项目会自动链接所需系统库。
-
开始前请确保你已安装 Cocoapods,如尚未安装 Cocoapods,参考 Getting Started with CocoaPods 安装说明。
-
在终端里进入项目根目录,并运行
pod init
命令。项目文件夹下会生成一个Podfile
文本文件。 -
打开
Podfile
文件,修改文件为如下内容。注意将Your App
替换为你的 Target 名称。Rubyplatform :ios, '9.0'
target 'Your App' do
# 将 x.y.z 替换为具体的 SDK 版本号,如 2.1.4
# 可通过发版说明获取最新版本号
pod 'AgoraRtm_iOS', 'x.y.z'
end -
在终端内运行
pod install
命令安装声网 SDK。成功安装后,Terminal 中会显示Pod installation complete!
。 -
成功安装后,项目文件夹下会生成一个后缀为
.xcworkspace
的文件,通过 Xcode 打开该文件进行后续操作。
2. 初始化 RTM
调用 RTM SDK 的任何 API 之前,需要先初始化一个 AgoraRtmClientKit
对象实例。参考如下步骤创建用户界面、初始化一个 AgoraRtmClientKit
对象实例。
-
创建用户界面。实时消息 app 一般会有如下输入框和按钮:
- 用户名、频道名、消息输入框
- 登录、登出按钮
- 订阅、取消订阅频道按钮
- 发送消息按钮
本节提供简易的 UI 界面设计以便于验证功能特性,你可以根据实际项目需求进行修改。
-
使用 Code View 打开
Main.storyboard
并将文件内容替换为以下代码: -
打开
ViewController.h
文件,将文件内容替换为如下代码:
-
打开
ViewController.m
文件,将文件内容替换为如下代码。你会发现此程序并不完整,不用紧张,我们在后续步骤中将一步步指导你补充完善代码。注意你需要将示例中的
your_appid
和your_token
字段替换成你项目的 App ID 和 Token。在测试阶段,为快速验证功能,你可以将your_appid
和your_token
字段都替换成你项目的 App ID。
如需了解更多初始化的信息,查看初始配置。
3. 添加事件监听
事件监听程序帮助你实现频道中消息、事件到达后的处理逻辑,添加以下代码到你的程序中以显示收到的消息或事件通知:
// Paste the following code snippet below "Add the event listener" comment
- (void)rtmKit:(AgoraRtmClientKit *)rtmKit didReceiveMessageEvent:(AgoraRtmMessageEvent *)event {
NSLog(@"%@", self.text);
self.text = [NSString stringWithFormat:@"receive message\nFrom channel: %@\npublisher:%@\nmessage:%@\n",event.channelName,event.publisher, event.message.stringData];
[self AddMsgToRecord:(self.text)];
}
4. 登录服务
你需要执行登录操作才能建立与 RTM 服务器的连接,然后才能调用 SDK 的其他 API。将以下代码添加到程序中:
// Paste the following code snippet below "Log in the RTM server" comment
[_kit loginByToken:self.token completion:^(AgoraRtmCommonResponse * _Nullable response, AgoraRtmErrorInfo * _Nullable errorInfo) {
if (errorInfo.errorCode != AgoraRtmErrorOk){
self.text = [NSString stringWithFormat:@"Login failed for user %@. Code: %ld",self.uid, (long)errorInfo.errorCode];
NSLog(@"%@", self.text);
}else {
NSLog(@"%@", self.text);
self.text = [NSString stringWithFormat:@"Login successful for user %@. Code: %ld",self.uid, (long)errorInfo.errorCode];
}
[self AddMsgToRecord:(self.text)];
}];
5. 收发消息
调用 publish
方法向 Message Channel 发送消息后,RTM 会把该消息分发给该频道的所有订阅者,以下代码演示如何发送字符串类型的消息,将此代码片段添加到程序中:
你需要先对消息负载进行字符串序列化,才能调用 publish
方法发送消息。
// Paste the following code snippet below "Publish a message" comment
[_kit publish:self.channelID message:self.channelMsg option:nil completion:^(AgoraRtmCommonResponse * _Nullable response, AgoraRtmErrorInfo * _Nullable errorInfo) {
if (errorInfo == nil)
{
self.text = [NSString stringWithFormat:@"Message sent to channel %@ : %@", self.channelID, self.channelMsg]; }
else
{
self.text = [NSString stringWithFormat:@"Message failed to send to channel %@ : %@ ErrorCode: %ld", self.channelID, self.channelMsg, (long)errorInfo.errorCode]; }
[self AddMsgToRecord:(self.text)];
}];
调用 subscribeWithChannel
方法订阅此频道以接收此频道中的消息。将以下代码添加到程序中:
// Paste the following code snippet below "Subscribe to a channel" comment
[_kit subscribeWithChannel:self.channelID option:nil completion:^(AgoraRtmCommonResponse * _Nullable response, AgoraRtmErrorInfo * _Nullable errorInfo) {
if(errorInfo == nil) {
self.text = [NSString stringWithFormat:@"Successfully subscribe channel %@",self.channelID];
NSLog(@"%@", self.text);
} else {
self.text = [NSString stringWithFormat:@"Failed to subscribe channel %@ Code: %ld",self.channelID, (long)errorInfo.errorCode];
NSLog(@"%@", self.text);
}
[self AddMsgToRecord:(self.text)];
}];
如果你不再需要在此频道收发消息,你可以调用 unsubscribeWithChannel
方法取消订阅此频道。将以下代码添加到程序中:
// Paste the following code snippet below "Unsubscribe from a channel" comment
[_kit unsubscribeWithChannel:self.channelID completion:^(AgoraRtmCommonResponse * _Nullable response, AgoraRtmErrorInfo * _Nullable errorInfo) {
if (errorInfo == nil){
self.text = [NSString stringWithFormat:@"Leave channel successful"];
} else {
self.text = [NSString stringWithFormat:@"Failed to leave channel Code: %ld", (long)errorInfo.errorCode]; }
[self AddMsgToRecord:(self.text)];
}];
如需了解更多收发消息的信息,查看消息。
6. 登出服务
当不再需要使用 RTM 服务时,你可以调用 logout
方法登出 RTM 系统。将以下代码添加到程序中:
// Paste the following code snippet below "Log out from the RTM server" comment
[_kit logout:^(AgoraRtmCommonResponse * _Nullable response, AgoraRtmErrorInfo * _Nullable errorInfo) {
if (errorInfo == nil){
self.text = [NSString stringWithFormat:@"Logout successful"];
NSLog(@"%@", self.text);
[_kit destroy];
_kit = nil;
} else {
self.text = [NSString stringWithFormat:@"Logout failed. Code: %ld",(long)errorInfo.errorCode];
NSLog(@"%@", self.text); }
[self AddMsgToRecord:(self.text)];
}];
本操作会影响你账单中的 PCU 计费项。
7. 组合到一起
经过上述步骤,你程序中的代码应该如下所示:
现在,你可以开始运行你的程序:
- 保存项目。
- 复制当前项目,使用相同的
appId
和不同的userId
。 - 运行上面的两个项目。成功运行后,你可以在两个设备中看到 app 界面。
- 将一个设备作为接收端,进行如下操作:
- 输入用户名,点击 Login。
- 输入频道名,点击 Subscribe。
- 将另一个设备作为发送端,进行如下操作:
- 输入不同的用户名,点击 Subscribe。
- 输入相同的频道名。
- 输入消息,点击 Publish MSG。
- 将上面的发送端和设备端互换,重复步骤 4 和 5。
如果两个设备均可收发消息,那么你已经成功集成并正确使用了 RTM 服务。
贯穿始终
相比于介绍如何写出代码,声网更愿意帮助你掌握上述程序编写的过程和逻辑。上述程序依次完成了以下操作,让你可以正确地收发消息:
- 设置并初始化 RTM 对象。
- 添加
didReceiveMessageEvent
事件监听函数。 - 调用
loginByToken
登录了 RTM 服务。 - 调用
subscribeWithChannel
订阅了一个 Message Channel。 - 调用
publish
发送消息。 - 调用
unsubscribeWithChannel
取消订阅一个 Message Channel。 - 调用
logout
登出 RTM 系统。
下一步
现在,你已经学会了如何使用 RTM Objective-C SDK 来实现 Message Channel 的收发消息功能。下一步,你可以通过 SDK 的 API 参考了解更多功能的使用方法。