標籤:ios 融雲 即時通訊 融雲即時通訊雲 雲端服務 融雲sdk整合
相信大家在項目中會用到即時通訊功能,自己去寫的話會需要前後台合作,會大大加大開發的周期,所以考慮使用第三方的即時通訊平台,比如現在有融雲,環信....等等等!樓主在項目開發過程中使用到過環信和融雲,今天就為大家寫下IOS端整合環信的方法和可能會遇到的問題。(融雲提供聊天會話列表和聊天介面並且允許我們自訂,所以很方便的)。
一:準備工作
1.首先開啟融雲官網http://www.rongcloud.cn/,註冊自己的賬戶,並登陸。
2.點擊左邊建立應用,在開啟的小視窗填寫你的項目的一些資訊(如果正在開發階段可以選擇未電訊廠商務使用者,如果項目上線後是需要提交申請審核,後面再講)。
3.如果對介面沒有更高的定製要求,融雲本身提供的所有聊天等介面已經足夠使用,此時整合IMKit架構使用即可,推薦使用CocoaPods自動整合
三、整合
(1)產生Podfile檔案,選擇IM架構
platform :ios, ‘8.0‘target ‘RongCloudKit‘ do pod ‘RongCloudIM/IMLib‘, ‘2.8.0‘ //需要自己去定製UI介面 pod ‘RongCloudIM/IMKit‘, ‘2.8.0‘ //融雲提供完善的UI介面end
(
2)安裝後匯入標頭檔即可使用
650) this.width=650;" src="http://images2015.cnblogs.com/blog/791499/201612/791499-20161218174055183-143801096.png" alt="791499-20161218174055183-143801096.png" />
四、使用(RCIM是一個單例類,幾乎很多重要的操作都是由這個類來完成的)
(1)註冊融Cloud APPKey
//註冊融Cloud APPKEY[[RCIM sharedRCIM] initWithAppKey:APPKEY];
(2)登入融雲端服務器
650) this.width=650;" src="/img/fz.gif" alt="複製代碼" style="margin:0px;padding:0px;border:none;" />
//使用手動產生的token串連融雲端服務器進行登入 [[RCIM sharedRCIM] connectWithToken:TOKEN success:^(NSString *userId) { NSLog(@"登陸成功。當前登入的使用者ID:%@", userId); } error:^(RCConnectErrorCode status) { NSLog(@"登陸的錯誤碼為:%ld", status); } tokenIncorrect:^{ //token到期或者不正確。 //如果設定了token有效期間並且token到期,請重新請求您的伺服器擷取新的token //如果沒有設定token有效期間卻提示token錯誤,請檢查您用戶端和伺服器的appkey是否匹配,還有檢查您擷取token的流程。 NSLog(@"token錯誤"); }];
650) this.width=650;" src="/img/fz.gif" alt="複製代碼" style="margin:0px;padding:0px;border:none;" />
(3)顯示聊天介面代碼如下(此處我繼承了原生會話類RCConversationViewController)
650) this.width=650;" src="/img/fz.gif" alt="複製代碼" style="margin:0px;padding:0px;border:none;" />
// RongCloudConversationViewController.h// RongCloudKit//// Created by 夏遠全 on 16/12/17.// Copyright 2016年 廣州市東德網路科技有限公司. All rights reserved.//#import <RongIMKit/RongIMKit.h>@interface SystemConversationViewController : [email protected]
650) this.width=650;" src="/img/fz.gif" alt="複製代碼" style="margin:0px;padding:0px;border:none;" />
650) this.width=650;" src="/img/fz.gif" alt="複製代碼" style="margin:0px;padding:0px;border:none;" />
// RongCloudConversationViewController.m// RongCloudKit//// Created by 夏遠全 on 16/12/17.// Copyright 2016年 廣州市東德網路科技有限公司. All rights reserved.//#import "SystemConversationViewController.h"@interface SystemConversationViewController ()<RCIMUserInfoDataSource>@[email protected] SystemConversationViewController-(instancetype)init{ self = [super init]; //設定會話的類型,如單聊、討論群組、群聊、聊天室、客服、公眾服務會話等 self.conversationType = ConversationType_PRIVATE; //設定會話的目標會話ID。(單聊、客服、公眾服務會話為對方的ID,討論群組、群聊、聊天室為會話的ID) self.targetId = OTHERID; //設定聊天會話介面要顯示的標題 self.title = OTHERID; return self;}-(void)viewDidLoad{ [super viewDidLoad]; //使用者資訊提供者 [RCIM sharedRCIM].userInfoDataSource = self;}#pragma mark - <RCIMUserInfoDataSource>/*! 擷取使用者資訊 @param userId 使用者ID @param completion 擷取使用者資訊完成之後需要執行的Block [userInfo:該使用者ID對應的使用者資訊] @discussion SDK通過此方法擷取使用者資訊並顯示,請在completion中返回該使用者ID對應的使用者資訊。 在您設定了使用者資訊提供者之後,SDK在需要顯示使用者資訊的時候,會調用此方法,向您請求使用者資訊用於顯示。 */-(void)getUserInfoWithUserId:(NSString *)userId completion:(void (^)(RCUserInfo *))completion{ //設定使用者資訊 NSString *avatarURL = @"http://xxxxxx.com/static/avatar/137180371639017.jpeg"; RCUserInfo *userInfo = [[RCUserInfo alloc] initWithUserId:userId name:userId portrait:avatarURL]; //block回調設定使用者資訊 completion(userInfo);}@end
650) this.width=650;" src="/img/fz.gif" alt="複製代碼" style="margin:0px;padding:0px;border:none;" />
650) this.width=650;" src="/img/fz.gif" alt="複製代碼" style="margin:0px;padding:0px;border:none;" />
//聊天介面-(void)conversationStart{ //建立一個聊天會話View Controller對象、顯示聊天會話介面 SystemConversationViewController *chat = [[SystemConversationViewController alloc]init]; [self.navigationController pushViewController:chat animated:YES];}
650) this.width=650;" src="/img/fz.gif" alt="複製代碼" style="margin:0px;padding:0px;border:none;" />
(4)顯示會話列表介面代碼如下(此處我繼承了原生會話列表類RCConversationListViewController)
650) this.width=650;" src="/img/fz.gif" alt="複製代碼" style="margin:0px;padding:0px;border:none;" />
// ConversationListViewController.h// RongCloudKit//// Created by 夏遠全 on 16/12/17.// Copyright 2016年 廣州市東德網路科技有限公司. All rights reserved.//#import <RongIMKit/RongIMKit.h>@interface SystemConversationListViewController : [email protected]
650) this.width=650;" src="/img/fz.gif" alt="複製代碼" style="margin:0px;padding:0px;border:none;" />
650) this.width=650;" src="/img/fz.gif" alt="複製代碼" style="margin:0px;padding:0px;border:none;" />
// ConversationListViewController.m// RongCloudKit//// Created by 夏遠全 on 16/12/17.// Copyright 2016年 廣州市東德網路科技有限公司. All rights reserved.//#import "SystemConversationListViewController.h"#import "SystemConversationViewController.h"@interface SystemConversationListViewController ()<RCIMUserInfoDataSource>@[email protected] SystemConversationListViewController- (void)viewDidLoad { //重寫顯示相關的介面,必須先調用super,否則會屏蔽SDK預設的處理 [super viewDidLoad]; self.conversationListTableView.tableFooterView = [[UIView alloc] init]; //設定需要顯示哪些類型的會話 [self setDisplayConversationTypes:@[@(ConversationType_PRIVATE), @(ConversationType_DISCUSSION), @(ConversationType_CHATROOM), @(ConversationType_GROUP), @(ConversationType_APPSERVICE), @(ConversationType_SYSTEM)]]; //設定需要將哪些類型的會話在會話列表中彙總顯示 [self setCollectionConversationType:@[@(ConversationType_DISCUSSION), @(ConversationType_GROUP)]]; //使用者資訊提供者 [RCIM sharedRCIM].userInfoDataSource = self;}//重寫RCConversationListViewController的onSelectedTableRow事件- (void)onSelectedTableRow:(RCConversationModelType)conversationModelType conversationModel:(RCConversationModel *)model atIndexPath:(NSIndexPath *)indexPath { SystemConversationViewController *conversationVC = [[SystemConversationViewController alloc]init]; conversationVC.conversationType = model.conversationType; conversationVC.targetId = model.targetId; conversationVC.title = model.targetId; [self.navigationController pushViewController:conversationVC animated:YES];}#pragma mark - <RCIMUserInfoDataSource>/*! 擷取使用者資訊 @param userId 使用者ID @param completion 擷取使用者資訊完成之後需要執行的Block [userInfo:該使用者ID對應的使用者資訊] @discussion SDK通過此方法擷取使用者資訊並顯示,請在completion中返回該使用者ID對應的使用者資訊。 在您設定了使用者資訊提供者之後,SDK在需要顯示使用者資訊的時候,會調用此方法,向您請求使用者資訊用於顯示。 */-(void)getUserInfoWithUserId:(NSString *)userId completion:(void (^)(RCUserInfo *))completion{ //設定使用者資訊 NSString *avatarURL = @"http://xxxxxx.com/static/avatar/137180371639017.jpeg"; RCUserInfo *userInfo = [[RCUserInfo alloc] initWithUserId:userId name:userId portrait:avatarURL]; //block回調設定使用者資訊 completion(userInfo);}@end
650) this.width=650;" src="/img/fz.gif" alt="複製代碼" style="margin:0px;padding:0px;border:none;" />
650) this.width=650;" src="/img/fz.gif" alt="複製代碼" style="margin:0px;padding:0px;border:none;" />
//會話列表-(void)chatViewList{ //建立一個會話列表介面類,顯示所有的會話連絡人 SystemConversationListViewController *chatList = [[SystemConversationListViewController alloc] init]; [self.navigationController pushViewController:chatList animated:YES];}
五,示範
650) this.width=650;" src="https://s1.51cto.com/wyfs02/M01/9C/D8/wKiom1l27JDC0dkzAAFMuzZCpuQ778.png-wh_500x0-wm_3-wmp_4-s_3038853752.png" title="QQ圖片20170725150014.png" alt="wKiom1l27JDC0dkzAAFMuzZCpuQ778.png-wh_50" />
650) this.width=650;" src="https://s3.51cto.com/wyfs02/M01/9C/D8/wKioL1l27K_RZxzhAAHCcKi27u8520.png-wh_500x0-wm_3-wmp_4-s_3364657694.png" title="_20170725150045.png" alt="wKioL1l27K_RZxzhAAHCcKi27u8520.png-wh_50" />
六、提示一下
融雲的開發文檔些的相當詳細,我這兒寫一下純屬閑來無事,自娛自樂,有這方面需要的還是去看官方的文檔吧,那文檔的詳細,厲害了我的哥~~~
http://www.rongcloud.cn/docs/
IOS整合融雲SDK即時通訊