美文网首页iOS开发技能
iOS-融云的部分功能集成

iOS-融云的部分功能集成

作者: BestVast | 来源:发表于2016-05-03 20:34 被阅读3063次

    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];
        }
        
    }
    

    相关文章

      网友评论

      本文标题:iOS-融云的部分功能集成

      本文链接:https://www.haomeiwen.com/subject/dxqrrttx.html