环信主要有两方面的业务:即时通讯和客服
环信只负责注册用户和聊天服务,不负责添加、删除好友等业务。也就是说环信不管好友关系的业务逻辑,只要注册进来的用户都可以聊天。
要想实现好友关系的业务逻辑需要重新开个服务器专门管理好有关系。让公司的服务端开发此功能并提供相应的接口。
第一步:登录环信官网注册账号下载sdk
第二步:创建应用获取Appkey;
第三步:xcode环境配置
将环信sdk拖入工程:EasemMobSDK
libEaseMobClientSDK完整版 登录注册聊天 视频聊天
libEaseMobClientSDKLite简版 登录注册聊天
完整版与简版在工程中只能二选一添加一个
添加系统依赖库
other linker flags配置
第四步:实现注册登录功能
1.初始化Sdk
#import "EaseMob.h"
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
//通过appkey连接到环信服务器中的对应的app
//注册appkey 初始化skd
[[EaseMob sharedInstance] registerSDKWithAppKey:@"jianghuhike#lxy" apnsCertName:nil];
[[EaseMob sharedInstance] application:application didFinishLaunchingWithOptions:launchOptions];
return YES;
}
- (void)applicationWillResignActive:(UIApplication *)application {
}
- (void)applicationDidEnterBackground:(UIApplication *)application {
//解决进入后台
[[EaseMob sharedInstance] applicationDidEnterBackground:application];
}
- (void)applicationWillEnterForeground:(UIApplication *)application {
}
- (void)applicationDidBecomeActive:(UIApplication *)application {
//解决进入前台
[[EaseMob sharedInstance] applicationDidBecomeActive:application];
}
- (void)applicationWillTerminate:(UIApplication *)application {
//解决退出
[[EaseMob sharedInstance] applicationWillTerminate:application];
}
2.用户注册
- (IBAction)regBtn:(id)sender {
[[EaseMob sharedInstance].chatManager asyncRegisterNewAccount:self.nameText.text password:self.pwdText.text withCompletion:^(NSString *username, NSString *password, EMError *error) {
if (!error) {
NSLog(@"注册成功");
}
else{
NSLog(@"error信息 = %@",error);
}
} onQueue:nil];
}
3.用户登录
- (IBAction)loginBtn:(id)sender {
[[EaseMob sharedInstance].chatManager asyncLoginWithUsername:self.nameText.text password:self.pwdText.text completion:^(NSDictionary *loginInfo, EMError *error) {
if (!error&&loginInfo) {
NSLog(@"登录成功");
//登录成功跳转到聊天页面
ChatViewController * chatViewController = [[ChatViewController alloc] init];
//chatViewController.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
[self presentViewController:chatViewController animated:YES completion:^{
//设置保存登录状态
[[EaseMob sharedInstance].chatManager setIsAutoLoginEnabled:YES];
}];
}
else{
NSLog(@"登录失败信息:%@",error);
}
} onQueue:nil];
}
4.用户自动登录
-(void)viewDidAppear:(BOOL)animated{
BOOL isLogin = [[EaseMob sharedInstance].chatManager isAutoLoginEnabled];
//进入该页面时判断自动登录是否设置
if (isLogin) {
ChatViewController * chatViewController = [[ChatViewController alloc] init];
[self presentViewController:chatViewController animated:YES completion:nil];
}
}
5.退出登录
//退出登录
- (IBAction)logoutBtn:(id)sender {
[[EaseMob sharedInstance].chatManager asyncLogoffWithUnbindDeviceToken:YES completion:^(NSDictionary *info, EMError *error) {
if (!error) {
NSLog(@"退出成功");
[self dismissViewControllerAnimated:YES completion:nil];
}
else{
NSLog(@"退出失败信息:%@",error);
}
} onQueue:nil];
}
第五步:实现聊天功能
1.发送消息前要先遵守协议设置回调代理
//需要遵守协议
<IChatManagerDelegate>
- (void)viewDidLoad {
[super viewDidLoad];
//设置回调代理
[[EaseMob sharedInstance].chatManager addDelegate:self delegateQueue:dispatch_get_main_queue()];
//设置一个用来拼接的字符串
_mString = [[NSMutableString alloc] init];
}
2.发送消息
//发送聊天信息
- (IBAction)sendBtn:(id)sender {
EMMessage * message = [self emMessageFromString:self.sendTxt.text];
[[EaseMob sharedInstance].chatManager sendMessage:message progress:nil error:nil];
[_mString appendFormat:@"我:%@\n",self.sendTxt.text];
self.msgTxt.text = _mString;
}
//将string转成可发送的消息实例
-(EMMessage *)emMessageFromString:(NSString *)string{
EMChatText * text = [[EMChatText alloc] initWithText:string];
EMTextMessageBody * body = [[EMTextMessageBody alloc] initWithChatObject:text];
EMMessage * message = [[EMMessage alloc] initWithReceiver:@"aaa" bodies:@[body]];
return message;
}
//发送完消息后的回调
-(void)didSendMessage:(EMMessage *)message error:(EMError *)error{
NSLog(@"发送消息完成");
}
3.接收消息
//当收到消息时回调
-(void)didReceiveMessage:(EMMessage *)message{
id<IEMMessageBody> obj = message.messageBodies.firstObject;
NSString * reciveMessage = ((EMTextMessageBody * )obj).text;
NSLog(@"收到的消息:%@",reciveMessage);
[_mString appendFormat:@"他:%@\n",reciveMessage];
self.msgTxt.text = _mString;
}