iOS 融云即時(shí)通訊~集成 (IM)

//聯(lián)系人:石虎QQ: 1224614774昵稱:嗡嘛呢叭咪

/**

注意:先看效果圖-->1.登錄融云-->2.生成 AppKey 和token -->3.下載 SDK --->4.看集成 SDK 文檔

*/

1.效果圖:

2.注冊 AppKey 和 token

2.1融云的登錄界面鏈接

https://developer.rongcloud.cn/signin?returnUrl=%2Fapp%2Fappkey%2FcAwpV5QM0FXNqq8TJfA%3D

圖1:

圖2:

圖3:

圖4:

圖5:

3.下載 SDK

集成 SDK 鏈接

http://www.rongcloud.cn/docs/ios.html#integration

圖1:

圖2:

4.看集成 SDK 文檔

圖1:

圖2:

圖3:

============================代碼實(shí)現(xiàn)====================================

第一步在:AppDelegate 實(shí)現(xiàn)

#import"AppDelegate.h"

#import

#import"SHViewController.h"

@interfaceAppDelegate()

@end

@implementationAppDelegate

- (BOOL)application:(UIApplication*)application didFinishLaunchingWithOptions:(NSDictionary*)launchOptions {

self.window.rootViewController= [[UINavigationControlleralloc]initWithRootViewController:[[ZHUViewControlleralloc]init]];

[selfappKey];

returnYES;

}

-(void)appKey

{

[[RCIMsharedRCIM]initWithAppKey:@"自己申請好的AppKey"];

[[RCIMsharedRCIM]connectWithToken:@"自己申請好的token"success:^(NSString*userId)

{

NSLog(@"登陸成功。當(dāng)前登錄的用戶ID:%@", userId);

[[RCIMsharedRCIM]setUserInfoDataSource:self];

}error:^(RCConnectErrorCodestatus)

{

NSLog(@"登陸的錯誤碼為:%d", (int)status);

}

tokenIncorrect:^{

NSLog(@"token錯誤");

}];

}

//代理

- (void)getUserInfoWithUserId:(NSString*)userId

completion:(void(^)(RCUserInfo*userInfo))completion

{

if([userIdisEqualToString:@"30000001"])

{

RCUserInfo* userInfo = [[RCUserInfoalloc]init];

userInfo.userId= userId;

userInfo.name=@"小石頭";

userInfo.portraitUri=@"http://file03.16sucai.com/2016/01/2016xqk0xw3e31u.jpg";

returncompletion(userInfo);

}

returncompletion(nil);

}

@end

第二步在:SHViewController 主控制器實(shí)現(xiàn)

/**

此代碼都是融云 SDK 文檔里面的只要看著文檔即可實(shí)現(xiàn)

*/

導(dǎo)入 SDK:

#import"SHViewController.h"

#import"LTViewController.h"

#import

#import"SHTwoViewController.h"

#import"RCDCustomerServiceViewController.h"

@interfaceSHViewController ()

@end

@implementationSHViewController

-(void)viewWillAppear:(BOOL)animated

{//導(dǎo)航欄顯示

self.navigationController.navigationBar.hidden=NO;

}

- (void)viewDidLoad {

[superviewDidLoad];

}

//聊天界面

- (IBAction)button:(id)sender {

//新建一個(gè)聊天會話View Controller對象

RCConversationViewController*chat = [[RCConversationViewControlleralloc]init];

//設(shè)置會話的類型,如單聊、討論組、群聊、聊天室、客服、公眾服務(wù)會話等

chat.conversationType=ConversationType_PRIVATE;

//設(shè)置會話的目標(biāo)會話ID。(單聊、客服、公眾服務(wù)會話為對方的ID,討論組、群聊、聊天室為會話的ID)

chat.targetId=@"30000001";

//設(shè)置聊天會話界面要顯示的標(biāo)題

chat.title=@"想顯示的會話標(biāo)題";

//顯示聊天會話界面

[self.navigationControllerpushViewController:chatanimated:YES];

}

