iOS开发:环信实现即时通讯
2016-05-16 15:15
363 查看
环信主要有两方面的业务:即时通讯和客服
环信只负责注册用户和聊天服务,不负责添加、删除好友等业务。也就是说环信不管好友关系的业务逻辑,只要注册进来的用户都可以聊天。
要想实现好友关系的业务逻辑需要重新开个服务器专门管理好有关系。让公司的服务端开发此功能并提供相应的接口。
第一步:登录环信官网注册账号下载sdk
第二步:创建应用获取Appkey;
第三步:xcode环境配置
将环信sdk拖入工程:EasemMobSDK
libEaseMobClientSDK完整版 登录注册聊天 视频聊天
libEaseMobClientSDKLite简版 登录注册聊天
完整版与简版在工程中只能二选一添加一个
添加系统依赖库
![](http://img.blog.csdn.net/20160516152656501?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)
other linker flags配置
![](http://img.blog.csdn.net/20160516152853872?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)
第四步:实现注册登录功能
1.初始化Sdk
2.用户注册
3.用户登录
4.用户自动登录
5.退出登录
第五步:实现聊天功能
1.发送消息前要先遵守协议设置回调代理
2.发送消息
3.接收消息
环信只负责注册用户和聊天服务,不负责添加、删除好友等业务。也就是说环信不管好友关系的业务逻辑,只要注册进来的用户都可以聊天。
要想实现好友关系的业务逻辑需要重新开个服务器专门管理好有关系。让公司的服务端开发此功能并提供相应的接口。
第一步:登录环信官网注册账号下载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; }
相关文章推荐
- IOS5中的addChildViewController
- 移动应用崩溃收集
- IOS开发—通过ChildViewController实现view的切换
- iOS - 正则表达式
- iOS开发-常用第三方开源框架介绍(你了解的ios只是冰山一角)
- 使用nagios检测windows服务器
- iOS文件管理(二)
- iOS开发导航条覆盖View的问题
- 《Motion Design for iOS》(十八)
- iOS核心动画-转场动画CATransition和组动画CAAnimationGroup
- iOS文字渐变效果实现
- iOS 9适配技巧
- iOS 二维码扫描
- iOS常见警告和错误集锦
- [绍棠] iOS开发:获取设备IP地址
- ios 开源组列表
- iOS 25个性能优化/内存优化常用方法
- iOS算法(一)快速排序算法
- iOS 隐藏键盘
- IOS 数据解析