本篇文章介绍下基于环信SDK3.2.3 demo 里面的音视频UI 快速集成到自己的项目当中,如有和自己项目不一样的业务逻辑,还请自己变通.
1.首先我建立一个空的项目,同时建立一个根控制器,也就是TabbarController.
(前提是项目已经集成SDK3.2.3和EaseUI)
2.在已下载的环信demo拖入call文件夹到自己的项目中.
3.在AppDelegate引入#import "DemoCallManager.h" 进行sdk初始化,注册登录加载主页MainViewController
AppDelegate.h AppDelegate.m 文件内容如下:
#import <UIKit/UIKit.h>
@class MainViewController;
@interface AppDelegate : UIResponder <UIApplicationDelegate>
@property (strong, nonatomic) UIWindow *window;
@property (strong, nonatomic) MainViewController *mainVC;
@end
#import "AppDelegate.h"
#import "MainViewController.h"
#import "DemoCallManager.h"
@interface AppDelegate ()
@end
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.appkey:dragonxuxiaolong#huanxinchat2
//easemob-demo#chatdemoui
self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
EMOptions *option = [EMOptions optionsWithAppkey:@"easemob-demo#chatdemoui"];
//推送证书没有直接不需要注释
//option.apnsCertName = @"devpush";
[[EMClient sharedClient] initializeSDKWithOptions:option];
NSLog(@"版本号===%@",[[EMClient sharedClient] version]);
//IM后台,需要设置开放注册模式,才可以再手机端注册环信id
EMError *regErr = [[EMClient sharedClient] registerWithUsername:@"123456" password:@"123456"];
if (!regErr) {
NSLog(@"同步注册成功");
}
//自动登录使用
BOOL isAutoLogin = [[EMClient sharedClient].options isAutoLogin];
//判断是否设置过自动登录
if (isAutoLogin) { //为YES, 直接加载主页根控制器
[self goHomePage];
} else {//为NO, 就去登录,然后再加载主页根控制器
EMError *error = [[EMClient sharedClient] loginWithUsername:@"123456" password:@"123456"];
if (!error) {
//登录成功后设置自动登录
[[EMClient sharedClient].options setIsAutoLogin:YES];
NSLog(@"%d=====%d",[[EMClient sharedClient] isLoggedIn],[[EMClient sharedClient] isConnected]);
[self goHomePage];
}else{
NSLog(@"%d",error.code);
}
}
// [[EMClient sharedClient] registerWithUsername:@"321" password:@"321" completion:^(NSString *aUsername, EMError *aError) {
// if (!aError) {
// NSLog(@"异步注册成功");
// }
// NSLog(@"error == %d",aError.code);
// }];
return YES;
}
- (void)goHomePage{
MainViewController *mainVC = [[MainViewController alloc] init];
//appdelegate 持有根控制器 方便其他地方调用 不要重复初始化保证根控制器唯一
self.mainVC = mainVC;
self.window.rootViewController = mainVC;
[self.window makeKeyAndVisible];
[[DemoCallManager sharedManager] setMainController:mainVC];
}
- (void)applicationWillTerminate:(UIApplication *)application {
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
// Saves changes in the application's managed object context before the application terminates.
[[DemoCallManager sharedManager] hangupCallWithReason:EMCallEndReasonHangup];
[self saveContext];
}
根控制器代码简单指定聊天界面仅参考:
#import "MainViewController.h"
#import "ViewController.h"
@interface MainViewController ()
@end
@implementation MainViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
ViewController *test = [[ViewController alloc] init];
UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:test];
[self addChildViewController:nav];
[test.tabBarItem setTitle:@"聊天"];
self.toolbarItems = @[test.tabBarItem];
self.viewControllers = @[nav];
}
简单初始化EaseUI聊天界面并跳转;
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
UIButton *chatToVideoBtn = [UIButton buttonWithType:(UIButtonTypeCustom)];
[chatToVideoBtn setFrame:CGRectMake(100, 100, 200, 50)];
[chatToVideoBtn setTitle:@"goChatVideo" forState:(UIControlStateNormal)];
[chatToVideoBtn addTarget:self action:@selector(goChatVideo) forControlEvents:(UIControlEventTouchUpInside)];
[self.view addSubview:chatToVideoBtn];
}
- (void)goChatVideo{
// 参数1: 对方环信id
// 参数2: 聊天类型:
// typedef enum{
// EMConversationTypeChat = 0, 单聊 /*! one to one chat room type */
// EMConversationTypeGroupChat, 群组 /*! Group chat room type */
// EMConversationTypeChatRoom 聊天室 /*! Chatroom chat room type */
// } EMConversationType;
EaseMessageViewController *chatVC = [[EaseMessageViewController alloc] initWithConversationChatter:@"654321" conversationType:EMConversationTypeChat];
//隐藏下面tabbar
chatVC.hidesBottomBarWhenPushed = YES;
[self.navigationController pushViewController:chatVC animated:YES];
}
现在编译走起,报错了不要担心一步一步解决
然后我这里使用环信appstore 上架demo 发起视频 体验即刻开始
从上面断点看到我们的发起成功,并模态界面如下图:
使用真机接受请求,模拟器画面如下图:
哈哈,大功告成!!!
下面实现下导航栏跳转视频
直接截图上代码 找到对应方法和类即可
以上逻辑是自己主动发起视频设置, 关于被邀请同理在- (void)callDidReceive:(EMCallSession *)aSession代理回调里面;把模态改下导航push即可. 希望帮助大家.