iOS-融云的部分功能集成

1、导入融云的SDK


融云SDK.png

2、添加framework


融云framework.png

3、添加融云API的管理类RongCloudManage
#import <Foundation/Foundation.h>

#import <RongIMKit/RongIMKit.h>
@interface RongCloudManage : NSObject <RCIMUserInfoDataSource, RCIMGroupInfoDataSource>

@property (nonatomic, strong) NSMutableArray *friendsArray;//好友数组
@property (nonatomic, strong) NSMutableArray *groupsArray; //群组数组

//单例类
+ (instancetype)shareDataManage;

//登录
-(void)loginRongCloudWithUserInfo:(RCUserInfo *)userInfo withToken:(NSString *)token;

//通过userid得到RCUserInfo
- (void)getUserInfoWithUserId:(NSString*)userId completion:(void (^)(RCUserInfo*))completion;

//好友中是否有这个userid
-(BOOL)hasTheFriendWithUserId:(NSString *)userId;

//群组中是否有这个groupid
- (BOOL)hasTheGroupWithGroupId:(NSString *)groupId;

// 从服务器同步好友列表
-(void) syncFriendList:(void (^)(NSMutableArray * friends,BOOL isSuccess))completion;

// 从服务器同步群组列表
-(void) syncGroupList:(void (^)(NSMutableArray * groups,BOOL isSuccess))completion;

//刷新badge
-(void) refreshBadgeValue;

//根据用户参数的id获取当前用户的name
-(NSString *)currentNameWithUserId:(NSString *)userId;

//根据用户参数的id(userId)获取RCUserInfo
-(RCUserInfo *)currentUserInfoWithUserId:(NSString *)userId;

//根据群组的id(groupId)获取RCGroup
-(RCGroup *)currentGroupInfoWithGroupId:(NSString *)groupId;
@end
#import "RongCloudManage.h"
#import "AppDelegate.h"
#import "RootTabBarController.h"
@implementation RongCloudManage
{
    NSMutableArray *dataSoure;
    
    NSArray *_userIdArray;
    NSArray *_nameArray;
    NSArray *_urlArray;
    NSArray *_tokenArray;
}
- (instancetype)init
{
    if (self = [super init]) {
        [RCIM sharedRCIM].userInfoDataSource = self;
        [RCIM sharedRCIM].groupInfoDataSource = self;
    }
    return self;
}
+ (instancetype)shareDataManage
{
    static RongCloudManage *manage = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        manage = [[RongCloudManage alloc] init];
    });
    return manage;
}

#pragma mark - loginRogCloud
-(void)loginRongCloudWithUserInfo:(RCUserInfo *)userInfo withToken:(NSString *)token{
    [[RCIM sharedRCIM] connectWithToken:token success:^(NSString *userId) {
        [RCIM sharedRCIM].globalNavigationBarTintColor = [UIColor redColor];
        NSLog(@"login success with userId %@",userId);
        //同步好友列表
        [self syncFriendList:^(NSMutableArray *friends, BOOL isSuccess) {
//            NSLog(@"%@",friends);
            if (isSuccess) {
                [self syncGroupList:^(NSMutableArray *groups, BOOL isSuccess) {
                    if (isSuccess) {
                        NSLog(@" success 发送通知");
                        [[NSNotificationCenter defaultCenter] postNotificationName:@"alreadyLogin" object:nil];
                    }
                }];
            }
        }];
        [RCIMClient sharedRCIMClient].currentUserInfo = userInfo;
        [[RongCloudManage shareDataManage] refreshBadgeValue];
    } error:^(RCConnectErrorCode status) {
        NSLog(@"status = %ld",(long)status);
    } tokenIncorrect:^{
        NSLog(@"token 错误");
    }];
}

