1、集成环信的即时通讯集成环信的即时通讯 集成环信的即时通讯字数4007阅读4086评论19喜欢54 收藏文章 分享 现在即时通讯比较火,很多公司会用到即时通讯,所以今天给大家讲讲即时通讯,因为没有服务器,所以使用了环信的SDK写了个即时通讯的demo,由于界面比较多,我不会像之前一样直接上代码,而是手把手一点点给大家说,用最简单的代码完成基本的即时通讯功能,尽量使大家看了这篇博客后都能写出一个简单的即时通讯的demo.github地址上官网注册账号首先来到环信的官网,然后登陆.没有账号先注册一个.进去之后创建应用,如图创建应用界面点击确定后,来到这个界面,只需要记住应用标示(APPKey)就行,
2、待会儿会在代码里用到它.屏幕快照 2016-01-04 下午7.18.38.png然后用cocoapods导入环信SDK,大家可以通过这篇博客来安装cocoapods.创建项目打开终端,输入cd,然后将项目入进去回车,就跳到项目地址,输入命令:pod init,然后会生成一个Podfile,双击这个文件,将里面的东西全删了,然后输入:pod EaseMobSDK,然后在终端输入命令:pod install(如果不行可以试试:pod install -verbose -no-repo-update).接下来就等着SDK下载安装到项目里了,大概几分钟后就好了.这时候需要双击.xcworkspace
3、的那个文件进去.SDK集成就完成了(不知道为什么官方文档里面写的集成特别复杂,需要导入各种框架,修改很多东西,其实只要终端一条指令就OK了).AppDelegate大家还记得刚才的APPKey吧,在AppDelegate里面需要注册使用.#import AppDelegate.h#import ViewController.h#import interface AppDelegate ()endimplementation AppDelegate- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOpti
4、ons:(NSDictionary *)launchOptions _window = UIWindow allocinitWithFrame:UIScreen mainScreen.bounds; _window.backgroundColor = UIColor whiteColor; _window makeKeyAndVisible; UINavigationController *nav = UINavigationController allocinitWithRootViewController:ViewController allocinit; _window.rootView
5、Controller = nav; /注册环信 EaseMob sharedInstanceregisterSDKWithAppKey:xmh123#cdxmh apnsCertName:; return YES;- (void)applicationWillResignActive:(UIApplication *)application / Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interr
6、uptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state. / Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.- (vo
7、id)applicationDidEnterBackground:(UIApplication *)application EaseMob sharedInstance applicationDidEnterBackground:application;- (void)applicationWillEnterForeground:(UIApplication *)application EaseMob sharedInstance applicationWillEnterForeground:application;- (void)applicationDidBecomeActive:(UIA
8、pplication *)application / Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.- (void)applicationWillTerminate:(UIApplication *)application EaseMob sharedInstanceapplic
9、ationWillTerminate:application;end登陆界面登陆界面看看这部分的代码吧.#import ViewController.h#import RegisterViewController.h#import FriendListViewController.h#import interface ViewController ()property(nonatomic, strong)UITextField *userNameTextField;/用户名property(nonatomic, strong)UITextField *passwordTextField;/密码
10、property(nonatomic, strong)UIButton *loginButton; /登陆按钮property(nonatomic, strong)UIButton *registerButton; /注册按钮endimplementation ViewController- (void)viewDidLoad super viewDidLoad; self.view.backgroundColor = UIColor whiteColor; self.navigationController.navigationBar.translucent = NO; self.title
11、 = 登陆界面; UILabel *usernameLabel = UILabel allocinitWithFrame:CGRectMake(20, 100, 80, 50); usernameLabel.text = 用户名; usernameLabel.font = UIFont systemFontOfSize:25; self.view addSubview:usernameLabel; _userNameTextField = UITextField allocinitWithFrame:CGRectMake(usernameLabel.frame.origin.x + usern
12、ameLabel.frame.size.width + 10, usernameLabel.frame.origin.y, 250, 50); _userNameTextField.borderStyle = 3; _userNameTextField.placeholder = 请输入用户名; self.view addSubview:_userNameTextField; UILabel *passwordLabel = UILabel allocinitWithFrame:CGRectMake(usernameLabel.frame.origin.x, usernameLabel.fra
13、me.origin.y + usernameLabel.frame.size.height + 10, usernameLabel.frame.size.width, usernameLabel.frame.size.height); passwordLabel.text = 密码; passwordLabel.font = UIFont systemFontOfSize:25; self.view addSubview:passwordLabel; _passwordTextField = UITextField allocinitWithFrame:CGRectMake(_userName
14、TextField.frame.origin.x, passwordLabel.frame.origin.y, _userNameTextField.frame.size.width, _userNameTextField.frame.size.height); _passwordTextField.placeholder = 请输入密码; _passwordTextField.borderStyle = 3; self.view addSubview:_passwordTextField; _loginButton = UIButton buttonWithType:UIButtonType
15、System; _loginButton.frame = CGRectMake(170, 300, 50, 50); _loginButton.titleLabel.font = UIFont systemFontOfSize:25; _loginButton setTitle:登陆 forState:UIControlStateNormal; _loginButton addTarget:self action:selector(didClickLoginButton) forControlEvents:UIControlEventTouchUpInside; self.view addSu
16、bview:_loginButton; _registerButton = UIButton buttonWithType:UIButtonTypeSystem; _registerButton.frame = CGRectMake(170, 410, 50, 50); _registerButton.titleLabel.font = UIFont systemFontOfSize:25; _registerButton setTitle:注册 forState:UIControlStateNormal; _registerButton addTarget:self action:selec
17、tor(jumpToRegister) forControlEvents:UIControlEventTouchUpInside; self.view addSubview:_registerButton;-(void)didClickLoginButton EaseMob sharedInstance.chatManager asyncLoginWithUsername:_userNameTextField.text password:_passwordTextField.text completion:(NSDictionary *loginInfo, EMError *error) if
18、 (!error) /如果验证用户名和密码没有问题就跳转到好友列表界面 self.navigationController pushViewController:FriendListViewController allocinit animated:YES; else / 显示错误信息的警告 NSLog(%,error); onQueue:dispatch_get_main_queue();-(void)jumpToRegister/跳转到注册界面 RegisterViewController *registerVC = RegisterViewController allocinit; se
19、lf.navigationController presentViewController:registerVC animated:YES completion:nil;- (void)didReceiveMemoryWarning super didReceiveMemoryWarning; / Dispose of any resources that can be recreated.end这个界面很简单,大部分都是界面搭建,当点击登陆时会调用EaseMob sharedInstance.chatManager asyncLoginWithUsername:_userNameTextFi
20、eld.text password:_passwordTextField.text completion:(NSDictionary *loginInfo, EMError *error)这个方法有2个参数,一个用户名,一个是密码,很容易猜到这就是验证你的用户名和密码是否正确,如果正确就跳转到好友列表界面,如果不对就会在控制台打印相应的错误.点击注册就跳转到注册界面.注册界面注册界面由于第一次登陆时没有账号,所以先得注册.先看看代码:#import RegisterViewController.h#import interface RegisterViewController ()proper
21、ty(nonatomic, strong)UITextField *userNameTextField;/用户名property(nonatomic, strong)UITextField *passwordTextField;/密码property(nonatomic, strong)UIButton *registerButton; /注册按钮endimplementation RegisterViewController- (void)viewDidLoad super viewDidLoad; self.view.backgroundColor = UIColor whiteColor
22、; self.navigationController.navigationBar.translucent = NO; self.title = 登陆界面; UILabel *usernameLabel = UILabel allocinitWithFrame:CGRectMake(20, 100, 80, 50); usernameLabel.text = 用户名; usernameLabel.font = UIFont systemFontOfSize:25; self.view addSubview:usernameLabel; _userNameTextField = UITextFi
23、eld allocinitWithFrame:CGRectMake(usernameLabel.frame.origin.x + usernameLabel.frame.size.width + 10, usernameLabel.frame.origin.y, 250, 50); _userNameTextField.borderStyle = 3; _userNameTextField.placeholder = 请输入用户名; self.view addSubview:_userNameTextField; UILabel *passwordLabel = UILabel allocin
24、itWithFrame:CGRectMake(usernameLabel.frame.origin.x, usernameLabel.frame.origin.y + usernameLabel.frame.size.height + 10, usernameLabel.frame.size.width, usernameLabel.frame.size.height); passwordLabel.text = 密码; passwordLabel.font = UIFont systemFontOfSize:25; self.view addSubview:passwordLabel; _p
25、asswordTextField = UITextField allocinitWithFrame:CGRectMake(_userNameTextField.frame.origin.x, passwordLabel.frame.origin.y, _userNameTextField.frame.size.width, _userNameTextField.frame.size.height); _passwordTextField.placeholder = 请输入密码; _passwordTextField.borderStyle = 3; self.view addSubview:_
26、passwordTextField; _registerButton = UIButton buttonWithType:UIButtonTypeSystem; _registerButton.frame = CGRectMake(170, 330, 50, 50); _registerButton.titleLabel.font = UIFont systemFontOfSize:25; _registerButton setTitle:注册 forState:UIControlStateNormal; _registerButton addTarget:self action:select
27、or(didClickedRegisterButton:) forControlEvents:UIControlEventTouchUpInside; self.view addSubview:_registerButton; UIButton *backButton = UIButton buttonWithType:UIButtonTypeSystem; backButton.frame = CGRectMake(170, 280, 50, 50); backButton.titleLabel.font = UIFont systemFontOfSize:25; backButton se
28、tTitle:返回 forState:UIControlStateNormal; backButton addTarget:self action:selector(backAction) forControlEvents:UIControlEventTouchUpInside; self.view addSubview:backButton;-(void)backAction self dismissViewControllerAnimated:YES completion:nil;-(void)touchesBegan:(NSSetUITouch * *)touches withEvent
29、:(UIEvent *)event/点击屏幕时让键盘回收 _passwordTextField resignFirstResponder; _userNameTextField resignFirstResponder;-(void)didClickedRegisterButton:(id)sender/ 登陆和注册有3种方法: 1. 同步方法 2. 通过delegate回调的异步方法。3.block异步方法 /其中官方推荐使用block异步方法,所以我这里就用block异步方法 /开始注册 EaseMob sharedInstance.chatManager asyncRegisterNew
30、Account:_userNameTextField.text password:_passwordTextField.text withCompletion:(NSString *username, NSString *password, EMError *error) if (!error) NSLog(注册成功); self dismissViewControllerAnimated:YES completion:nil; else NSLog(%,error); onQueue:dispatch_get_main_queue();- (void)didReceiveMemoryWarning super didReceiveMemoryWarning; / Dispose of any resources that can be recreated./*#pragma mark - Navigation/ In a storyboard-based application, you will often want to do a little preparation before navigation- (void)prepareForSegue:(
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1