融云开发demo
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