- (void)createDateSource
{
    
    _userIdArray = @[@"13579",
                     @"12888",
                     @"12345",
                     @"54321",
                     @"87654321",
                     @"123456",
                     @"654321",];
    _nameArray = @[@"hello_one",
                   @"hello_two",
                   @"hello_three",
                   @"猴赛雷",
                   @"妹子",
                   @"123456",
                   @"654321",];
    
    _urlArray = @[@"http://weixin.ihk.cn/ihkwx_upload/fodder/20151210/1449727866527.jpg",
                  @"http://weixin.ihk.cn/ihkwx_upload/fodder/20151210/1449727755947.jpg",
                  @"http://pic.ihk.cn/head/base/ihk/2014/04/30/213816839.jpg",
                  @"http://pic.ihk.cn/head/80_93/ihk/2015/05/20/104331022.jpg",
                  @"http://weixin.ihk.cn/ihkwx_upload/fodder/20151218/1450420944608.jpg",
                  @"123456",
                  @"654321",];
    
    _tokenArray = @[@"aiAxITW3kbJg7Ayy9ynaU5ciKm2nZw8edLjLs0YBrzGXTgFE/7jwYcLCUBKKKC4Wn/e/+Oa/M+Fw4Wu0M0Iyfw==",
                    @"bYqaXADdV3GCCqm7eFRbeJciKm2nZw8edLjLs0YBrzGXTgFE/7jwYXQZci6kNiGdS0okiEM9x1nIpiIsgpVQNA==",
                    @"WKEnT6Ir0b6GlD7nW+z/cJciKm2nZw8edLjLs0YBrzGXTgFE/7jwYfo/iUQapWcahM/YZxEzSoF2xFDsI935wg==",
                    @"uB98Blf86jDhuTXcs9RpXNBkzAOozj9F+pyFMVI7MpSkT/x/daJtb0PC6GPnRWihxK+d1jfut+xBstcUxIOpUQ==",
                    @"eZ8Q9k7+16LW5j6iIYvJvpciKm2nZw8edLjLs0YBrzGXTgFE/7jwYfBSKHbePSQzj2GZjW47L0wcJJ7rq10J7K8vdcu/4QoY",
                    @"tYzEy49VLDjgxcVYPnEMedBkzAOozj9F+pyFMVI7MpTRis9jOGS/fwKnzxyDH7hNr3dhmyLSPYrPutrE54DbYQ==",
                    @"Nly/jQvunT48ceC7FVIr/o48JsPo610z1NcuN8qpfMOhYvO8Cbxa5QTrO1asCXNZuFWMAujpZII3y0gALhIGAA==",];
}

-(void)refreshBadgeValue
{
    dispatch_async(dispatch_get_main_queue(), ^{

        NSInteger unreadMsgCount = (NSInteger)[[RCIMClient sharedRCIMClient] getUnreadCount:@[@(ConversationType_PRIVATE), @(ConversationType_GROUP)]];


        RootTabBarController *root = (id)[UIApplication sharedApplication].delegate.window.rootViewController;
        UINavigationController *chatNav = root.viewControllers[1];
        if (unreadMsgCount == 0) {
            chatNav.tabBarItem.badgeValue = nil;
        }else{
            chatNav.tabBarItem.badgeValue = [NSString stringWithFormat:@"%li",(long)unreadMsgCount];
        }
    });
}
-(BOOL)hasTheFriendWithUserId:(NSString *)userId
{
    
    if (self.friendsArray) {
        NSMutableArray *tempArray = [[NSMutableArray alloc]init];
        
        for (RCUserInfo *aUserInfo in self.friendsArray) {
            [tempArray addObject:aUserInfo.userId];
        }
        
        if ([tempArray containsObject:userId]) {
            return YES;
        }
    }
    return NO;
}

- (BOOL)hasTheGroupWithGroupId:(NSString *)groupId
{
    NSLog(@"group-->%@<--", self.groupsArray);
    if (self.groupsArray.count) {
        NSMutableArray *tempArray = [[NSMutableArray alloc] init];
        for (RCGroup *aGroupInfo in self.groupsArray) {
            [tempArray addObject:aGroupInfo.groupId];
        }
        if ([tempArray containsObject:groupId]) {
            return YES;
        }
    }
    return NO;
}
/**
 *  从服务器同步好友列表
 */
-(void)syncFriendList:(void (^)(NSMutableArray* friends,BOOL isSuccess))completion
{
    dataSoure = [[NSMutableArray alloc]init];
    
    for (NSInteger i = 0; i<7; i++) {
        
        RCUserInfo *aUserInfo =[[RCUserInfo alloc]initWithUserId:_userIdArray[i] name:_nameArray[i] portrait:_urlArray[i]];
        [dataSoure addObject:aUserInfo];
        
    }
    self.friendsArray = dataSoure;
    completion(dataSoure,YES);
    
}
/**
 *  从服务器同步群组列表
 */
