wzf / leanchat-ios

用 LeanCloud 实时通信组件做的社交应用 LeanChat

Home Page: https://itunes.apple.com/gb/app/leanchat/id943324553

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

leanchat

App Store

LeanChat 已经在 Apple Store 上架,可前往 https://itunes.apple.com/gb/app/leanchat/id943324553 或搜 LeanChat。

介绍

这个示例项目全面展示了 LeanCloud 实时通讯功能的应用,但含杂着许多 UI 代码和其它功能,并不适合快速上手学习,如果你第一次接触 LeanMessage,更推荐 LeanMessage-Demo 项目。等熟悉了之后,可前往 LeanCloud-Demos 挑选你喜欢的 IM 皮肤进行集成。集成的过程中,若遇到疑难问题,不妨再来参考 LeanChat 项目。

下载

请直接点击 Github 上的Download Zip,如图所示,这样只下载最新版。如果是 git clone,则可能非常慢,因为含杂很大的提交历史。某次测试两者是1.5M:40M。

qq20150618-2 2x

运行

  cd LeanChat
  pod install --verbose --no-repo-update // 拯救短暂的人生
  open LeanChat.workspace

这里可以看到三个项目,介绍如下。

子项目介绍

  • LeanChatLib ,核心的聊天逻辑和聊天界面库。有了它,可以快速集成聊天功能,支持文字、音频、图片、表情消息,消息通知。同时也有相应的 Android 版本
  • LeanChatExample,leanchatlib 最简单的使用例子。展示了如何用少量代码调用 LeanChatLib 来加入聊天,无论是用 LeanCloud 的用户系统还是自己的用户系统。
  • LeanChat-ios,为 LeanChat 整个应用。它包含好友管理、群组管理、地理消息、附近的人、个人页面、登录注册的功能,完全基于 LeanCloud 的存储和通信功能。它也是对 LeanChatLib 更复杂的应用。

分支

  • master 分支,使用了 LeanCloud 的实时通信服务2.0 (推荐)
  • v1 分支,使用了 LeanCloud 的实时通信服务 1.0

LeanChatLib 介绍

封装了最近对话页面和聊天页面,LeanChat 和 LeanChatExample 项目都依赖于它。可通过以下方式安装,

    pod 'LeanChatLib'

如何三步加入IM

  1. LeanCloud 中创建应用
  2. 加入 LeanChatLib 的 pod 依赖
  3. 依次在合适的地方加入以下代码,

应用启动后,初始化,以及配置 IM User

   [AVOSCloud setApplicationId: YourAppID clientKey: YourAppKey];
   [CDIM sharedInstance].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<CDUserModel>)getUserById:(NSString *)userId{
    CDUser* user=[[CDUser alloc] init];
    user.userId=userId;
    user.username=userId;
    user.avatarUrl=@"http://ac-x3o016bx.clouddn.com/86O7RAPx2BtTW5zgZTPGNwH9RZD5vNDtPm1YbIcu";
    return user;
}

@end

这里的 CDUser 是应用内的User对象,你可以在你的User对象实现 CDUserModel 协议即可。

CDUserModel,

@protocol CDUserModel <NSObject>

@required

-(NSString*)userId;

-(NSString*)avatarUrl;

-(NSString*)username;

@end

登录时调用,

        CDIM* im=[CDIM sharedInstance];
        [im openWithClientId:selfId callback:^(BOOL succeeded, NSError *error) {
            if(error){
                DLog(@"%@",error);
            }else{
                [self performSegueWithIdentifier:@"goMain" sender:sender];
            }
        }];

和某人聊天,

        [[CDIM sharedInstance] fetchConvWithOtherId:otherId callback:^(AVIMConversation *conversation, NSError *error) {
            if(error){
                DLog(@"%@",error);
            }else{
                LCEChatRoomVC* chatRoomVC=[[LCEChatRoomVC alloc] initWithConv:conversation];
                [weakSelf.navigationController pushViewController:chatRoomVC animated:YES];
            }
        }];

和多人群聊,

        CDIM* im=[CDIM sharedInstance];
        NSMutableArray* memberIds=[NSMutableArray array];
        [memberIds addObject:groupId1];
        [memberIds addObject:groupId2];
        [memberIds addObject:im.selfId];
        [im fetchConvWithMembers:memberIds callback:^(AVIMConversation *conversation, NSError *error) {
            if(error){
                DLog(@"%@",error);
            }else{
                LCEChatRoomVC* chatRoomVC=[[LCEChatRoomVC alloc] initWithConv:conversation];
                [weakSelf.navigationController pushViewController:chatRoomVC animated:YES];
            }
        }];

注销时,

    [[CDIM sharedInstance] closeWithCallback:^(BOOL succeeded, NSError *error) {
        DLog(@"%@",error);
    }];

然后,就可以像上面截图那样聊天了。

部署 LeanChat 需知

如果要部署完整的LeanChat的话,因为该应用有添加好友的功能,请在设置->应用选项中,勾选互相关注选项,以便一方同意的时候,能互相加好友。

qq20150407-5

开发指南

实时通信服务开发指南

Wiki

更多介绍

致谢

感谢曾宪华大神的 MessageDisplayKit 开源库。

About

用 LeanCloud 实时通信组件做的社交应用 LeanChat

https://itunes.apple.com/gb/app/leanchat/id943324553

License:MIT License


Languages

Language:Objective-C 99.0%Language:Ruby 0.9%Language:C 0.2%