我好像特別喜歡做聊天室類的東東,剛折騰完微軟的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的對接。
附: