1、准备工作,配置证书,绑定APPID,。注意开发证书和生产证书。创建应用,上传p12 文件(证书)到环信后台,环信会返回一个应用标识就是APPKEY,以及你的推送证书的名字。
2、下载官方SDK,
3、新建个工程,把上面的 HyphenateFullSDK拖到工程里,开始配置。
- 设置bitcode 为 No;
- 创建pch ,
#ifdef __OBJC__
#import <Foundation/Foundation.h>
#import <Hyphenate/Hyphenate.h>
#define HUANXINAPPKEY @"1198170313178186#lxchatdemo" //应用标识
#define APNSNAME @"LXChatDemo" //证明名字
#endif
配置依赖的静态库:
CoreMedia.framework
AudioToolbox.framework
AVFoundation.framework
MobileCoreServices.framework
ImageIO.framework
libc++.dylib
libz.dylib
libstdc++.6.0.9.dylib
libsqlite3.dylib
libiconv.dylib
如果编译出现这样的问题:
dyld: Library not loaded: @rpath/Hyphenate.framework/Hyphenate
Referenced from: /Users/liangyi/Library/Developer/CoreSimulator/Devices/F3F0CB24-F891-489D-B71F-1AA6DA17832C/data/Containers/Bundle/Application/8D4D4F2E-A19F-4590-BE80-41403C735448/EMDemo.app/EMDemo
Reason: image not found
//字面意思是嵌入二进制文件
最后编译command + B 通过即可。
4、根据官方视频简单实现单聊:
EMOptions
是SDK的设置选项
EMClient
是用户选项,大概看一下头文件,基本上包含了单聊,群聊,聊天室:
@property (nonatomic, strong, readonly) id<IEMChatManager> chatManager;
/*!
* \~chinese
* 好友模块
*
* \~english
* Contact Management
*/
@property (nonatomic, strong, readonly) id<IEMContactManager> contactManager;
/*!
* \~chinese
* 群组模块
*
* \~english
* Group Management
*/
@property (nonatomic, strong, readonly) id<IEMGroupManager> groupManager;
/*!
* \~chinese
* 聊天室模块
*
* \~english
* Chat room Management
*/
@property (nonatomic, strong, readonly) id<IEMChatroomManager> roomManager;
直接按照视频实现下
#import "AppDelegate.h"
#import "ChatViewController.h"
@interface AppDelegate ()
@end
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
EMOptions *option =[EMOptions optionsWithAppkey:HUANXINAPPKEY];
option.apnsCertName = APNSNAME;
EMError *error =[[EMClient sharedClient]initializeSDKWithOptions:option];
if (!error) {
NSLog(@"初始化成功");
}
error = [[EMClient sharedClient] registerWithUsername:@"111" password:@"111"];
if (!error) {
NSLog(@"注册成功");
}else{
NSLog(@"%@",error.errorDescription);
}
error = [[EMClient sharedClient] registerWithUsername:@"222" password:@"222"];
if (!error) {
NSLog(@"注册成功");
}else{
NSLog(@"%@",error.errorDescription);
}
error =[[EMClient sharedClient]loginWithUsername:@"111" password:@"111"];
if (!error) {
NSLog(@"登录成功");
}else{
NSLog(@"%@",error.errorDescription);
}
// error =[[EMClient sharedClient]loginWithUsername:@"222" password:@"222"];
// if (!error) {
// NSLog(@"登录成功");
// }else{
// NSLog(@"%@",error.errorDescription);
// }
self.window =[[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds];
ChatViewController *vc =[[ChatViewController alloc]initWithConversationId:@"222" conversationType:EMConversationTypeChat];
UINavigationController *nav =[[UINavigationController alloc]initWithRootViewController:vc];
self.window.rootViewController = nav;
[self.window makeKeyAndVisible];
return YES;
}
//接受消息 必须ChatManager 的代理
@interface ChatViewController ()<EMChatManagerDelegate>
@property (weak, nonatomic) IBOutlet UITextField *input;
@property(nonatomic,strong)EMConversation *conversation;
@property (weak, nonatomic) IBOutlet UILabel *receiveLabel;
@end
@implementation ChatViewController
-(id)initWithConversationId:(NSString *)conversation conversationType:(EMConversationType)conversationType{
self = [super initWithNibName:@"ChatViewController" bundle:nil];
if (self) {
self.conversation = [[EMClient sharedClient].chatManager getConversation:conversation type:conversationType createIfNotExist:YES];
[[EMClient sharedClient].chatManager addDelegate:self delegateQueue:nil];
}
return self;
}
-(void)dealloc{
[[EMClient sharedClient] removeDelegate:self];
}
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
}
#pragma mark --接受消息---
-(void)messagesDidReceive:(NSArray *)aMessages{
EMMessage *message = aMessages[0];
EMTextMessageBody *body = (EMTextMessageBody *)message.body;
self.receiveLabel.text = body.text;
}
#pragma mark ---发送消息---
- (IBAction)sendAction:(id)sender {
EMTextMessageBody *body =[[EMTextMessageBody alloc]initWithText:self.input.text];
EMMessage *message =[[EMMessage alloc]initWithConversationID:self.conversation.conversationId from:[EMClient sharedClient].currentUsername to:self.conversation.conversationId body:body ext:nil];
[[EMClient sharedClient].chatManager sendMessage:message progress:^(int progress) {
} completion:^(EMMessage *message, EMError *error) {
if (!error) {
NSLog(@"发送成功");
}else{
NSLog(@"%@",error.errorDescription);
}
}];
}
最后别忘了:
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key>
<true/>
</dict>
简单单聊集成就是这样了。至于消息的存储,排序,提示,还需要借助数据库,推送进行处理,当然也可以使用环信的数据库。
最简单的集成单聊页面,就是把EaseUI 拖到项目里,然后定义一个UIViewController继承与EaseMessageViewController即可:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
EMOptions *option =[EMOptions optionsWithAppkey:HUANXINAPPKEY];
option.apnsCertName = APNSNAME;
EMError *error =[[EMClient sharedClient]initializeSDKWithOptions:option];
if (!error) {
NSLog(@"初始化成功");
}
error =[[EMClient sharedClient]loginWithUsername:@"111" password:@"111"];
if (!error) {
NSLog(@"登录成功");
}else{
NSLog(@"%@",error.errorDescription);
}
self.window =[[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds];
ChatViewController *vc =[[ChatViewController alloc]initWithConversationChatter:@"222" conversationType:EMConversationTypeChat];
UINavigationController *nav =[[UINavigationController alloc]initWithRootViewController:vc];
self.window.rootViewController = nav;
[self.window makeKeyAndVisible];
return YES;
}
然后就可以实现简单的单聊了,环信官方建议好友体系由自己管理,当然开发环境环信也提供了好友体系。