-(void) syncGroupList:(void (^)(NSMutableArray * groups,BOOL isSuccess))completion{
    if (self.groupsArray) {
        [self.groupsArray removeAllObjects];
    }
    NSMutableArray *_dataSourceGroup = [[NSMutableArray alloc] init];
    for (NSInteger i = 1; i<3; i++) {
        if (i==1){
            RCGroup *aGroup = [[RCGroup alloc]initWithGroupId:@"8888888" groupName:@"医食保健群_an" portraitUri:@"http://farm2.staticflickr.com/1715/23815656639_ef86cf1498_m.jpg"];
            [_dataSourceGroup addObject:aGroup];
            
        }
        else if (i==2){
            RCGroup *aGroup = [[RCGroup alloc]initWithGroupId:@"66666666" groupName:@"青龙帮" portraitUri:@"http://farm2.staticflickr.com/1455/23888379640_edf9fce919_m.jpg"];
            [_dataSourceGroup addObject:aGroup];
        }
    }
    //    NSLog(@"111->>>%@", _dataSourceGroup);
    self.groupsArray = _dataSourceGroup;
    completion(self.groupsArray,YES);
    
}
#pragma mark
#pragma mark 根据userId获取RCUserInfo
-(RCUserInfo *)currentUserInfoWithUserId:(NSString *)userId{
    for (NSInteger i = 0; i<self.friendsArray.count; i++) {
        RCUserInfo *aUser = self.friendsArray[i];
        if ([userId isEqualToString:aUser.userId]) {
            NSLog(@"current = %@",aUser.name);
            return aUser;
        }
    }
    return nil;
}
#pragma mark
#pragma mark 根据userId获取RCGroup
-(RCGroup *)currentGroupInfoWithGroupId:(NSString *)groupId{
    for (NSInteger i = 0; i<self.groupsArray.count; i++) {
        RCGroup *aGroup = self.groupsArray[i];
        if ([groupId isEqualToString:aGroup.groupId]) {
            return aGroup;
        }
    }
    return nil;
}
-(NSString *)currentNameWithUserId:(NSString *)userId{
    for (NSInteger i = 0; i < self.friendsArray.count; i++) {
        RCUserInfo *aUser = self.friendsArray[i];
        if ([userId isEqualToString:aUser.userId]) {
            NSLog(@"current = %@",aUser.name);
            return aUser.name;
        }
    }
    return nil;
}

#pragma mark - RCIMUserInfoDataSource
- (void)getUserInfoWithUserId:(NSString*)userId completion:(void (^)(RCUserInfo*))completion
{
    NSLog(@"getUserInfoWithUserId ----- %@", userId);
    
    if (userId == nil || [userId length] == 0 )
    {
        [self syncFriendList:^(NSMutableArray *friends, BOOL isSuccess) {
            
        }];
        
        completion(nil);
        return ;
    }
    
    if ([userId isEqualToString:[RCIM sharedRCIM].currentUserInfo.userId]) {
        RCUserInfo *myselfInfo = [[RCUserInfo alloc]initWithUserId:[RCIM sharedRCIM].currentUserInfo.userId name:[RCIM sharedRCIM].currentUserInfo.name portrait:[RCIM sharedRCIM].currentUserInfo.portraitUri];
        completion(myselfInfo);
        
    }
    
    for (NSInteger i = 0; i < self.friendsArray.count; i++) {
        RCUserInfo *aUser = self.friendsArray[i];
        if ([userId isEqualToString:aUser.userId]) {
            completion(aUser);
            break;
        }
    }
}

#pragma mark - RCIMGroupInfoDataSource
- (void)getGroupInfoWithGroupId:(NSString *)groupId
                     completion:(void (^)(RCGroup *groupInfo))completion{
    for (NSInteger i = 0; i < self.groupsArray.count; i++) {
        RCGroup *aGroup = self.groupsArray[i];
        if ([groupId isEqualToString:aGroup.groupId]) {
            completion(aGroup);
            break;
        }
    }
}

3、在AppDelegate.m文件里面进行的操作

