最近項目中有涉及到即時通訊的功能,所用到的是環信3.1.5的sdk。下面就開始介紹其過程:
(1)集成環信SDK:
pod:pod 'HyphenateFullSDK'
手動:因為我們要使用到語音和視頻的功能,所以我們需要導入(環信 iOS HyphenateFullSDK 開發使用(包含實時通話功能))HyphenateFullSDK這個包。手動導入包文件后,我們需要手動加上一些庫:
第 1 步:SDK 包含實時語音依賴庫有:
CoreMedia.framework
AudioToolbox.framework
AVFoundation.framework
MobileCoreServices.framework
ImageIO.framework
libc++.dylib
libz.dylib
libstdc++.6.0.9.dylib
libsqlite3.dylib
libiconv.dylib
(如果使用的是 xcode7,后綴為 tbd。)
第 2 步:SDK 不支持 bitcode,向 Build Settings → Linking → Enable Bitcode 中設置 NO。
(2)添加EaseUI:
必須和SDK相對應的版本,不然會出現各種報錯(方法找不到或者不對)
注意:如果編譯報錯,1、先檢查EaseUI中的第三方是否和本地工程中的重復。 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?2、在PCH文件中引入頭文件時:#ifdef __OBJC__ ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? #import "easeUI.h" ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? #import "EMSDKFull.h" ? ?等等 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?#endif
正式開始代碼的編寫:
//AppKey:注冊的AppKey,詳細見下面注釋。//apnsCertName:推送證書名(不需要加后綴),詳細見下面注釋。
初始化SDK:
EMOptions*options=[EMOptions optionsWithAppkey:@"douser#istore"];? ? options.apnsCertName=@"istore_dev"; ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?[[EMClient sharedClient]initializeSDKWithOptions:options];
注冊:
EMError*error=[[EMClient sharedClient]registerWithUsername:@"8001"password:@"111111"];if(error==nil){NSLog(@"注冊成功");}
登陸:
EMError*error=[[EMClient sharedClient]loginWithUsername:@"8001"password:@"111111"];if(!error){NSLog(@"登錄成功");}
注意:具體的自動登錄(免登陸)、重連、退出登陸、異地登錄等請移駕至環信官方:http://docs.easemob.com/im/300iosclientintegration/30iossdkbasic
聊天會話列表界面:建議自己搭建,便于管理和拓展
1:直接用環信的列表界面:ConversationListController ? 直接初始化加到自己的tabar上去
2:自己搭建列表界面:
- (void)didReceiveMessages:(NSArray *)aMessages;是刷新該列表的重點(記住注冊及代理)。對方發來的消息都會在這個方法中進行刷新列表及跟新角標數等。
注意:其他代碼自行到demo中去尋找,環信demo中都有參考;
單聊+群聊:因為自己寫的效果遠沒有環信的好,加上需求不要對聊天界面進行自定義,所以最后我還是直接用的demo中的界面(把demo中的ChatViewController拖到自己工程中,編譯,注釋掉報錯的代碼),初始化,push到聊天界面即可:
ChatViewController *chatController = [[ChatViewController alloc] initWithConversationChatter:conversation.conversationId? conversationType:conversation.type];
群列表:獲取與之相關所有的群
NSArray *groups = [[EMClient sharedClient].groupManager getMyGroupsFromServerWithError:nil];
語音和視頻及時通話:因為都是環信寫的 所以把有關及時通話的文件拖進來;
ChatDemoHelper? ? ? ? CallViewController
1:在pct中加上 #define DEMO_CALL 1?
2:全局搜索#if DEMO_CALL == 1 ? 刪掉,編譯如有報錯請仔細看是不是哪句代碼重復了或者哪里沒有注釋完全。
3.ChatDemoHelper.m中 注冊代碼到——#pragma mark - EMCallManagerDelegate宏中間的代碼都可以注釋掉
重中之重:在根控制器中一定要把控制器賦給ChatDemoHelper:[ChatDemoHelper shareHelper].mainVC = self;
如果集成語音和視頻過程中有問題的,可以參考官方給出的視頻,不過只能借鑒:http://www.imgeek.org/video/24
上面所述就是我使用環信的心得,不是很全面。大神手下留情,新手一枚。后續我會把單人昵稱、頭像;群組名稱、群組頭像等等的一些方法寫出來,如果發現我所提出的有問題和有好的建議,請直接提出來,也可以一起討論。qq:1804094055 有問題可以找我。