李洪强iOS开发之-环信02.1_环信 SDK 2.x到3.0升级文档
SDK 2.x 至 3.0 升级指南
环信 SDK 3.0 升级文档
3.0 中的核心类为 EMClient 类,通过 EMClient 类可以获取到 chatManager、groupManager、contactManager、roomManager对象。原来 2.0 版本的 SDK 很多方法提供了同步、异步回调、异步(block)三种方法,3.0 版只提供同步方法(async开头的方法为异步方法)。
例如:
//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版本,网络请求的方法只提供了同步方法 - (EMError *)loginWithUsername:(NSString *)aUsername password:(NSString *)aPassword;
注册回调,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 升级辅助工具(替换枚举、属性、方法名称等):
python EMReplace.py replacePath
xcode 编译提示工具:
//1. Xcode->build Phases //2. 添加一个新的Run Script //3. 填写脚本:python ${路径}/EMChecker.py //编译运行会有提示
代码升级替换文档
3.0 SDK 大部分代码与 2.0 SDK 比较,逻辑没有变化,为了统一修改了名称。2.0 版本提供的异步方法,对应的回调 3.0 不再支持(注意:3.0 只提供同步方法)。
入口的变化,2.0 版本 [EaseMob shareInstance] → 3.0 版本 [EMClient sharedClient]。
2.0版本 | 3.0版本 |
---|---|
登录相关 | |
2.0版本,当前登录的用户信息 [[[EaseMob sharedInstance] chatManager] loginInfo] | 3.0版本,只提供当前登录账号 [[EMClient sharedClient] currentUsername] |
2.0版本,当前是否已有登录的用户 [[[EaseMob sharedInstance] chatManager] isLoggedIn] | 3.0版本 [[EMClient sharedClient] isLoggedIn] |
2.0版本,是否连上聊天服务器 [[[EaseMob sharedInstance] chatManager] isConnected] | 3.0版本 [[EMClient sharedClient] isConnected] |
2.0版本,将数据库数据导入新的数据库 [[EaseMob sharedInstance].chatManager importDataToNewDatabase] | 3.0版本,升级到SDK 3.0版本需要调用该方法,开发者需要等该方法执行完后再进行数据库查询操作 [[EMClient sharedClient] dataMigrationTo3] |
2.0版本,从数据库获取信息 [[EaseMob sharedInstance].chatManager loadDataFromDatabase] | 3.0版本不再支持 |
2.0版本,在聊天服务器上创建账号 - (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; | 3.0版本,只提供同步方法 EMError *error = [[EMClient sharedClient] registerWithUsername:@"username"password:@"password"] |
2.0版本,使用用户名密码登录聊天服务器 - (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版本,只提供同步方法 - (EMError *)loginWithUsername:(NSString *)aUsername password:(NSString *)aPassword //调用方法:EMError *error = [[EMClient sharedClient] loginWithUsername:@"username" password:@"password"] |
2.0版本,注销当前登录用户 - (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; | 3.0版本,只提供同步方法 - (EMError *)logout:(BOOL)bIsUnbindDeviceToken; //调用方法:EMError *error = [[EMClient sharedClient] logout:NO]; |
登录回调 | |
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; | 3.0版本不再支持的回调 |
2.0版本,用户自动登录完成后的回调 - (void)didAutoLoginWithInfo:(NSDictionary *)loginInfo error:(EMError *)error; | 3.0版本使用EMClientDelegate - (void)didAutoLoginWithError:(EMError *)error; |
2.0版本,当前登录账号在其它设备登录时的通知回调 - (void)didLoginFromOtherDevice; 2.0版本,当前登录账号已经被从服务器端删除 - (void)didRemovedFromServer; | 3.0版本使用EMClientDelegate,回调行为与2.0保持一致 |
2.0版本,将要发起自动重连操作的回调 - (void)willAutoReconnect; 自动重连操作完成后的回调 - (void)didAutoReconnectFinishedWithError:(NSError *)error; | 3.0版本使用EMClientDelegate - (void)didConnectionStateChanged:(EMConnectionState)connectionState; |
消息相关 | |
2.0版本IChatManagerChat | 3.0版本IEMChatManager |
2.0版本,发送消息的方法 - (EMMessage *)sendMessage:(EMMessage *)message progress:(id)progress error:(EMError **)pError; - (EMMessage *)asyncSendMessage:(EMMessage *)message progress:(id)progress; - (EMMessage *)asyncSendMessage:(EMMessage *)message progress:(id)progress prepare:(void (^)(EMMessage *message, EMError *error))prepare onQueue:(dispatch_queue_t)aPrepareQueue completion:(void (^)(EMMessage *message, EMError *error))completion onQueue:(dispatch_queue_t)aCompletionQueue; | 3.0版本 - (void)asyncSendMessage:(EMMessage *)aMessage progress:(void (^)(int progress))aProgress completion:(void (^)(EMMessage *message, EMError *error))aProgressCompletion; //调用: //[[EMClient sharedClient].chatManager asyncSendMessage:message progress:nil completion:^(EMMessage *aMessage, EMError *aError) {}]; |
2.0版本,发送一个"已读消息"(在UI上显示了或者阅后即焚的销毁的时候发送)的回执到服务器 - (void)sendReadAckForMessage:(EMMessage *)message; | 3.0版本 - (void)asyncSendReadAckForMessage:(EMMessage *)message; //调用: //[[EMClient sharedClient].chatManager asyncSendReadAckForMessage:message]; |
2.0版本,重新发送某一条消息 - (EMMessage *)resendMessage:(EMMessage *)message progress:(id)progress error:(EMError **)pError; - (EMMessage *)asyncResendMessage:(EMMessage *)message progress:(id)progress; - (EMMessage *)asyncResendMessage:(EMMessage *)message progress:(id)progress prepare:(void (^)(EMMessage *message, EMError *error))prepare onQueue:(dispatch_queue_t)aPrepareQueue completion:(void (^)(EMMessage *message, EMError *error))completion onQueue:(dispatch_queue_t)aCompletionQueue; | 3.0版本 */ - (void)asyncResendMessage:(EMMessage *)aMessage progress:(void (^)(int progress))aProgressCompletion completion:(void (^)(EMMessage *message, EMError *error))aCompletion; //调用: //[[EMClient sharedClient].chatManager asyncResendMessage:message progress:nil completion:^(EMMessage *aMessage, EMError *aError) {}]; |
聊天室相关 | |
2.0版本 | 3.0版本 |
2.0版本,聊天室的主题 @property (nonatomic, strong, readonly) NSString *chatroomSubject; | 3.0版本 @property (nonatomic, copy, readonly) NSString *subject; |
2.0版本,聊天室的描述 @property (nonatomic, strong, readonly) NSString *chatroomDescription; | 3.0版本 @property (nonatomic, copy, readonly) NSString *description; |
2.0版本,聊天室的最大人数 @property (nonatomic, readonly) NSInteger chatroomMaxOccupantsCount; | 3.0版本 @property (nonatomic, readonly) NSInteger maxOccupantsCount; |
群组相关 | |
2.0版本 | 3.0版本 |
2.0版本,从数据库获取与登录者相关的群组 - (NSArray *)loadAllMyGroupsFromDatabaseWithAppend2Chat:(BOOL)append2Chat; | 3.0版本 - (NSArray *)loadAllMyGroupsFromDB; //调用:[[EMClient sharedClient].groupManager loadAllMyGroupsFromDB]; |
2.0版本,创建群组 - (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版本 - (EMGroup *)createGroupWithSubject:(NSString *)aSubject description:(NSString *)aDescription invitees:(NSArray *)aInvitees message:(NSString *)aMessage setting:(EMGroupOptions *)aSetting error:(EMError **)pError; //调用: //EMError *error = nil; //EMGroup *group = [[EMClient sharedClient].groupManager createGroupWithSubject:@"subject" description:@"desc" invitees:source message:@"message" setting:setting error:&error]; |
2.0版本,创建匿名群组 | 3.0版本不再提供 |
2.0版本,加入一个匿名公开群组 | 3.0版本不再提供 |
2.0版本,退出群组(需要非owner的权限) - (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; | 3.0版本 - (EMGroup *)leaveGroup:(NSString *)aGroupId error:(EMError **)pError; //调用: //EMError *error = nil; //[[EMClient sharedClient].groupManager leaveGroup:@"groupId" error:&error]; |
2.0版本,解散群组,需要owner权限 - (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; | 3.0版本 - (EMGroup *)destroyGroup:(NSString *)aGroupId error:(EMError **)pError; //调用: //EMError *error = nil; //[[EMClient sharedClient].groupManager destroyGroup:@"groupId" error:&error]; |