集成环信的即时通讯Word文档格式.docx
《集成环信的即时通讯Word文档格式.docx》由会员分享,可在线阅读,更多相关《集成环信的即时通讯Word文档格式.docx(23页珍藏版)》请在冰豆网上搜索。
podinstall--verbose--no-repo-update).接下来就等着SDK下载安装到项目里了,大概几分钟后就好了.这时候需要双击.xcworkspace的那个文件进去.SDK集成就完成了(不知道为什么官方文档里面写的集成特别复杂,需要导入各种框架,修改很多东西,其实只要终端一条指令就OK了).
AppDelegate
大家还记得刚才的APPKey吧,在AppDelegate里面需要注册使用.
#import"
AppDelegate.h"
ViewController.h"
#import
@interfaceAppDelegate()
@end
@implementationAppDelegate
-(BOOL)application:
(UIApplication*)applicationdidFinishLaunchingWithOptions:
(NSDictionary*)launchOptions{
_window=[[UIWindowalloc]initWithFrame:
[UIScreenmainScreen].bounds];
_window.backgroundColor=[UIColorwhiteColor];
[_windowmakeKeyAndVisible];
UINavigationController*nav=[[UINavigationControlleralloc]initWithRootViewController:
[[ViewControlleralloc]init]];
_window.rootViewController=nav;
//注册环信
[[EaseMobsharedInstance]registerSDKWithAppKey:
@"
xmh123#cdxmh"
apnsCertName:
"
];
returnYES;
}
-(void)applicationWillResignActive:
(UIApplication*)application{
//Sentwhentheapplicationisabouttomovefromactivetoinactivestate.Thiscanoccurforcertaintypesoftemporaryinterruptions(suchasanincomingphonecallorSMSmessage)orwhentheuserquitstheapplicationanditbeginsthetransitiontothebackgroundstate.
//Usethismethodtopauseongoingtasks,disabletimers,andthrottledownOpenGLESframerates.Gamesshouldusethismethodtopausethegame.
-(void)applicationDidEnterBackground:
[[EaseMobsharedInstance]applicationDidEnterBackground:
application];
-(void)applicationWillEnterForeground:
[[EaseMobsharedInstance]applicationWillEnterForeground:
-(void)applicationDidBecomeActive:
//Restartanytasksthatwerepaused(ornotyetstarted)whiletheapplicationwasinactive.Iftheapplicationwaspreviouslyinthebackground,optionallyrefreshtheuserinterface.
-(void)applicationWillTerminate:
[[EaseMobsharedInstance]applicationWillTerminate:
登陆界面登陆界面
看看这部分的代码吧.
RegisterViewController.h"
FriendListViewController.h"
@interfaceViewController()
@property(nonatomic,strong)UITextField*userNameTextField;
//用户名
@property(nonatomic,strong)UITextField*passwordTextField;
//密码
@property(nonatomic,strong)UIButton*loginButton;
//登陆按钮
@property(nonatomic,strong)UIButton*registerButton;
//注册按钮
@implementationViewController
-(void)viewDidLoad{
[superviewDidLoad];
self.view.backgroundColor=[UIColorwhiteColor];
self.navigationController.navigationBar.translucent=NO;
self.title=@"
登陆界面"
;
UILabel*usernameLabel=[[UILabelalloc]initWithFrame:
CGRectMake(20,100,80,50)];
usernameLabel.text=@"
用户名"
usernameLabel.font=[UIFontsystemFontOfSize:
25];
[self.viewaddSubview:
usernameLabel];
_userNameTextField=[[UITextFieldalloc]initWithFrame:
CGRectMake(usernameLabel.frame.origin.x+usernameLabel.frame.size.width+10,usernameLabel.frame.origin.y,250,50)];
_userNameTextField.borderStyle=3;
_userNameTextField.placeholder=@"
请输入用户名"
_userNameTextField];
UILabel*passwordLabel=[[UILabelalloc]initWithFrame:
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=[UIFontsystemFontOfSize:
passwordLabel];
_passwordTextField=[[UITextFieldalloc]initWithFrame:
CGRectMake(_userNameTextField.frame.origin.x,passwordLabel.frame.origin.y,_userNameTextField.frame.size.width,_userNameTextField.frame.size.height)];
_passwordTextField.placeholder=@"
请输入密码"
_passwordTextField.borderStyle=3;
_passwordTextField];
_loginButton=[UIButtonbuttonWithType:
UIButtonTypeSystem];
_loginButton.frame=CGRectMake(170,300,50,50);
_loginButton.titleLabel.font=[UIFontsystemFontOfSize:
[_loginButtonsetTitle:
登陆"
forState:
UIControlStateNormal];
[_loginButtonaddTarget:
selfaction:
@selector(didClickLoginButton)forControlEvents:
UIControlEventTouchUpInside];
_loginButton];
_registerButton=[UIButtonbuttonWithType:
_registerButton.frame=CGRectMake(170,410,50,50);
_registerButton.titleLabel.font=[UIFontsystemFontOfSize:
[_registerButtonsetTitle:
注册"
[_registerButtonaddTarget:
@selector(jumpToRegister)forControlEvents:
_registerButton];
-(void)didClickLoginButton
{
[[EaseMobsharedInstance].chatManagerasyncLoginWithUsername:
_userNameTextField.textpassword:
_passwordTextField.textcompletion:
^(NSDictionary*loginInfo,EMError*error){
if(!
error){
//如果验证用户名和密码没有问题就跳转到好友列表界面
[self.navigationControllerpushViewController:
[[FriendListViewControlleralloc]init]animated:
YES];
}else{
//显示错误信息的警告
NSLog(@"
%@"
error);
}
}onQueue:
dispatch_get_main_queue()];
-(void)jumpToRegister
//跳转到注册界面
RegisterViewController*registerVC=[[RegisterViewControlleralloc]init];
[self.navigationControllerpresentViewController:
registerVCanimated:
YEScompletion:
nil];
-(void)didReceiveMemoryWarning{
[superdidReceiveMemoryWarning];
//Disposeofanyresourcesthatcanberecreated.
这个界面很简单,大部分都是界面搭建,当点击登陆时会调用
[[EaseMobsharedInstance].chatManagerasyncLoginWithUsername:
^(NSDictionary*loginInfo,EMError*error)
这个方法有2个参数,一个用户名,一个是密码,很容易猜到这就是验证你的用户名和密码是否正确,如果正确就跳转到好友列表界面,如果不对就会在控制台打印相应的错误.
点击注册就跳转到注册界面.
注册界面注册界面
由于第一次登陆时没有账号,所以先得注册.先看看代码:
@interfaceRegisterViewController()
@implementationRegisterViewController
_registerButton.frame=CGRectMake(170,330,50,50);
@selector(didClickedRegisterButton:
)forControlEvents:
UIButton*backButton=[UIButtonbuttonWithType:
backButton.frame=CGRectMake(170,280,50,50);
backButton.titleLabel.font=[UIFontsystemFontOfSize:
[backButtonsetTitle:
返回"
[backButtonaddTarget:
@selector(backAction)forControlEvents:
backButton];
-(void)backAction
[selfdismissViewControllerAnimated:
-(void)touchesBegan:
(NSSetUITouch*>
*)toucheswithEvent:
(UIEvent*)event
//点击屏幕时让键盘回收
[_passwordTextFieldresignFirstResponder];
[_userNameTextFieldresignFirstResponder];
-(void)didClickedRegisterButton:
(id)sender
//登陆和注册有3种方法:
1.同步方法2.通过delegate回调的异步方法。
3.block异步方法
//其中官方推荐使用block异步方法,所以我这里就用block异步方法
//开始注册
[[EaseMobsharedInstance].chatManagerasyncRegisterNewAccount:
_passwordTextField.textwithCompletion:
^(NSString*username,NSString*password,EMError*error){
注册成功"
);
else
{
/*
#pragmamark-Navigation
//Inastoryboard-basedapplication,youwilloftenwanttodoalittlepreparationbeforenavigation
-(void)prepareForSegue:
(