测试已测试 | ✗ |
Lang语言 | Obj-CObjective C |
许可 | MIT |
发布最后发布 | 2016年1月 |
由lzwjava、ChenYilong 维护。
依赖 | |
AVOSCloud | >= 0 |
AVOSCloudIM | >= 0 |
JSBadgeView | = 1.4.1 |
DateTools | = 1.5.0 |
FMDB | = 2.5 |
SDWebImage | >= 0 |
LeanChat 已在 App Store 上架,您可通过 https://itunes.apple.com/gb/app/leanchat/id943324553 或在 App Store 中搜索 LeanChat 来访问。
这个示例项目全面展示了 LeanCloud 实时通讯功能的应用,但其中混合了许多 UI 代码和其他功能,不适合快速入门学习,如果你是第一次接触 LeanMessage,更推荐 LeanMessage-Demo 项目。熟悉后,可前往 LeanCloud-Demos 选择你喜欢的 IM 皮肤进行集成。在集成过程中遇到难题,不妨再次参考 LeanChat 项目。
如果您有任何问题,请提出 issue,并写下您不清楚的地方,我会尽快提供帮助。
请直接点击 Github 上的Download Zip
,如图所示,这样只下载最新版。如果是 git clone
,则可能非常慢,因为它包含了大量的提交历史。某次测试两者的下载速度是 1.5M:40M。
// LeanChat (复杂例子)
cd LeanChat
pod install --verbose // 如果本地有 AVOSCloud 依赖库,可加选项 --no-repo-update 加快速度
open LeanChat.workspace
// LeanChatExample (简单例子)
cd LeanChatExample
pod install --verbose --no-repo-update
open LeanChatExample.xcworkspace
// LeanChatSwift (Swift 例子)
cd LeanChatSwift
pod install --verbose --no-repo-update
open LeanChatSwift.xcworkspace
// LeanChatLib (封装了 LeanCloud 通信组件 和 UI 的库)
cd LeanChatLib
pod install --verbose --no-repo-update
open LeanChatLib.xcworkspace
如果在编译过程中遇到definition of 'AVUser' must be imported from module 'LeanChatLib.CDChatListVC' before it is required
类似的问题,可以在菜单 Product 中按住 Option,然后点击 Clean Build Folder 清空所有 Build 文件,然后重新编译即可。这个问题似乎是 Cocoapods 在进行复杂编译时出现的 Bug。具体可以看这个 Gif。
请注意,由于默认使用了生产证书,所以在开发时,离线消息没有推送。而线上版本是有推送的,您可以在 App Store 上下载。具体也可以参考这个 issue。
这里可以看到三个项目,具体介绍如下。
封装了最近对话页面和聊天页面,LeanChat 和 LeanChatExample 项目都依赖于它。可以通过以下方式安装
pod 'LeanChatLib'
大多数情况下,你会通过拖拽源代码的方式集成 LeanChatLib。在这种情况下,首先需要安装 AVOSCloud.framework
和 AVOSCloudIM.framework
,如果不是使用 pod install 'AVOSCloud'
和 pod install 'AVOSCloudIM'
来安装的话,则可以根据 LeanCloud 的 快速入门指南 配置 SDK 所需的 Framework。同时安装另外两个依赖库 JSBadgeView
与 DateTools
。在执行 pod install
运行本 Demo 的时候,会生成 Pods 目录,你可以在其中找到这两个 Pods。或者你也可以在网上搜索。另外,可以通过 .podspec 文件 来配置,podspec 描述了需要集成的源文件、需要引入的系统 framework 等等。或者你可以参考这个 工单。
应用启动后,初始化,并配置 IM User
[AVOSCloud setApplicationId:@"YourAppId" clientKey:@"YourAppKey"];
[CDChatManager manager].userDelegate = [[CDUserFactory alloc] init];
配置一个 UserFactory,遵守 CDUserDelegate 协议即可。
#import "CDUserFactory.h"
#import <LeanChatLib/LeanChatLib.h>
@interface CDUserFactory ()<CDUserDelegate>
@end
@implementation CDUserFactory
#pragma mark - CDUserDelegate
- (void)cacheUserByIds:(NSSet *)userIds block:(AVIMArrayResultBlock)block{
block(nil,nil); // don't forget it
}
- (id<CDUserModelDelegate>)getUserById:(NSString *)userId {
CDUser *user = [[CDUser alloc] init];
user.userId = userId;
user.username = userId;
user.avatarUrl = @"http://image17-c.poco.cn/mypoco/myphoto/20151211/16/17338872420151211164742047.png";
return user;
}
@end
这里的 CDUser 是应用内的 User 对象,你可以在你的 User 对象中实现 CDUserModelDelegate 协议。
CDUserModelDelegate 协议内容如下:
@protocol CDUserModelDelegate <NSObject>
@required
- (NSString*)userId;
- (NSString*)avatarUrl;
- (NSString*)username;
@end
登录时调用:
[[CDChatManager manager] openWithClientId:selfId callback: ^(BOOL succeeded, NSError *error) {
if (error) {
DLog(@"%@", error);
}
else {
//go Main Controller
}
}];
与某人聊天:
[[CDChatManager manager] fetchConversationWithOtherId : otherId callback : ^(AVIMConversation *conversation, NSError *error) {
if (error) {
DLog(@"%@", error);
}
else {
LCEChatRoomVC *chatRoomVC = [[LCEChatRoomVC alloc] initWithConversaion:conversation];
[weakSelf.navigationController pushViewController:chatRoomVC animated:YES];
}
}];
与多人群聊:
NSMutableArray *memberIds = [NSMutableArray array];
[memberIds addObject:groupId1];
[memberIds addObject:groupId2];
[memberIds addObject:[CDChatManager manager].selfId];
[[CDChatManager manager] fetchConversaionWithMembers:memberIds callback: ^(AVIMConversation *conversation, NSError *error) {
if (error) {
DLog(@"%@", error);
}
else {
LCEChatRoomVC *chatRoomVC = [[LCEChatRoomVC alloc] initWithConversation:conversation];
[weakSelf.navigationController pushViewController:chatRoomVC animated:YES];
}
}];
注销时:
[[CDChatManager manager] closeWithCallback: ^(BOOL succeeded, NSError *error) {
}];
之后,就可以像上面截图那样进行聊天了。注意,目前我们不推荐直接使用 pod 方式引入 LeanChatLib,因为有些界面和功能需要你来自定义,所以我们推荐将 LeanChatLib 的代码复制到项目中,这样修改起来更加方便。
0.2.6
将 SDK 升级至 3.1.4,适配 iOS 9
0.2.5
使用 AVIMConversationQuery 里的 cachePolicy,节省流量,更好地支持离线。修复当调用 fecthConversationWithConversationId 时对话不存在可能崩溃的 Bug。
0.2.4
增加兔斯基表情
0.2.3
增加 fetchConversationWithMembers: 接口的参数校验,修复对话列表在单聊对话但只有一个成员时可能出现的崩溃。
0.2.2
AVOSCloud 库升级至 3.1.2.8
0.2.1
ChatListDelegate 增加 configureCell: 和 prepareConversaion: 接口,以便实现更复杂的对话定制。
对于图像消息,使用 AVFile 来缓存图像,使得自己发送的照片无需重新下载。
0.2.0
补充注释,支持重发消息,显示失败的消息,增加音效和振动。
0.1.3
修复了快速下拉加载历史消息时崩溃的 Bug。
0.1.2
使用 SDK 的聊天缓存,去掉了 FMDB 依赖。可以看到服务器上的历史消息,重新安装后也可以看到历史聊天记录。去掉了 CDNotify 类。
0.1.1
重构
0.1.0
发布
如果要部署完整的 LeanChat,由于该应用包含添加好友的功能,请在设置->应用选项中勾选互相关注选项,以便在一方同意时,对方能互相添加好友。
感谢曾宪华大神的 MessageDisplayKit 开源库。