假定你已经集成了静态库,动态库,设置了bitcode,AppID绑定了推送,push Notifation 已经打开,接下来就是要实现聊天,视频,推送等等。
环信的demo一上来猛一看,很懵逼,没办法,一点点抠,把你需要的模块都抠出来,包括资源,图片,等等,[这里我只集成单聊,群组]。
1、把不用的全部删掉,(我主要把红包,聊天室,机器人相关的全部删除)。
还有记得 call 文件夹下的全部删除,废弃
2、pch 文件,已经国际化的配置,包括头文件的配置(我这里使用的全部的sdk 所以,DEMO_CALL 为 1)
3、最重要就是MainViewController的替换以及ChatDemoHelper的替换。
demo 里,根控制器是带NavBar 的TarbbarController,而TarbarController 又包含了三个子控制器。
针对项目结构自己做调整,头文件不用变,替换你需要的类
#import <UIKit/UIKit.h>
#import <UserNotifications/UserNotifications.h>
@interface LXTabbarController : UITabBarController
{
EMConnectionState _connectionState;
}
- (void)jumpToChatList;
- (void)setupUntreatedApplyCount;
- (void)setupUnreadMessageCount;
- (void)networkChanged:(EMConnectionState)connectionState;
- (void)didReceiveLocalNotification:(UILocalNotification *)notification;
- (void)didReceiveUserNotification:(UNNotification *)notification;
- (void)playSoundAndVibration;
- (void)showNotificationWithMessage:(EMMessage *)message;
@end
.m 里 关于接口的方法可以直接拿demo的方法使用,只是在配置子控制的地方自己设置。
#pragma mark - 添加所有的子控制器
- (void)addAllChildViewControllers {
//
//
_chatListVC= [[ConversationListController alloc] init];
[_chatListVC networkChanged:_connectionState];
[self addOneChildViewController:_chatListVC image:[UIImage imageNamed:@"tabbar_chats"] selectedImage:[UIImage imageNamed:@"tabbar_chatsHL"] title:@"会话"];
//
_contactsVC = [[ContactListViewController alloc] init];
[self addOneChildViewController:_contactsVC image:[UIImage imageNamed:@"tabbar_contacts"]
selectedImage:[UIImage imageNamed:@"tabbar_contactsHL"] title:@"通讯录"];
//
FourViewController *businessVc = [[FourViewController alloc] init];
[self addOneChildViewController:businessVc image:[UIImage imageNamed:@"tabbar_setting"] selectedImage:[UIImage imageNamed:@"tabbar_settingHL@2x"] title:@"设置"];
}
#pragma mark - 添加一个子控制器
- (void)addOneChildViewController:(UIViewController *)viewController image:(UIImage *)image selectedImage:(UIImage *)selectedImage title:(NSString *)title {
UINavigationController *navC = [[UINavigationController alloc] initWithRootViewController:viewController];
navC.navigationBar.barTintColor = [UIColor colorWithRed:0.47 green:0.83 blue:0.98 alpha:1];
NSMutableDictionary *attributes = [NSMutableDictionary dictionary];
attributes[NSForegroundColorAttributeName] = [UIColor colorWithWhite:0.996 alpha:1.000];
navC.navigationBar.titleTextAttributes = attributes;
navC.tabBarItem.title = title;
navC.tabBarItem.image = image;
navC.tabBarItem.titlePositionAdjustment = UIOffsetMake(0, -3);
navC.tabBarItem.selectedImage = selectedImage;
[self addChildViewController:navC];
}
如果你也是和我一样,使用Tabbar 配置Navbar的结构,有个地方需要注意:
4、最重要的是把ChatDemoHelper里修改一下
替换所有的MainViewController,设置MainController
#import "ChatDemoHelper.h"
#import "AppDelegate.h"
#import "ApplyViewController.h"
#import "MBProgressHUD.h"
#import "EaseSDKHelper.h"
#if DEMO_CALL == 1
#import "DemoCallManager.h"
#endif
static ChatDemoHelper *helper = nil;
@implementation ChatDemoHelper
+ (instancetype)shareHelper
{
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
helper = [[ChatDemoHelper alloc] init];
});
return helper;
}
- (void)dealloc
{
[[EMClient sharedClient] removeDelegate:self];
[[EMClient sharedClient].groupManager removeDelegate:self];
[[EMClient sharedClient].contactManager removeDelegate:self];
[[EMClient sharedClient].roomManager removeDelegate:self];
[[EMClient sharedClient].chatManager removeDelegate:self];
}
- (id)init
{
self = [super init];
if (self) {
[self initHelper];
}
return self;
}
#pragma mark - setter
- (void)setMainVC:(LXTabbarController *)mainVC
{
_mainVC = mainVC;
#if DEMO_CALL == 1
[[DemoCallManager sharedManager] setMainController:mainVC];
#endif
}
#pragma mark - init
- (void)initHelper
{
[[EMClient sharedClient] addDelegate:self delegateQueue:nil];
[[EMClient sharedClient].groupManager addDelegate:self delegateQueue:nil];
[[EMClient sharedClient].contactManager addDelegate:self delegateQueue:nil];
[[EMClient sharedClient].roomManager addDelegate:self delegateQueue:nil];
[[EMClient sharedClient].chatManager addDelegate:self delegateQueue:nil];
#if DEMO_CALL == 1
[DemoCallManager sharedManager];
#endif
}
剩下的 和Demo中直接复制过来。
最后说一下环信的推送:
环信的推送应用在后台是不会推送的,需要在接收消息的地方,判断当前应用的活跃状态,然后做本地推送。
- (void)didReceiveMessages:(NSArray *)aMessages
{
BOOL isRefreshCons = YES;
for(EMMessage *message in aMessages){
BOOL needShowNotification = (message.chatType != EMChatTypeChat) ? [self _needShowNotification:message.conversationId] : YES;
UIApplicationState state = [[UIApplication sharedApplication] applicationState];
if (needShowNotification) {
#if !TARGET_IPHONE_SIMULATOR
switch (state) {
case UIApplicationStateActive:
[self.mainVC playSoundAndVibration];
break;
case UIApplicationStateInactive:
[self.mainVC playSoundAndVibration];
break;
case UIApplicationStateBackground:
[self.mainVC showNotificationWithMessage:message];
break;
default:
break;
}
#endif
}
if (_chatVC == nil) {
_chatVC = [self _getCurrentChatView];
}
BOOL isChatting = NO;
if (_chatVC) {
isChatting = [message.conversationId isEqualToString:_chatVC.conversation.conversationId];
}
if (_chatVC == nil || !isChatting || state == UIApplicationStateBackground) {
[self _handleReceivedAtMessage:message];
if (self.conversationListVC) {
[_conversationListVC refresh];
}
if (self.mainVC) {
[_mainVC setupUnreadMessageCount];
}
return;
}
if (isChatting) {
isRefreshCons = NO;
}
}
if (isRefreshCons) {
if (self.conversationListVC) {
[_conversationListVC refresh];
}
if (self.mainVC) {
[_mainVC setupUnreadMessageCount];
}
}
}
好了,基本功能都实现了。