====== SDK 2.x 至 3.0 升级指南 ======
-----
=====环信 SDK 3.0 升级文档=====
3.0 中的核心类为 EMClient 类,通过 EMClient 类可以获取到 chatManager、groupManager、contactManager、roomManager对象。原来 2.0 版本的 SDK 很多方法提供了同步、异步回调、异步(block)三种方法,3.0 版只提供同步方法和异步block方法。
例如:
//2.0版本登录方法,提供了同步、异步、异步block三种方法
- (NSDictionary *)loginWithUsername:(NSString *)username
password:(NSString *)password
error:(EMError **)pError;
- (void)asyncLoginWithUsername:(NSString *)username
password:(NSString *)password;
- (void)asyncLoginWithUsername:(NSString *)username
password:(NSString *)password
completion:(void (^)(NSDictionary *loginInfo, EMError *error))completion
onQueue:(dispatch_queue_t)aQueue;
//3.0版本,网络请求的方法提供了同步方法和异步block方法
- (EMError *)loginWithUsername:(NSString *)aUsername
password:(NSString *)aPassword;
- (void)loginWithUsername:(NSString *)aUsername
password:(NSString *)aPassword
completion:(void (^)(NSString *aUsername, EMError *aError))aCompletionBlock;
注册回调,3.0 SDK 对功能进行了模块化,将不同功能回调封装到不同模块。
//2.0版本回调注册
[[EaseMob sharedInstance].chatManager addDelegate:self delegateQueue:nil];
//3.0版本回调注册需要分模块注册
[[EMClient sharedClient] addDelegate:self delegateQueue:nil];//登录相关的回调
[[EMClient sharedClient].groupManager addDelegate:self delegateQueue:nil];//群组回调
[[EMClient sharedClient].contactManager addDelegate:self delegateQueue:nil];//联系人回调
[[EMClient sharedClient].roomManager addDelegate:self delegateQueue:nil];//聊天室回调
[[EMClient sharedClient].chatManager addDelegate:self delegateQueue:nil];//消息回调
=====代码升级替换文档=====
3.0 SDK 大部分代码与 2.0 SDK 比较,逻辑没有变化,为了统一修改了名称。
入口的变化,2.0 版本 [EaseMob shareInstance] -> 3.0 版本 [EMClient sharedClient]。
2.0版本 EaseMob | 3.0版本 EMClient |
---|---|
登录相关 | |
当前登录的用户信息[[[EaseMob sharedInstance] chatManager] loginInfo]; | 只提供当前登录账号[[EMClient sharedClient] currentUsername]; |
当前是否已有登录的用户[[[EaseMob sharedInstance] chatManager] isLoggedIn]; | 当前是否已有登录的用户[[EMClient sharedClient] isLoggedIn]; |
是否连上聊天服务器[[[EaseMob sharedInstance] chatManager] isConnected]; | 是否连上聊天服务器[[EMClient sharedClient] isConnected]; |
将数据库数据导入新的数据库[[EaseMob sharedInstance].chatManager importDataToNewDatabase]; | 升级到SDK 3.0版本需要调用该方法,开发者需要等该方法执行完后再进行数据库查询操作[[EMClient sharedClient] dataMigrationTo3]; |
从数据库获取信息[[EaseMob sharedInstance].chatManager loadDataFromDatabase]; | 分别从不同模块获取[[EMClient sharedClient].chatManager getAllConversations]; |
在聊天服务器上创建账号- (BOOL)registerNewAccount:(NSString *)username password:(NSString *)password error:(EMError **)pError;- (void)asyncRegisterNewAccount:(NSString *)username password:(NSString *)password;- (void)asyncRegisterNewAccount:(NSString *)username password:(NSString *)password withCompletion:(void (^)(NSString *username,NSString *password,EMError *error))completion onQueue:(dispatch_queue_t)aQueue; | 在聊天服务器上创建账号[[EMClient sharedClient] registerWithUsername:@"username"password:@"password"];[[EMClient sharedClient] registerWithUsername:@"username"password:@"password" completion:block]; |
使用用户名密码登录聊天服务器- (NSDictionary *)loginWithUsername:(NSString *)username password:(NSString *)password error:(EMError **)pError;- (void)asyncLoginWithUsername:(NSString *)username password:(NSString *)password;- (void)asyncLoginWithUsername:(NSString *)username password:(NSString *)password completion:(void (^)(NSDictionary *loginInfo, EMError *error))completion onQueue:(dispatch_queue_t)aQueue; | 使用用户名密码登录聊天服务器[[EMClient sharedClient] loginWithUsername:@"username" password:@"password"];[[EMClient sharedClient] loginWithUsername:@"username" password:@"password" completion:block]; |
注销当前登录用户- (NSDictionary *)logoffWithUnbindDeviceToken:(BOOL)isUnbind error:(EMError **)pError;- (void)asyncLogoffWithUnbindDeviceToken:(BOOL)isUnbind;- (void)asyncLogoffWithUnbindDeviceToken:(BOOL)isUnbind completion:(void (^)(NSDictionary *info, EMError *error))completion onQueue:(dispatch_queue_t)aQueue; | 注销当前登录用户[[EMClient sharedClient] logout:NO];[[EMClient sharedClient] logout:NO completionblock]; |
登录回调 | |
2.0版本 EMChatManagerLoginDelegate | 3.0版本 EMClientDelegate |
用户将要进行自动登录操作- (void)willAutoLoginWithInfo:(NSDictionary *)loginInfo error:(EMError *)error;用户登录完成- (void)didLoginWithInfo:(NSDictionary *)loginInfo error:(EMError *)error;用户注销完成- (void)didLogoffWithError:(EMError *)error;注册新用户后的回调- (void)didRegisterNewAccount:(NSString *)username password:(NSString *)password error:(EMError *)error; | 不再支持相关回调 |
用户自动登录完成- (void)didAutoLoginWithInfo:(NSDictionary *)loginInfo error:(EMError *)error; | 用户自动登录完成- (void)autoLoginDidCompleteWithError:(EMError *)error; |
当前登录账号在其它设备登录- (void)didLoginFromOtherDevice; | 当前登录账号在其它设备登录- (void)userAccountDidLoginFromOtherDevice; |
当前登录账号已经被从服务器端删除- (void)didRemovedFromServer; | 当前登录账号已经被从服务器端删除- (void)userAccountDidRemoveFromServer; |
将要发起自动重连操作的回调- (void)willAutoReconnect;自动重连操作完成后的回调- (void)didAutoReconnectFinishedWithError:(NSError *)error; | 连接状态发生变化- (void)didConnectionStateChanged:(EMConnectionState)connectionState; |
消息相关 | |
2.0版本 IChatManagerChat IChatManagerDelegate | 3.0版本 IEMChatManager EMChatManagerDelegate |
发送消息- (EMMessage *)sendMessage:(EMMessage *)message
progress:(id |
发送消息[IEMChatManager sendMessage:progress:completion:]; |
发送已读回执到服务器- (void)sendReadAckForMessage:(EMMessage *)message; | 发送已读回执到服务器[IEMChatManager sendMessageReadAck:completion:]; |
重新发送消息- (EMMessage *)resendMessage:(EMMessage *)message
progress:(id |
重新发送消息[IEMChatManager resendMessage:progress:completion:] |
聊天室相关 | |
2.0版本 IChatManagerChatroom IChatManagerDelegate | 3.0版本 IEMChatroomManager EMChatroomManagerDelegate EMChatroomOptions |
主题[EMChatroom chatroomSubject]; | 主题[EMChatroom subject]; |
描述[EMChatroom chatroomDescription]; | 描述[EMChatroom description]; |
最大人数[EMChatroom chatroomMaxOccupantsCount]; | 最大人数[EMChatroom maxOccupantsCount]; |
群组相关 | |
2.0版本 IChatManagerGroup IChatManagerDelegate | 3.0版本 IEMGroupManager EMGroupManagerDelegate |
从数据库获取与登录者相关的群组- (NSArray *)loadAllMyGroupsFromDatabaseWithAppend2Chat:(BOOL)append2Chat; | 获取用户所有群组[IEMGroupManager getJoinedGroups]; |
创建群组- (EMGroup *)createGroupWithSubject:(NSString *)subject description:(NSString *)description invitees:(NSArray *)invitees initialWelcomeMessage:(NSString *)welcomeMessage styleSetting:(EMGroupStyleSetting *)styleSetting error:(EMError **)pError;- (void)asyncCreateGroupWithSubject:(NSString *)subject description:(NSString *)description invitees:(NSArray *)invitees initialWelcomeMessage:(NSString *)welcomeMessage styleSetting:(EMGroupStyleSetting *)styleSetting;- (void)asyncCreateGroupWithSubject:(NSString *)subject description:(NSString *)description invitees:(NSArray *)invitees initialWelcomeMessage:(NSString *)welcomeMessage styleSetting:(EMGroupStyleSetting *)styleSetting completion:(void (^)(EMGroup *group, EMError *error))completion onQueue:(dispatch_queue_t)aQueue; | 3.0版本[IEMGroupManager createGroupWithSubject:description:invitees:message:setting:error:];[IEMGroupManager createGroupWithSubject:description:invitees:message:setting:completion:]; |
创建匿名群组- (EMGroup *)createAnonymousGroupWithSubject:(NSString *)subject description:(NSString *)description initialWelcomeMessage:(NSString *)welcomeMessage nickname:(NSString *)nickname styleSetting:(EMGroupStyleSetting *)styleSetting error:(EMError **)pError;- (void)asyncCreateAnonymousGroupWithSubject:(NSString *)subject description:(NSString *)description initialWelcomeMessage:(NSString *)welcomeMessage nickname:(NSString *)nickname styleSetting:(EMGroupStyleSetting *)styleSetting;- (void)asyncCreateAnonymousGroupWithSubject:(NSString *)subject description:(NSString *)description initialWelcomeMessage:(NSString *)welcomeMessage nickname:(NSString *)nickname styleSetting:(EMGroupStyleSetting *)styleSetting completion:(void (^)(EMGroup *group, EMError *error))completion onQueue:(dispatch_queue_t)aQueue; | 不再提供 |
加入一个匿名公开群组- (EMGroup *)joinAnonymousPublicGroup:(NSString *)groupId nickname:(NSString *)nickname error:(EMError **)pError;- (void)asyncJoinAnonymousPublicGroup:(NSString *)groupId nickname:(NSString *)nickname;- (void)asyncJoinAnonymousPublicGroup:(NSString *)groupId nickname:(NSString *)nickname completion:(void (^)(EMGroup *group, EMError *error))completion onQueue:(dispatch_queue_t)aQueue; | 不再提供 |
退出群组- (EMGroup *)leaveGroup:(NSString *)groupId error:(EMError **)pError;- (void)asyncLeaveGroup:(NSString *)groupId;- (void)asyncLeaveGroup:(NSString *)groupId completion:(void (^)(EMGroup *group, EMGroupLeaveReason reason, EMError *error))completion onQueue:(dispatch_queue_t)aQueue; | 退出群组[IEMGroupManager leaveGroup:error:];[IEMGroupManager leaveGroup:completion:]; |
解散群组- (EMGroup *)destroyGroup:(NSString *)groupId error:(EMError **)pError;- (void)asyncDestroyGroup:(NSString *)groupId;- (void)asyncDestroyGroup:(NSString *)groupId completion:(void (^)(EMGroup *group, EMGroupLeaveReason reason, EMError *error))completion onQueue:(dispatch_queue_t)aQueue; | 解散群组[IEMGroupManager destroyGroup:];[IEMGroupManager destroyGroup: finishCompletion:]; |