//聯(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:
============================代碼實現(xiàn)====================================
第一步在:AppDelegate 實現(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(@"登陸成功赠橙。當前登錄的用戶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 主控制器實現(xiàn)
/**
此代碼都是融云 SDK 文檔里面的只要看著文檔即可實現(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 {
//新建一個聊天會話View Controller對象
RCConversationViewController*chat = [[RCConversationViewControlleralloc]init];
//設(shè)置會話的類型凯力,如單聊兴喂、討論組再菊、群聊村怪、聊天室、客服馆截、公眾服務(wù)會話等
chat.conversationType=ConversationType_PRIVATE;
//設(shè)置會話的目標會話ID。(單聊亿眠、客服、公眾服務(wù)會話為對方的ID磅废,討論組纳像、群聊、聊天室為會話的ID)
chat.targetId=@"30000001";
//設(shè)置聊天會話界面要顯示的標題
chat.title=@"想顯示的會話標題";
//顯示聊天會話界面
[self.navigationControllerpushViewController:chatanimated:YES];
}
//列表
- (IBAction)list:(id)sender
{
LTTwoViewController* lttwo = [[SHTwoViewControlleralloc]init];
[self.navigationControllerpushViewController:lttwoanimated:YES];
}
//機器人
- (IBAction)robot:(id)sender
{
RCDCustomerServiceViewController*chatService = [[RCDCustomerServiceViewControlleralloc]init];
chatService.conversationType=ConversationType_CUSTOMERSERVICE;
chatService.targetId=@"KEFU148292606720649";
chatService.title=@"客服";
//? ? chatService.csInfo = csInfo; //用戶的詳細信息拯勉,此數(shù)據(jù)用于上傳用戶信息到客服后臺竟趾,數(shù)據(jù)的nickName和portraitUrl必須填寫。(目前該字段暫時沒用到谜喊,客服后臺顯示的用戶信息是你獲取token時傳的參數(shù)潭兽,之后會用到)
[self.navigationControllerpushViewController:chatServiceanimated:YES];
}
@end
第三步在:SHViewController 主控制器實現(xiàn)
1.實現(xiàn)聊天界面功能
上面已經(jīng)實現(xiàn)此功能
2.實現(xiàn)列表功能
.h文件
#import
@interfaceLTViewController :RCConversationListViewController
@end
.m文件
#import"LTViewController.h"
@interfaceLTViewController()
@end
@implementationLTViewController
- (void)viewDidLoad {
[superviewDidLoad];
}
@end
在加一個 XIB
3.實現(xiàn)客服機器人功能
必須實現(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的實現(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é)束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