//列表

- (IBAction)list:(id)sender

{

LTTwoViewController* lttwo = [[SHTwoViewControlleralloc]init];

[self.navigationControllerpushViewController:lttwoanimated:YES];

}

//機(jī)器人

- (IBAction)robot:(id)sender

{

RCDCustomerServiceViewController*chatService = [[RCDCustomerServiceViewControlleralloc]init];

chatService.conversationType=ConversationType_CUSTOMERSERVICE;

chatService.targetId=@"KEFU148292606720649";

chatService.title=@"客服";

//? ? chatService.csInfo = csInfo; //用戶的詳細(xì)信息,此數(shù)據(jù)用于上傳用戶信息到客服后臺,數(shù)據(jù)的nickName和portraitUrl必須填寫。(目前該字段暫時(shí)沒用到,客服后臺顯示的用戶信息是你獲取token時(shí)傳的參數(shù),之后會用到)

[self.navigationControllerpushViewController:chatServiceanimated:YES];

}

@end

第三步在:SHViewController 主控制器實(shí)現(xiàn)

1.實(shí)現(xiàn)聊天界面功能

上面已經(jīng)實(shí)現(xiàn)此功能

2.實(shí)現(xiàn)列表功能

.h文件

#import

@interfaceLTViewController :RCConversationListViewController

@end

.m文件

#import"LTViewController.h"

@interfaceLTViewController()

@end

@implementationLTViewController

- (void)viewDidLoad {

[superviewDidLoad];

}

@end

在加一個(gè) XIB

3.實(shí)現(xiàn)客服機(jī)器人功能

必須實(shí)現(xiàn)RCDCustomerServiceViewController類

.h文件

#import

@interfaceRCDCustomerServiceViewController :RCConversationViewController

@end

.m文件

#import"RCDCustomerServiceViewController.h"

@interfaceRCDCustomerServiceViewController()

@end

@implementationRCDCustomerServiceViewController

- (void)viewDidLoad {

[superviewDidLoad];

[selfnotifyUpdateUnreadMessageCount];

self.navigationItem.rightBarButtonItem=nil;

}

- (void)leftBarButtonItemPressed:(id)sender {

//需要調(diào)用super的實(shí)現(xiàn)

[superleftBarButtonItemPressed:sender];

[self.navigationControllerpopViewControllerAnimated:YES];

}

- (void)commentCustomerServiceWithStatus:(RCCustomerServiceStatus)serviceStatus

commentId:(NSString*)commentId

quitAfterComment:(BOOL)isQuit {

[supercommentCustomerServiceWithStatus:serviceStatus

commentId:commentId

quitAfterComment:isQuit];

}

//*********應(yīng)用自定義評價(jià)界面結(jié)束2*************

- (void)notifyUpdateUnreadMessageCount {

__weaktypeof(&*self) __weakself =self;

intcount = [[RCIMClientsharedRCIMClient]getUnreadCount:@[

@(ConversationType_PRIVATE),

@(ConversationType_DISCUSSION),

@(ConversationType_APPSERVICE),

@(ConversationType_PUBLICSERVICE),

@(ConversationType_GROUP)

]];

dispatch_async(dispatch_get_main_queue(), ^{

NSString*backString =nil;

if(count >0&& count <1000) {

backString = [NSStringstringWithFormat:@"返回(%d)", count];

}elseif(count >=1000) {

backString =@"返回(...)";

}else{

backString =@"返回";

}

UIImageView*backImg = [[UIImageViewalloc]

initWithImage:[UIImageimageNamed:@"navigator_btn_back"]];

backImg.frame=CGRectMake(-6,4,10,17);

UILabel*backText =

[[UILabelalloc]initWithFrame:CGRectMake(9,4,85,17)];

backText.text= backString;//

backText.font= [UIFontsystemFontOfSize:17];

[backTextsetBackgroundColor:[UIColorclearColor]];

[backTextsetTextColor:[UIColorwhiteColor]];

});

}

@end

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容