- (void)initRongCloud
{
    [[RCIM sharedRCIM] initWithAppKey:@"8w7jv4qb7hp2y"];
    [RCIM sharedRCIM].enableMessageAttachUserInfo = YES;
    [[RongCloudManage shareDataManage] loginRongCloudWithUserInfo:[[RCUserInfo alloc] initWithUserId:@"12345" name:@"hello_three" portrait:@"http://pic.ihk.cn/head/base/ihk/2014/04/30/213816839.jpg"] withToken:@"WKEnT6Ir0b6GlD7nW+z/cJciKm2nZw8edLjLs0YBrzGXTgFE/7jwYfo/iUQapWcahM/YZxEzSoF2xFDsI935wg=="];
}

4、会话列表类

#import <UIKit/UIKit.h>
#import <RongIMKit/RongIMKit.h>
@interface MessageController : RCConversationListViewController
@end
#import "MessageController.h"

@interface MessageController ()

@end

@implementation MessageController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setDisplayConversationTypes:@[@(ConversationType_PRIVATE), @(ConversationType_GROUP)]];
    
    [self setNavigationBar];
    
    self.conversationListTableView.tableFooterView = [UIView new];
}

- (void)setNavigationBar
{
    self.view.backgroundColor = [UIColor colorWithRed:0.9295 green:0.9246 blue:0.9345 alpha:1.0];
    
    self.navigationItem.title = @"消息";
    [self.navigationController.navigationBar setTitleTextAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:20], NSForegroundColorAttributeName:[UIColor whiteColor]}];
    self.navigationController.navigationBar.barTintColor = [UIColor colorWithRed:0.2945 green:0.5072 blue:1.0 alpha:1.0];
    
}
//点击cell,跳转到好友或者群组界面
- (void)onSelectedTableRow:(RCConversationModelType)conversationModelType conversationModel:(RCConversationModel *)model atIndexPath:(NSIndexPath *)indexPath
{
//如果是群组类型
    if (model.conversationType == ConversationType_GROUP) {
        RCConversationViewController *rcConversationVC = [[RCConversationViewController alloc] initWithConversationType:model.conversationType targetId:model.targetId];
        
        RCGroup *group = [[RongCloudManage shareDataManage] currentGroupInfoWithGroupId:model.targetId];
        rcConversationVC.title = group.groupName;
        NSLog(@"group-->%@-->%@", model.conversationTitle, group.groupName);
        [self.navigationController pushViewController:rcConversationVC animated:YES];
    }
//如果是好友类型
    else if (model.conversationType == ConversationType_PRIVATE) {
        RCConversationViewController *rcConversationVC = [[RCConversationViewController alloc] initWithConversationType:model.conversationType targetId:model.targetId];
        rcConversationVC.title = model.conversationTitle;
        NSLog(@"private-->%@", model.conversationTitle);
        [self.navigationController pushViewController:rcConversationVC animated:YES];
    }
    
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 206,126评论 6 481
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 88,254评论 2 382
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 152,445评论 0 341
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 55,185评论 1 278
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 64,178评论 5 371
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,970评论 1 284
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 38,276评论 3 399
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,927评论 0 259
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 43,400评论 1 300
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,883评论 2 323
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,997评论 1 333
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,646评论 4 322
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 39,213评论 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 30,204评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,423评论 1 260
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 45,423评论 2 352
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,722评论 2 345

推荐阅读更多精彩内容

  • 写在前面融云是一个比较强大的第三方框架,为我们提供了即时通讯的基本组件,导入SDK,进行一些简单的配置就可以看到会...
    汪小喵阅读 14,667评论 14 31
  • 相信大家在项目中会用到即时通讯功能,自己去写的话会需要前后台合作,会大大加大开发的周期,所以考虑使用第三方的即时通...
    ProgrammerChan阅读 8,407评论 16 38
  • 点击查看原文 Web SDK 开发手册 SDK 概述 网易云信 SDK 为 Web 应用提供一个完善的 IM 系统...
    layjoy阅读 13,671评论 0 15
  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 171,498评论 25 707
  • 继上一张上色的无奈之后,来一张黑白哒。这是一副,我都数不清画了多少小精灵的画。为了临摹,仔细看了原作品的每一个部分...
    Hi彤彤阅读 572评论 0 1