appDelegate
#define RONGCLOUD_IM_APPKEY @"k51hidwq1m2sb" #define kDeviceToken @"lzKtgLtA9vbCYX0V877Gc2JMSvnicNVKHA7I55vnR+9v4XolYQKISmmhuD3OPjzX85ejuwdM85I=" #import "AppDelegate.h" #import "ViewController.h" #import <RongIMKit/RongIMKit.h> @interface AppDelegate () @end @implementation AppDelegate - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; NSString *_deviceTokenCache = [[NSUserDefaults standardUserDefaults]objectForKey:kDeviceToken]; //初始化融雲SDK, [[RCIM sharedRCIM] initWithAppKey:RONGCLOUD_IM_APPKEY]; [[RCIM sharedRCIM] connectWithToken:kDeviceToken success:^(NSString *userId) { NSLog(@"連接成功"); } error:^(RCConnectErrorCode status) { } tokenIncorrect:^{ }]; #ifdef __IPHONE_8_0 // 在 iOS 8 下注冊蘋果推送,申請推送權限。 UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeBadge |UIUserNotificationTypeSound |UIUserNotificationTypeAlert) categories:nil]; [[UIApplication sharedApplication] registerUserNotificationSettings:settings]; #else // 注冊蘋果推送,申請推送權限。 [[UIApplication sharedApplication] registerForRemoteNotificationTypes:UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeSound]; #endif // 初始化 ViewController。 ViewController *viewController = [[ViewController alloc] init]; // 初始化 UINavigationController。 UINavigationController *nav = [[UINavigationController alloc]initWithRootViewController:viewController]; // 設置背景顏色為黑色。 [nav.navigationBar setBackgroundColor:[UIColor blackColor]]; // 初始化 rootViewController。 self.window.rootViewController = nav; self.window.backgroundColor = [UIColor whiteColor]; [self.window makeKeyAndVisible]; return YES; } #ifdef __IPHONE_8_0 - (void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings { // Register to receive notifications. [application registerForRemoteNotifications]; } - (void)application:(UIApplication *)application handleActionWithIdentifier:(NSString *)identifier forRemoteNotification:(NSDictionary *)userInfo completionHandler:(void(^)())completionHandler { // Handle the actions. if ([identifier isEqualToString:@"declineAction"]){ } else if ([identifier isEqualToString:@"answerAction"]){ } } #endif // 獲取蘋果推送權限成功。 -(void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken { // 設置 deviceToken。 } -(void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error { } // 獲取用戶信息的方法。 -(void)getUserInfoWithUserId:(NSString *)userId completion:(void(^)(RCUserInfo* userInfo))completion { // 此處最終代碼邏輯實現需要您從本地緩存或服務器端獲取用戶信息。 if ([@"1" isEqual:userId]) { RCUserInfo *user = [[RCUserInfo alloc]init]; user.userId = @"1"; user.name = @"韓梅梅"; user.portraitUri = @"http://rongcloud-web.qiniudn.com/docs_demo_rongcloud_logo.png"; return completion(user); } if ([@"2" isEqual:userId]) { RCUserInfo *user = [[RCUserInfo alloc]init]; user.userId = @"2"; user.name = @"李雷"; user.portraitUri = @"http://rongcloud-web.qiniudn.com/docs_demo_rongcloud_logo.png"; return completion(user); } return completion(nil); } // 獲取群組信息的方法。 -(void)getGroupInfoWithGroupId:(NSString*)groupId completion:(void (^)(RCGroup *group))completion { // 此處最終代碼邏輯實現需要您從本地緩存或服務器端獲取群組信息。 if ([@"1" isEqual:groupId]) { RCGroup *group = [[RCGroup alloc]init]; group.groupId = @"1"; group.groupName = @"同城交友"; //group.portraitUri = @"http://rongcloud-web.qiniudn.com/docs_demo_rongcloud_logo.png"; return completion(group); } if ([@"2" isEqual:groupId]) { RCGroup *group = [[RCGroup alloc]init]; group.groupId = @"2"; group.groupName = @"跳蚤市場"; //group.portraitUri = @"http://rongcloud-web.qiniudn.com/docs_demo_rongcloud_logo.png"; return completion(group); } return completion(nil); }
viewController
#import "ViewController.h" #import "ChartListViewController.h" #import <RongIMKit/RongIMKit.h> @interface ViewController () @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; // 單聊 UIButton *singleChart = [[UIButton alloc] initWithFrame:CGRectMake(100, 100, 100, 30)]; [singleChart setTitle:@"開始單聊" forState:UIControlStateNormal]; [singleChart setTitleColor:[UIColor blackColor] forState:UIControlStateNormal]; [singleChart addTarget:self action:@selector(singleChart) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:singleChart]; // 創建聊天列表 UIButton *listBtn = [[UIButton alloc] initWithFrame:CGRectMake(100, 150, 100, 30)]; [listBtn setTitle:@"進入列表" forState:UIControlStateNormal]; [listBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal]; [listBtn addTarget:self action:@selector(showList) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:listBtn]; } /** * 單聊界面 */ - (void)singleChart { // 您需要根據自己的 App 選擇場景觸發聊天。這里的例子是一個 Button 點擊事件。 RCConversationViewController *conversationVC = [[RCConversationViewController alloc]init]; conversationVC.conversationType =ConversationType_PRIVATE; //會話類型,這里設置為 PRIVATE 即發起單聊會話。 conversationVC.targetId = @"1"; // 接收者的 targetId,這里為舉例。 conversationVC.userName = @"袁俊亮"; // 接受者的 username,這里為舉例。 conversationVC.title = @"無聊"; // 會話的 title。 // 把單聊視圖控制器添加到導航棧。 [self.navigationController pushViewController:conversationVC animated:YES]; } /** * 顯示列表 */ - (void)showList { ChartListViewController *chatListViewController = [[ChartListViewController alloc] initWithDisplayConversationTypes:@[@(ConversationType_GROUP),@(ConversationType_DISCUSSION),@(ConversationType_PRIVATE)] collectionConversationType:@[@(ConversationType_GROUP),@(ConversationType_DISCUSSION),@(ConversationType_PRIVATE)]]; [self.navigationController pushViewController:chatListViewController animated:YES]; } @end
ChartListViewController
#import "ChartListViewController.h" @interface ChartListViewController () @end @implementation ChartListViewController - (void)viewDidLoad { [super viewDidLoad]; } - (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]; } @end