我好像特別喜歡做聊天室類的東東,剛折騰完微軟的SignalR又折騰App。本來想研究研究XMPP的,由於服務器的搭建問題,先采用一個第三方的吧,看看效果如何。聽到弟弟說他們公司用到了融雲,我也下載個SDK玩玩。融雲的Demo和文檔已經非常詳細了,我就不搬過來了。 融雲官方文檔地址:http://www.rongcloud.cn/docs/
第一步:首先把SDK導入到自己的項目中。還有其他依賴的framework都要加上。
第二步:我這里沒有自己寫UI,所以,直接用 <RongIMKit/RongIMKit.h> 融雲 UI庫。
第三步:在AppDelegate中初始化連接雲服務器操作
#import <RongIMKit/RongIMKit.h> #import <RongIMLib/RongIMLib.h> /*遵守融雲鏈接狀態監聽代理*/ @interface AppDelegate : UIResponder <UIApplicationDelegate,RCIMConnectionStatusDelegate> @property (strong, nonatomic) UIWindow *window; @end
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { /*即時通訊部分開始*/ [[RCIM sharedRCIM] initWithAppKey:kMSChatAppKey];//AppKey [[MSUserTool sharedMSUserTool] resetLoginIMServer]; //推送處理1 if([application respondsToSelector:@selector(registerUserNotificationSettings:)]){ UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeBadge | UIUserNotificationTypeAlert | UIUserNotificationTypeSound) 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]; /*即時通訊部分結束*/
然后在.m文件中添加如下代碼:
-(void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken { NSString *token = [[[deviceToken description] stringByReplacingOccurrencesOfString:@"<" withString:@""] stringByReplacingOccurrencesOfString:@">" withString:@""]; [[RCIMClient sharedRCIMClient] setDeviceToken:token]; } -(void)onRCIMConnectionStatusChanged:(RCConnectionStatus)status { //賬號被別人頂下來了 if (status == ConnectionStatus_KICKED_OFFLINE_BY_OTHER_CLIENT) { NSLog(@"您的賬號被別人頂下來了。。。"); } } - (void)didReceiveMessageNotification:(NSNotification *)notification { [UIApplication sharedApplication].applicationIconBadgeNumber = [UIApplication sharedApplication].applicationIconBadgeNumber + 1; }
至此,AppDelegate代碼部分結束了。主要是做一些RCIM的初始化操作。
第四步:搭建UI。我這里模擬了三個用戶。用戶ID分別為 1, 2 ,3.我在API調試中獲取了token,直接寫死了。后續我會發出如何調用ServerAPI獲取token的方法,涉及到請求頭設置和簽名的生成方法。
新建一個Controller。這個Controller主要就是相當於好友列表。這個需要自己搭建UI。這里面要實現的功能就是點擊里面的好友進入到聊天頁面。還有點擊最近聯系跳轉到最近的聯系人頁面。當然,如果想要聊天,需要用當前用戶的token連接服務器,調用SDK的方法即可:
#import <UIKit/UIKit.h> #import <RongIMKit/RongIMKit.h> @interface MSChatController : UITableViewController<RCIMUserInfoDataSource> @end
-(void)redirectToRecentController { BOOL hasLogin = [[MSUserTool sharedMSUserTool] isLoginIMServer]; //已經登錄過了 if (hasLogin) { NSLog(@"已經登陸過聊天服務器了..."); [self toChatList]; return; } //登錄雲 NSString *token = [MSUserTool sharedMSUserTool].currentUser.userToken; [[RCIM sharedRCIM] connectWithToken:token success:^(NSString *userId) { //記錄登錄狀態 [[MSUserTool sharedMSUserTool] loginIMServer]; [[RCIM sharedRCIM] setUserInfoDataSource:self]; dispatch_async(dispatch_get_main_queue(), ^{ [self toChatList]; }); } error:^(RCConnectErrorCode status) { } tokenIncorrect:^{ }]; } //跳轉到聊天頁面 -(void)toChatDetail:(MSCurrentUser *)user { RCConversationViewController *conversationVC = [[RCConversationViewController alloc]init]; conversationVC.conversationType = ConversationType_PRIVATE;//私人聊天類型 conversationVC.targetId = [NSString stringWithFormat:@"%ld", user.userId ]; //對方ID conversationVC.title = user.userName;//對方昵稱 conversationVC.hidesBottomBarWhenPushed = YES; [self.navigationController pushViewController:conversationVC animated:YES]; } //跳轉到最近聊天記錄頁面 -(void)toChatList { MSChatListViewController *chatListViewController = [[MSChatListViewController alloc]init]; chatListViewController.hidesBottomBarWhenPushed = YES; [self.navigationController pushViewController:chatListViewController animated:YES]; } //根據userId獲取用戶信息,實現此代理方法 - (void)getUserInfoWithUserId:(NSString *)userId completion:(void(^)(RCUserInfo* userInfo))completion { RCUserInfo *user = [[RCUserInfo alloc] init]; //獲取當前用戶信息,傳遞給RCUserInfo MSCurrentUser *loginUser = [[MSCurrentUser alloc] initWithUserId:[userId integerValue]]; user.userId = [NSString stringWithFormat:@"%ld",loginUser.userId]; user.name = loginUser.userName; user.portraitUri = loginUser.userPhoto; return completion(user); }
然后添加最主要也是比較簡單的一個Controller,因為是繼承自SDK的Controller所以,界面不用重新搭建。RCConversationListViewController (最近聯系人ontroller) 。RCConversationViewController(聊天詳情Controller)
然后,運行一下就可以啦,寫的不夠詳細,其實感覺如果實現一個簡單的SDK對接,很容易。他們給封裝的太好了。只要掌握里面其中幾個方法,就可以實現一個簡單的聊天室。1V1或者群組聊天等。當然,真正的項目開發怎么可能這么容易。以此總結融雲SDK的對接。
附: