iOS開發融雲即時通信集成詳細步驟html
1.融雲即時通信iOS SDK下載地址 http://rongcloud.cn/downloads 選擇iOS SDK下載ios
2.進行應用開發以前,須要先在融雲開發者平臺建立應用,若是您已經註冊了融雲開發者賬號,請前往 融雲開發者平臺 建立應用;若是您尚未註冊融雲開發者賬號,請前往 融雲官方網站 首先註冊開發者賬號,註冊後建立應用。註冊地址 https://developer.rongcloud.cn/signupweb
3.登錄融雲開發者平臺 https://developer.rongcloud.cn/signin 建立應用緩存
4.進入後臺以後點擊建立應用,進入這樣一個建立界面服務器
圖1微信
5.最後點擊建立 點擊個人應用 而後在左邊點擊個人應用名稱網絡
圖2app
6.點擊AppKey進入async
圖3ide
7.手動安裝融雲即時通信SDK
7.1將下載好的最新的融雲SDK導入到本身的項目中
7.2添加依賴庫 在Build Phases中第三個選項link中點擊左下角+號添加依賴庫
所需的依賴庫
圖4
8.獲取Token
和第五步同樣,進入融雲後臺點擊個人應用—>本身的應用名稱—>IM服務—>API調試
右邊會進入一個界面,在這裏獲取調試Token
圖5
填的時候能夠按照這個參數填,就是個案例
用戶 Id:
userId = "1" // 用戶在融雲系統中惟一的身份 Id,可爲任意數字或字符串,但必須保證全局惟一。
用戶名稱:
name = "韓梅梅" // 用戶的顯示名稱,用來在 Push 推送時,或者客戶端沒有提供用戶信息時,顯示用戶的名稱。
用戶頭像圖片:
portraitUri = "http://rongcloud-web.qiniudn.com/docs_demo_rongcloud_logo.png"
如今咱們得到了AppKey和Token了
9.下面就開始快速集成了
9.1
在本身的項目中AppDelegate.h文件中導入頭文件
#import <RongIMLib/RongIMLib.h>
#import <RongIMKit/RongIMKit.h>
而後遵照RCIMConnectionStatusDelegate這個代理方法
即變成這樣@interface AppDelegate : UIResponder <UIApplicationDelegate,RCIMConnectionStatusDelegate>
9.2在AppDelegate.m文件中導入頭文件
//融雲即時通信
#import <RongIMKit/RongIMKit.h>
#import <RongIMLib/RongIMLib.h>
#import <UIKit/UIKit.h>
而後將得到的融雲的AppKey 寫成一個宏 以下 將本身的AppKey 替換便可\
k51hidwq1bbcdds4b將這個換成本身的便可
//融雲即時通信AppKey
#define RONGCLOUD_IM_APPKEY @"k51hidwq1bbcdds4b"
10.在AppDelegate.m的文件中的
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
}
方法中加入如下代碼
//融雲即時通信
//初始化融雲SDK。
[[RCIM sharedRCIM] initWithAppKey:RONGCLOUD_IM_APPKEY];
/**
* 推送處理1
*/
if ([application
respondsToSelector:@selector(registerUserNotificationSettings:)]) {
//註冊推送, iOS 8
UIUserNotificationSettings *settings = [UIUserNotificationSettings
settingsForTypes:(UIUserNotificationTypeBadge |
UIUserNotificationTypeSound |
UIUserNotificationTypeAlert)
categories:nil];
[application registerUserNotificationSettings:settings];
} else {
UIRemoteNotificationType myTypes = UIRemoteNotificationTypeBadge |
UIRemoteNotificationTypeAlert |
UIRemoteNotificationTypeSound;
[application registerForRemoteNotificationTypes:myTypes];
}
//融雲即時通信
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:@selector(didReceiveMessageNotification:)
name:RCKitDispatchMessageNotification
object:nil];
[[RCIM sharedRCIM] setConnectionStatusDelegate:self];
加入到方法中的代碼到這裏
下面是單獨的方法 直接加在AppDelegate.m的文件中便可
/**
* 將獲得的devicetoken 傳給融雲用於離線狀態接收push ,您的app後臺要上傳推送證書
*
* @param application <#application description#>
* @param deviceToken <#deviceToken description#>
*/
- (void)application:(UIApplication *)application
didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
NSString *token =
[[[[deviceToken description] stringByReplacingOccurrencesOfString:@"<"
withString:@""]
stringByReplacingOccurrencesOfString:@">"
withString:@""]
stringByReplacingOccurrencesOfString:@" "
withString:@""];
[[RCIMClient sharedRCIMClient] setDeviceToken:token];
}
/**
* 網絡狀態變化。
*
* @param status 網絡狀態。
*/
- (void)onRCIMConnectionStatusChanged:(RCConnectionStatus)status {
if (status == ConnectionStatus_KICKED_OFFLINE_BY_OTHER_CLIENT) {
UIAlertView *alert = [[UIAlertView alloc]
initWithTitle:@"提示"
message:@"您"
@"的賬號在別的設備上登陸,您被迫下線!"
delegate:nil
cancelButtonTitle:@"知道了"
otherButtonTitles:nil, nil];
[alert show];
//注意這裏下面的4行,根據本身須要修改 也能夠註釋了,可是隻能註釋這4行,網絡狀態變化這個方法必定要實現
ViewController *loginVC = [[ViewController alloc] init];
UINavigationController *_navi =
[[UINavigationController alloc] initWithRootViewController:loginVC];
self.window.rootViewController = _navi;
}
}
- (void)didReceiveMessageNotification:(NSNotification *)notification {
[UIApplication sharedApplication].applicationIconBadgeNumber =
[UIApplication sharedApplication].applicationIconBadgeNumber + 1;
}
11.開始建立會話
先建立一個繼承RCConversationListViewController名爲ChatListViewController的控制器
建立以後的控制器.h文件即爲
#import <UIKit/UIKit.h>
#import <RongIMKit/RongIMKit.h>
@interface ChatListViewController : RCConversationListViewController
@end
這樣的樣式
在你要建立即時會話的界面的控制器的.h文件中導入頭文件
//融雲即時通信
#import <RongIMKit/RongIMKit.h>
並遵照數據源方法RCIMUserInfoDataSource
即變成了
#import <RongIMKit/RongIMKit.h>
@interface ViewController : UIViewController<RCIMUserInfoDataSource>
在.m文件中導入頭文件
//融雲即時通信
#import "ChatListViewController.h"
#import <RongIMKit/RCConversationViewController.h>
將咱們獲取的Token定義成宏 就像這樣的格式 換成本身的Token便可
//融雲即時通信Token
#define RONGCLOUD_IM_Token @"LU0IpXzEeYXUxuJi5n9hAwNcet2QRQu/IRxLhvshFhvLm8f3gdUu+y4TIhufZfJ/fIXRJrQyBu8cJAN2bcAolA=="
好比在我所在的控制器我有一個開始回答按鈕
我想在這個控制器點擊開始回答按鈕就想讓他建立即時會話
這樣來實現,點擊開始回答按鈕
/**
* 點擊開始回答執行的方法
*/
-(void)startAnswer
{
//登錄融雲
//登陸融雲服務器,開始階段能夠先從融雲API調試網站獲取,以後token須要經過服務器到融雲服務器取。
NSString *token=RONGCLOUD_IM_Token;
[[RCIM sharedRCIM] connectWithToken:token success:^(NSString *userId) {
//設置用戶信息提供者,頁面展示的用戶頭像及暱稱都會今後代理取 這裏會跳到會話列表界面 就是咱們日常QQ聊天都有一個
會話的列表 若是想直接跳到聊天界面 下面再說
[[RCIM sharedRCIM] setUserInfoDataSource:self];
NSLog(@"Login successfully with userId: %@.", userId);
dispatch_async(dispatch_get_main_queue(), ^{
ChatListViewController *chatListViewController = [[ChatListViewController alloc]init];
[self.navigationController pushViewController:chatListViewController animated:YES];
});
} error:^(RCConnectErrorCode status) {
NSLog(@"login error status: %ld.", (long)status);
} tokenIncorrect:^{
NSLog(@"token 無效 ,請確保生成token 使用的appkey 和初始化時的appkey 一致");
}];
} error:^(RCConnectErrorCode status) {
NSLog(@"login error status: %ld.", (long)status);
} tokenIncorrect:^{
NSLog(@"token 無效 ,請確保生成token 使用的appkey 和初始化時的appkey 一致");
}];
YYCLog(@"點擊了開始回答");
}
而後在這個控制器再實現一個方法 就是下面這個方法
/**
*此方法中要提供給融雲用戶的信息,建議緩存到本地,而後改方法每次從您的緩存返回
*/
- (void)getUserInfoWithUserId:(NSString *)userId completion:(void(^)(RCUserInfo* userInfo))completion
{
//此處爲了演示寫了一個用戶信息
if ([@"1" isEqual:userId]) {
RCUserInfo *user = [[RCUserInfo alloc]init];
user.userId = @"1";
user.name = @"測試1";
user.portraitUri = @"https://ss0.baidu.com/73t1bjeh1BF3odCf/it/u=1756054607,4047938258&fm=96&s=94D712D20AA1875519EB37BE0300C008";
return completion(user);
}else if([@"2" isEqual:userId]) {
RCUserInfo *user = [[RCUserInfo alloc]init];
user.userId = @"2";
user.name = @"測試2";
user.portraitUri = @"https://ss0.baidu.com/73t1bjeh1BF3odCf/it/u=1756054607,4047938258&fm=96&s=94D712D20AA1875519EB37BE0300C008";
return completion(user);
}
}
這個方法也要在這個.m文件中實現
這裏都是測試 先這樣寫 我到後面再寫怎麼具體實現
下面代碼都同樣
下面就是在咱們的ChatListViewController.h文件中
#import <RongIMKit/RongIMKit.h>
#import <RongIMKit/RongIMKit.h>
@interface ChatListViewController : RCConversationListViewController
@end
在.m文件中 這是會話列表界面 點擊右上角的單聊便可以開始聊天 就像咱們的微信聊天同樣的界面
想要和不一樣的人聊天 只要將conversationVC.targetId = @"user」;後面的user改一下就好了 如今時界面搭建 這樣
界面就搭建好了
// 會話聊天界面
#import "ChatListViewController.h"
@interface ChatListViewController ()
@end
@implementation ChatListViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self setDisplayConversationTypes:@[@(ConversationType_PRIVATE),@(ConversationType_DISCUSSION)]];
//自定義導航左右按鈕
UIBarButtonItem *rightButton = [[UIBarButtonItem alloc]initWithTitle:@"單聊" style:UIBarButtonItemStylePlain target:self action:@selector(rightBarButtonItemPressed:)];
[rightButton setTintColor:[UIColor whiteColor]];
UIButton *backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
backBtn.frame = CGRectMake(0, 6, 67, 23);
UIImageView *backImg = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"navigator_btn_back"]];
backImg.frame = CGRectMake(-10, 0, 22, 22);
[backBtn addSubview:backImg];
UILabel *backText = [[UILabel alloc] initWithFrame:CGRectMake(12, 0, 65, 22)];
backText.text = @"退出";
backText.font = [UIFont systemFontOfSize:15];
[backText setBackgroundColor:[UIColor clearColor]];
[backText setTextColor:[UIColor whiteColor]];
[backBtn addSubview:backText];
[backBtn addTarget:self action:@selector(leftBarButtonItemPressed:) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *leftButton = [[UIBarButtonItem alloc] initWithCustomView:backBtn];
[self.navigationItem setLeftBarButtonItem:leftButton];
self.navigationItem.rightBarButtonItem = rightButton;
self.conversationListTableView.tableFooterView = [UIView new];
}
/**
*重寫RCConversationListViewController的onSelectedTableRow事件
*
* @param conversationModelType 數據模型類型
* @param model 數據模型
* @param indexPath 索引
*/
-(void)onSelectedTableRow:(RCConversationModelType)conversationModelType conversationModel:(RCConversationModel *)model atIndexPath:(NSIndexPath *)indexPath
{
RCConversationViewController *conversationVC = [[RCConversationViewController alloc]init];
conversationVC.conversationType =model.conversationType;
conversationVC.targetId = model.targetId;
conversationVC.userName =model.conversationTitle;
conversationVC.title = model.conversationTitle;
[self.navigationController pushViewController:conversationVC animated:YES];
}
-(void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
self.tabBarController.navigationItem.title = @"會話";
}
/**
* 退出登陸
*
* @param sender <#sender description#>
*/
- (void)leftBarButtonItemPressed:(id)sender {
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"提示" message:@"肯定要退出?" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"退出", nil];
[alertView show];
}
/**
* 重載右邊導航按鈕的事件 zheli
*
* @param sender <#sender description#>
*/
-(void)rightBarButtonItemPressed:(id)sender
{
RCConversationViewController *conversationVC = [[RCConversationViewController alloc]init];
conversationVC.conversationType =ConversationType_PRIVATE;
conversationVC.targetId = @"user"; //這裏模擬本身給本身發消息,您能夠替換成其餘登陸的用戶的UserId
conversationVC.userName = @"測試1";
conversationVC.title = @"自問自答";
[self.navigationController pushViewController:conversationVC animated:YES];
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
if (buttonIndex == 1) {
[[RCIM sharedRCIM]disconnect];
[self.navigationController popViewControllerAnimated:YES];
}
}
@end
這樣融雲即時聊天界面就搭建好了,就只是個測試界面 後面我會更新完整的融雲即時聊天
這樣集成的呢,是有會話列表的,若是不想點擊開始回答進入會話列表界面,而是直接進入聊天界面
直接將
RCConversationViewController *conversationVC = [[RCConversationViewController alloc]init];
conversationVC.conversationType =ConversationType_PRIVATE;
conversationVC.targetId = @"user"; //這裏模擬本身給本身發消息,您能夠替換成其餘登陸的用戶的UserId
conversationVC.userName = @"測試1";
conversationVC.title = @"自問自答";
[self.navigationController pushViewController:conversationVC animated:YES];
這段代碼複製粘貼到
/**
* 點擊開始回答執行的方法
*/
-(void)startAnswer
{
}
這個方法裏,可是就是必須先登錄融雲服務器 而後將
[[RCIM sharedRCIM] setUserInfoDataSource:self];
NSLog(@"Login successfully with userId: %@.", userId);
dispatch_async(dispatch_get_main_queue(), ^{
ChatListViewController *chatListViewController = [[ChatListViewController alloc]init];
[self.navigationController pushViewController:chatListViewController animated:YES];
});
這段代碼刪了
就變成了
/**
* 點擊開始回答執行的方法
*/
-(void)startAnswer
{
//登錄融雲
//登陸融雲服務器,開始階段能夠先從融雲API調試網站獲取,以後token須要經過服務器到融雲服務器取。
NSString *token=RONGCLOUD_IM_Token;
[[RCIM sharedRCIM] connectWithToken:token success:^(NSString *userId) {
} error:^(RCConnectErrorCode status) {
NSLog(@"login error status: %ld.", (long)status);
} tokenIncorrect:^{
NSLog(@"token 無效 ,請確保生成token 使用的appkey 和初始化時的appkey 一致");
}];
//直接跳到聊天界面
RCConversationViewController *conversationVC = [[RCConversationViewController alloc]init];
conversationVC.conversationType =ConversationType_PRIVATE;
conversationVC.targetId = @"user"; //這裏模擬本身給本身發消息,您能夠替換成其餘登陸的用戶的UserId
conversationVC.userName = @"測試1";
conversationVC.title = @"自問自答";
[self.navigationController pushViewController:conversationVC animated:YES];
//
//
//
YYCLog(@"點擊了開始回答");
}
這樣點擊開始回答按鈕就直接進入了聊天界面 就像咱們的微信聊天界面同樣的
這樣就集成好了
更多進階請參考一下官方文檔
官方網站集成文檔地址:http://www.rongcloud.cn/docs/ios.html