集成測試
評測環境
2G/3G/WiFi網絡下:
測評環境
|
MAC OS(10.9.2)+ XCode(5.1) + iPhone
|
測試對象
|
環信即時通訊雲 IOS SDK Version 2.0.9.1
|
測試設備
|
iPhone
|
系統版本
|
IOS SDK 7.1
|
設備網絡
|
2G/3G/WIFI
|
測試難度
|
一般
|
測試方法
|
客戶端運行環信即時通訊雲IOS SDK實例
|
測試時間
|
2014-10-01 17:10:00
|
測試時長
|
6個小時
|
基本參數:
是否免費
|
註冊用戶300萬以下或日活30萬以下終身免費
|
離線存儲空間
|
每個用戶100MB
|
平臺支持
|
IOS、Android
|
實時語音、視頻
|
即將支持
|
單聊
|
支持
|
羣聊
|
支持
|
發送文字
|
支持
|
發送表情
|
支持
|
發送語音
|
支持
|
發送地理位置
|
支持
|
發送照片
|
支持
|
發送視頻
|
支持
|
離線消息
|
支持
|
存儲用戶信息
|
不支持,由用戶業務服務器負責
|
流量數據統計
|
支持
|
消息發送數量
|
無限制
|
注:具體參數詳見: http://www.easemob.com/price/
集成過程
客戶端集成
環信即時通訊雲IOS SDK的大體開發流程如下:
註冊成爲開發者=》服務端配置=》創建應用=》下載SDK=》集成開發=》測試應用
1、註冊環信賬號
登錄環信官方首頁:http://www.easemob.com/
點擊登錄,如果沒有環信賬號需要進行註冊,填寫用戶相關詳細信息,如下圖所示:
注:企業ID只限於數字、字母、橫線,且不能以橫線開頭和結尾。企業ID會存在於生成的APPKEY中,如:測試demo中 APPKEY爲DevStore#chatdemo,則DevStore爲填寫的企業ID。
如果是個人應用開發的話,企業ID直接寫成自己定義的ID就可以。
2、激活賬號
註冊成功後,會向你填寫的郵箱中發送驗證信息,請前往郵箱進行賬號激活。
3、登錄
進行登錄:https://console.easemob.com/index.html
4、創建應用
賬號成功激活後,登錄到環信管理後臺的頁面,在我的應用中,點擊創建應用按鈕,如下圖:
點擊創建應用,填寫應用的相關信息。
注: 註冊模式中,開放註冊:允許在該應用下自由註冊新用戶,授權註冊:只有企業管理員或者應用管理員才能註冊用戶。
建議在開發測試階段選擇開放註冊,這樣便於程序的測試。
顯示創建應用成功。
5、獲取應用的配置信息
保存好應用的基本信息:API KEY以及client_id、client_secret。
注: AppKey作爲客戶端應用程序的標識,在進行客戶端開發的時候會用到。
client_id和client_secret在客戶端與服務器進行數據交換時進行鑑權使用,它就是擁有超級權限的一個token,可以調REST接口,來處理管理員做的事情,使用它的人應有最高的權限,所以一般情況下對於普通的客戶端來說是用不到的。
6、下載SDK
下載IOS SDK http://www.easemob.com/sdk/
注:目前的最新版本爲2.0.9.1
7、SDK集成
1)新建工程
2)添加環信 SDK
直接將環信 SDK拖入到工程中
選擇複製
3)添加系統鏈接庫
添加 SystemConfigureation.framework
除此之外,還應依次添加如下庫文件:
4)編譯環境的設置
設置Linker
向Other Linker Flags 中添加 -ObjC。(如果已有,則不需要再添加)
注:-ObjC就是會加載靜態庫文件中實現一個類或者分類的所有成員
設置Architectures
5)設置AppKey
設置APNS和AppKey,如下圖所示:
注:極力推薦開發者在環信提供的Demo之上進行應用開發,該Demo已經實現了聊天所需的大部分功能,這樣可以提高開發效率。
EaseMob作爲客戶端基本工具類(單例)負責與服務器端交互。
6)註冊功能
實現註冊功能,添加如下代碼:
01 |
- (IBAction)doRegister:(id)sender |
05 |
[self.view endEditing:YES]; |
06 |
if ([self.usernameTextField.text isChinese]) |
08 |
UIAlertView *alert = [[UIAlertView alloc] |
09 |
initWithTitle:@ "用戶名不支持中文" |
12 |
cancelButtonTitle:@ "確定" |
13 |
otherButtonTitles:nil]; |
19 |
[self showHudInView:self.view hint:@ "正在註冊..." ]; |
20 |
[[EaseMob sharedInstance].chatManager asyncRegisterNewAccount:_usernameTextField.text |
21 |
password:_passwordTextField.text |
22 |
withCompletion: case EMErrorServerNotReachable: |
23 |
TTAlertNoTitle(@ "連接服務器失敗!" ); |
25 |
case EMErrorServerDuplicatedAccount: |
26 |
TTAlertNoTitle(@ "您註冊的用戶已存在!" ); |
28 |
case EMErrorServerTimeout: |
29 |
TTAlertNoTitle(@ "連接服務器超時!" ); |
32 |
TTAlertNoTitle(@ "註冊失敗" ); |
IChatManager接口類,它是一個聊天管理器,獲取該對象後,可以做登錄、聊天、加好友等操作。
在這裏使用異步的方法asyncRegisterNewAccount在聊天服務器上創建賬號。
進入環信服務管理的後臺,查看IM用戶
現在已有devstore_test用戶,說明註冊成功。
7)登錄功能
實現登錄功能,添加如下代碼:
01 |
- ( void ) loginWithUsername:(NSString *)username password:(NSString *)password |
04 |
[self showHudInView:self.view hint:@ "正在登錄..." ]; |
05 |
[[EaseMob sharedInstance].chatManager asyncLoginWithUsername:username |
08 |
^(NSDictionary *loginInfo, EMError *error) { |
10 |
if (loginInfo && !error) { |
11 |
[[NSNotificationCenter defaultCenter] postNotificationName:KNOTIFICATION_LOGINCHANGE object:@YES]; |
13 |
switch (error.errorCode) { |
14 |
case EMErrorServerNotReachable: |
15 |
TTAlertNoTitle(@ "連接服務器失敗!" ); |
17 |
case EMErrorServerAuthenticationFailure: |
18 |
TTAlertNoTitle(@ "用戶名或密碼錯誤" ); |
20 |
case EMErrorServerTimeout: |
21 |
TTAlertNoTitle(@ "連接服務器超時!" ); |
24 |
TTAlertNoTitle(@ "登錄失敗" ); |
IChatManager 接口類,它是一個聊天管理器,獲取該對象後,可以做登錄、聊天、加好友等操作。
在這裏使用異步的方法asyncLoginWithUsername登錄聊天服務器。
驗證登錄功能
8)發送消息功能
發送文字,實現消息發送功能,添加如下代碼:
01 |
+(EMMessage *)sendTextMessageWithString:(NSString *)str toUsername:(NSString *)username isChatGroup:( BOOL )isChatGroup |
02 |
requireEncryption:( BOOL )requireEncryption |
05 |
NSString *willSendText = [ConvertToCommonEmoticonsHelper convertToCommonEmoticons:str]; |
06 |
EMChatText *text = [[EMChatText alloc] initWithText:willSendText]; |
07 |
EMTextMessageBody *body = [[EMTextMessageBody alloc] initWithChatObject:text]; |
09 |
EMMessage *retureMsg = [[EMMessage alloc] initWithReceiver:username bodies:[NSArray arrayWithObject:body]]; |
10 |
retureMsg.requireEncryption = requireEncryption; |
11 |
retureMsg.isGroup = isChatGroup; |
12 |
EMMessage *message = [[EaseMob sharedInstance].chatManager asyncSendMessage:retureMsg progress:nil]; |
EMChatText 用於封裝要發送消息的具體內容
EMTextMessageBody 對消息內容進行封裝的消息體
EMMessage 封裝消息,創建EMessage時需要指定消息的接受者和要發送的消息列表。
客戶的工具類EaseMob中的聊天管器chatManager中的asyncSendMessage負責發送消息。
驗證發送消息功能:
9)接收消息功能
接收文字,實現消息接收功能,添加如下代碼:
01 |
- ( void )showNotificationWithMessage:(EMMessage *)message |
04 |
EMPushNotificationOptions *options = [[EaseMob sharedInstance].chatManager pushNotificationOptions]; |
06 |
UILocalNotification *notification = [[UILocalNotification alloc] init]; |
07 |
notification.fireDate = [NSDate date]; |
09 |
if (options.displayStyle == ePushNotificationDisplayStyle_messageSummary) { |
10 |
id<IEMMessageBody> messageBody = [message.messageBodies firstObject]; |
11 |
NSString *messageStr = nil; |
12 |
switch (messageBody.messageBodyType) { |
13 |
case eMessageBodyType_Text: |
15 |
messageStr = ((EMTextMessageBody *)messageBody).text; |
18 |
case eMessageBodyType_Image: |
23 |
case eMessageBodyType_Location: |
28 |
case eMessageBodyType_Voice: |
33 |
case eMessageBodyType_Video:{ |
41 |
NSString *title = message.from; |
42 |
if (message.isGroup) { |
43 |
NSArray *groupArray = [[EaseMob sharedInstance].chatManager groupList]; |
44 |
for (EMGroup *group in groupArray) { |
45 |
if ([group.groupId isEqualToString:message.conversation.chatter]) { |
46 |
title = [NSString stringWithFormat:@ "%@(%@)" , message.groupSenderName, group.groupSubject]; |
52 |
notification.alertBody = [NSString stringWithFormat:@ "%@:%@" , title, messageStr]; |
55 |
notification.alertBody = @ "您有一條新消息" ; |
57 |
notification.alertAction = @ "打開" ; |
58 |
notification.timeZone = [NSTimeZone defaultTimeZone]; |
60 |
[[UIApplication sharedApplication] scheduleLocalNotification:notification]; |
61 |
UIApplication *application = [UIApplication sharedApplication]; |
62 |
application.applicationIconBadgeNumber += 1; |
IEMMessageBody中的messageBodyType用來指定消息的類型
驗證接收消息功能:
除此之外,還可以通過網頁在環信管理後臺進行用戶的管理和消息的發送。
主要功能
用戶登錄
用戶註冊
在客戶端進行註冊的時候需要將應用的註冊方式改爲開發註冊。
消息發送與接收
環信客戶端支持許多媒體,包括文字、圖片、視頻、地理位置等。
查找與添加好友
羣組管理
刪除用戶或組
功能特色
1、數據流量統計
環信即時通訊雲可以按照IM用戶、消息、GET請求次數、POST請求次數、DELETE請求次數、PUT請求次數進行數據統計。同時也支持按照時間段對以上統計項進行查詢。
用戶數量的統計
消息數量的統計
2、用戶信息管理
在環信管理後臺可以對當前的用戶(環信ID標識)進行管理,包括爲用戶添加好友、創建組、註冊用戶、發送消息、批量刪除等。
3、客戶端設置
環信IOS客戶端已經實現了聊天所具有的大部分功能,另外用戶也可以根據自己的需要進行相應的設置,包括自動登錄、消息推送設置、黑名單以及日誌上傳等。
DEMO展示
AppDelegate.m
01 |
- ( BOOL )application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions |
03 |
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; |
04 |
self.window.backgroundColor = [UIColor whiteColor]; |
05 |
apnsCertName = @ "devstorehuanxin" ; |
07 |
[[EaseMob sharedInstance] registerSDKWithAppKey:@ "devstore#devstorehuanxin" apnsCertName:apnsCertName]; |
08 |
NSLog(@ "註冊AppKey......" ); |
11 |
[[EaseMob sharedInstance] enableUncaughtExceptionHandler]; |
15 |
[[EaseMob sharedInstance] application:application didFinishLaunchingWithOptions:launchOptions]; |
17 |
#warning 註冊爲SDK的ChatManager的delegate (及時監聽到申請和通知) |
18 |
[[EaseMob sharedInstance].chatManager removeDelegate:self]; |
19 |
[[EaseMob sharedInstance].chatManager addDelegate:self delegateQueue:nil]; |
21 |
#warning 如果使用MagicalRecord, 要加上這句初始化MagicalRecord |
23 |
[MagicalRecord setupCoreDataStackWithStoreNamed:[NSString stringWithFormat:@ "%@.sqlite" , @ "UIDemo" ]]; |
25 |
[self loginStateChange:nil]; |
26 |
[self.window makeKeyAndVisible]; |
註冊用戶
01 |
- (IBAction)doRegister:(id)sender |
05 |
[self.view endEditing:YES]; |
06 |
if ([self.usernameTextField.text isChinese]) |
08 |
UIAlertView *alert = [[UIAlertView alloc] |
09 |
initWithTitle:@ "用戶名不支持中文" |
12 |
cancelButtonTitle:@ "確定" |
13 |
otherButtonTitles:nil]; |
19 |
[self showHudInView:self.view hint:@ "正在註冊..." ]; |
20 |
[[EaseMob sharedInstance].chatManager asyncRegisterNewAccount:_usernameTextField.text |
21 |
password:_passwordTextField.text |
22 |
withCompletion: case EMErrorServerNotReachable: |
23 |
TTAlertNoTitle(@ "連接服務器失敗!" ); |
25 |
case EMErrorServerDuplicatedAccount: |
26 |
TTAlertNoTitle(@ "您註冊的用戶已存在!" ); |
28 |
case EMErrorServerTimeout: |
29 |
TTAlertNoTitle(@ "連接服務器超時!" ); |
32 |
TTAlertNoTitle(@ "註冊失敗" ); |
LoginViewController.m
用戶登錄
01 |
- (IBAction)doRegister:(id)sender |
05 |
[self.view endEditing:YES]; |
06 |
if ([self.usernameTextField.text isChinese]) |
08 |
UIAlertView *alert = [[UIAlertView alloc] |
09 |
initWithTitle:@ "用戶名不支持中文" |
12 |
cancelButtonTitle:@ "確定" |
13 |
otherButtonTitles:nil]; |
19 |
[self showHudInView:self.view hint:@ "正在註冊..." ]; |
20 |
[[EaseMob sharedInstance].chatManager asyncRegisterNewAccount:_usernameTextField.text |
21 |
password:_passwordTextField.text |
22 |
withCompletion: case EMErrorServerNotReachable: |
23 |
TTAlertNoTitle(@ "連接服務器失敗!" ); |
25 |
case EMErrorServerDuplicatedAccount: |
26 |
TTAlertNoTitle(@ "您註冊的用戶已存在!" ); |
28 |
case EMErrorServerTimeout: |
29 |
TTAlertNoTitle(@ "連接服務器超時!" ); |
32 |
TTAlertNoTitle(@ "註冊失敗" ); |
發送文字
01 |
+(EMMessage *)sendTextMessageWithString:(NSString *)str |
02 |
toUsername:(NSString *)username |
03 |
isChatGroup:( BOOL )isChatGroup |
04 |
requireEncryption:( BOOL )requireEncryption |
07 |
NSString *willSendText = [ConvertToCommonEmoticonsHelper convertToCommonEmoticons:str]; |
08 |
EMChatText *text = [[EMChatText alloc] initWithText:willSendText]; |
09 |
EMTextMessageBody *body = [[EMTextMessageBody alloc] initWithChatObject:text]; |
11 |
EMMessage *retureMsg = [[EMMessage alloc] initWithReceiver:username bodies:[NSArray arrayWithObject:body]]; |
12 |
retureMsg.requireEncryption = requireEncryption; |
13 |
retureMsg.isGroup = isChatGroup; |
14 |
EMMessage *message = [[EaseMob sharedInstance].chatManager asyncSendMessage:retureMsg progress:nil]; |
接收消息
01 |
- ( void )showNotificationWithMessage:(EMMessage *)message |
04 |
EMPushNotificationOptions *options = [[EaseMob sharedInstance].chatManager pushNotificationOptions]; |
06 |
UILocalNotification *notification = [[UILocalNotification alloc] init]; |
07 |
notification.fireDate = [NSDate date]; |
09 |
if (options.displayStyle == ePushNotificationDisplayStyle_messageSummary) { |
10 |
id<IEMMessageBody> messageBody = [message.messageBodies firstObject]; |
11 |
NSString *messageStr = nil; |
12 |
switch (messageBody.messageBodyType) { |
13 |
case eMessageBodyType_Text: |
15 |
messageStr = ((EMTextMessageBody *)messageBody).text; |
18 |
case eMessageBodyType_Image: |
23 |
case eMessageBodyType_Location: |
28 |
case eMessageBodyType_Voice: |
33 |
case eMessageBodyType_Video:{ |
41 |
NSString *title = message.from; |
42 |
if (message.isGroup) { |
43 |
NSArray *groupArray = [[EaseMob sharedInstance].chatManager groupList]; |
44 |
for (EMGroup *group in groupArray) { |
45 |
if ([group.groupId isEqualToString:message.conversation.chatter]) { |
46 |
title = [NSString stringWithFormat:@ "%@(%@)" , message.groupSenderName, group.groupSubject]; |
52 |
notification.alertBody = [NSString stringWithFormat:@ "%@:%@" , title, messageStr]; |
55 |
notification.alertBody = @ "您有一條新消息" ; |
57 |
notification.alertAction = @ "打開" ; |
58 |
notification.timeZone = [NSTimeZone defaultTimeZone]; |
60 |
[[UIApplication sharedApplication] scheduleLocalNotification:notification]; |
61 |
UIApplication *application = [UIApplication sharedApplication]; |
62 |
application.applicationIconBadgeNumber += 1; |
詳細實現代碼參見環信SDK中的UI實例,裏面有詳細的說明。
測試日誌
測試日誌截圖:
遇到問題
總體來說使用環信SDK進行客戶端集成的難度是比較低的。環信官方提供的IOS 客戶端已經實現了聊天的大部分功能,而且客戶端的代碼是開源的,開發者可以根據自己的需求在此之上進行相應的修改,這樣極大的簡化了客戶端的開發負擔。
開發過程中遇到的問題主要集中在配置方面,下面筆者對集成過程中幾個重點地方進行一下說明:
1、編譯環境配置
設置Linker
在Other Linker Flags 中添加 –ObjC,如下圖所示:
設置Architectures
2、AppKey設置
需要注意的是,registerSDKWithAppKey的參數一定要與創建應用時給定的AppKey相一致。
注: AppKey作爲客戶端應用程序的標識,在進行客戶端開發的時候會用到。
client_id和client_secret在客戶端與服務器進行數據交換時進行鑑權使用,它就是擁有超級權限的一個token,可以調REST接口,來處理管理員做的事情,使用它的人應有最高的權限,所以一般情況下對於普通的客戶端來說是用不到的。
3、環信管理後臺的使用
在進行測試開發的時候建議將用戶註冊模式改爲開放註冊,這樣便於客戶端的測試,筆者剛開始未注意到這個問題,導致註冊失敗。
上手難易
總的來說,筆者認爲使用環信IOS SDK進行集成開發是比較容易的,環信官方提供的IOS 客戶端已經實現了聊天的大部分功能,主要包括:好友管理、發送接收消息、羣組管理以及各種自定義配置選項。而且客戶端的代碼是開源的,開發者可以根據自己的需求在此之上進行相應的修改,在此強烈建議開發者在官方提供的客戶端之上進行相應的開發,這樣極大的簡化了客戶端的開發負擔,提高開發效率。
環信即時通訊雲服務使用標準的Auth 2.0協議進行身份鑑權,數據訪問使用REST API方式。在進行開發之前希望開發者可以學習一下相關的內容,有助於你對環信IOS SDK的使用。
開發文檔
1、開發中關於IOS SDK的問題參見IOS 在stack overflow的主頁:
http://stackoverflow.com/questions/tagged/ios
2、對於Autho2.0 的原理說明可以參考官方提供的地址 http://www.ruanyifeng.com/blog/2014/05/restful_api.html或者
http://blog.csdn.net/zhangzhebjut/article/details/26006905
RES API介紹參見環信官網:http://www.easemob.com/docs/rest/#rest
3、關於環信IOS SDK集成過程介紹,開發者可以參考官方提供的文檔說明:
http://www.easemob.com/docs/ios/
除此之外,開發者也可以參考官方提供的實例以提高開發效率。
4、環信IOS SDK使用的類接口文檔說明可以參見:
http://www.easemob.com/apidoc/ios/chat/