一,在融云开发者控制台创建应用
保存app key,app secrect。
二, 导入sdk
target 'MyApp' do
pod 'RongCloudIM/IMLib', '~> 2.8.3'
pod 'RongCloudIM/IMKit', '~> 2.8.3'
end
三,初始化
#import <RongIMKit/RongIMKit.h>
在 .m中,
[[RCIM sharedRCIM] initWithAppKey:@"YourTestAppKey"];
四,获取token
NSDictionary * dict = @{@"userId":[QLTUersDefaults objectForKey:SYSaveAuthTokenKey_id],
@"name":[QLTUersDefaults objectForKey:SYSaveAuthTokenKey_username],
@"portraitUri":[QLTUersDefaults objectForKey:SYSaveAuthTokenKey_userimg]
};
RequestOperationManager *manager = [RequestOperationManager shareInstance];
manager.requestSerializer = [AFHTTPRequestSerializer serializer];
NSString *appkey = SYSaveAuthTokenKey_RongCloud_appKey;
NSString *nonce = [[MSTool shareInstance] getRandomNonce];
NSString *timestamp = [[MSTool shareInstance] getTimestamp];
NSString *signature = [[MSTool shareInstance] getSignatureWithAppSecret:SYSaveAuthTokenKey_RongCloud_secret nonce:nonce timestamp:timestamp];
//设置请求头
[manager.requestSerializer setValue:appkey forHTTPHeaderField:@"App-Key"];
[manager.requestSerializer setValue:nonce forHTTPHeaderField:@"Nonce"];
[manager.requestSerializer setValue:timestamp forHTTPHeaderField:@"Timestamp"];
[manager.requestSerializer setValue:signature forHTTPHeaderField:@"Signature"];
[manager POST:@"https://api-cn.ronghub.com/user/getToken.json" parameters:dict progress:^(NSProgress * _Nonnull uploadProgress) {
} success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
NSLog(@"融云%@",responseObject);
/** {
token = "yNeNVnRO+oBWcS5VrP8IxhtzgYN9ENRs5wZc2MZKZy/xwXzCW/P7u6zBk8p/E7usUvmkgIKzr6QQi81GLeG8Iw==",
userId = "100042",
code = 200,
}**/
[QLTUersDefaults setObject:responseObject[@"token"] forKey:SYSaveAuthTokenKey_RongCloud_token];
[self getConnectWithRongCloud];
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
NSLog(@"error~~~%@",error);
}];
五,链接服务器
[[RCIM sharedRCIM] connectWithToken:[QLTUersDefaults objectForKey:SYSaveAuthTokenKey_RongCloud_token] success:^(NSString *userId) {
NSLog(@"登陆成功。当前登录的用户ID:%@", userId);
} error:^(RCConnectErrorCode status) {
NSLog(@"登陆的错误码为:%ld", status);
} tokenIncorrect:^{
//token过期或者不正确。
//如果设置了token有效期并且token过期,请重新请求您的服务器获取新的token
//如果没有设置token有效期却提示token错误,请检查您客户端和服务器的appkey是否匹配,还有检查您获取token的流程。
NSLog(@"token错误");
}];
六,直接进入会话页面
新建TalkViewController控制器继承于 RCConversationViewController
//设置会话的类型,如单聊、群聊、聊天室、客服、公众服务会话等
//设置会话的目标会话ID。(单聊、客服、公众服务会话为对方的ID,群聊、聊天室为会话的ID)
TalkViewController *chat = [TalkViewController alloc] initWithConversationType:ConversationType_PRIVATE
targetId:@"targetIdYouWillChatIn"];
//设置聊天会话界面要显示的标题
chat.title = @"想显示的会话标题";
//显示聊天会话界面
[self.navigationController pushViewController:chat animated:YES];
七,获取用户的头像,名称
//获取用户信息
@param userId 用户ID
@param completion 获取用户信息完成之后需要执行的Block
@param userInfo(in completion) 该用户ID对应的用户信息
@discussion SDK通过此方法获取用户信息并显示,请在completion中返回该用户ID对应的用户信息。
在您设置了用户信息提供者之后,SDK在需要显示用户信息的时候,会调用此方法,向您请求用户信息用于显示。
-(void)getUserInfoWithUserId:(NSString *)userId completion:(void (^)(RCUserInfo *))completion
{
RCUserInfo * user = [[RCUserInfo alloc] init];
//如果获取的是当前用户的信息,显示保存在本地的;如果不是从自己服务器获取后传过来。
if ([userId isEqualToString:[RCIM sharedRCIM].currentUserInfo.userId]) {
user.userId = userId;
user.portraitUri = [QLTUersDefaults objectForKey:SYSaveAuthTokenKey_userimg];
user.name = [QLTUersDefaults objectForKey:SYSaveAuthTokenKey_username];
return completion(user);
}else {
[self getUserData:userId completion:^(RCUserInfo * user) {
return completion(user);
}];
}
}
#pragma mark -从服务器获取好友信息
-(void)getUserData:(NSString *)userid completion:(void (^)(RCUserInfo *))completion {
NSDictionary *dict = @{@"uid":userid};
[RequestOperationManager requestWithParameters:dict withRequestType:POST_Request urlString:user_chatApi finishHandle:^(id result) {
for (int i = 0; i < [result[@"data"] count]; i ++) {
[QLTUersDefaults setObject:result[@"data"][0] forKey:@"other"];
RCUserInfo * user = [[RCUserInfo alloc] init];
user.userId = userid;
user.portraitUri = result[@"data"][0][@"userimg"];
user.name = result[@"data"][0][@"realname"];
[[RCIM sharedRCIM] refreshUserInfoCache:user withUserId:user.userId];
completion(user);
}
} failHandle:^(id result) {
}];
}
注意设置 [[RCIM sharedRCIM] setUserInfoDataSource:[RongCloudTool shareInstance]];