集成环信的即时通讯.docx

上传人:b****5 文档编号:7302374 上传时间:2023-01-22 格式:DOCX 页数:23 大小:22.72KB
下载 相关 举报
集成环信的即时通讯.docx_第1页
第1页 / 共23页
集成环信的即时通讯.docx_第2页
第2页 / 共23页
集成环信的即时通讯.docx_第3页
第3页 / 共23页
集成环信的即时通讯.docx_第4页
第4页 / 共23页
集成环信的即时通讯.docx_第5页
第5页 / 共23页
点击查看更多>>
下载资源
资源描述

集成环信的即时通讯.docx

《集成环信的即时通讯.docx》由会员分享,可在线阅读,更多相关《集成环信的即时通讯.docx(23页珍藏版)》请在冰豆网上搜索。

集成环信的即时通讯.docx

集成环信的即时通讯

集成环信的即时通讯

集成环信的即时通讯

字数4007

阅读4086

评论19

喜欢54

收藏文章分享

现在即时通讯比较火,很多公司会用到即时通讯,所以今天给大家讲讲即时通讯,因为没有服务器,所以使用了环信的SDK写了个即时通讯的demo,由于界面比较多,我不会像之前一样直接上代码,而是手把手一点点给大家说,用最简单的代码完成基本的即时通讯功能,尽量使大家看了这篇博客后都能写出一个简单的即时通讯的demo.github地址

上官网注册账号

首先来到环信的官网,然后登陆.没有账号先注册一个.

进去之后创建应用,如图创建应用界面

点击确定后,来到这个界面,只需要记住应用标示(APPKey)就行,待会儿会在代码里用到它.屏幕快照2016-01-04下午7.18.38.png

然后用cocoapods导入环信SDK,大家可以通过这篇博客来安装cocoapods.

创建项目

打开终端,输入cd,然后将项目入进去回车,就跳到项目地址,输入命令:

podinit,然后会生成一个Podfile,双击这个文件,将里面的东西全删了,然后输入:

pod'EaseMobSDK',然后在终端输入命令:

podinstall(如果不行可以试试:

podinstall--verbose--no-repo-update).接下来就等着SDK下载安装到项目里了,大概几分钟后就好了.这时候需要双击.xcworkspace的那个文件进去.SDK集成就完成了(不知道为什么官方文档里面写的集成特别复杂,需要导入各种框架,修改很多东西,其实只要终端一条指令就OK了).

AppDelegate

大家还记得刚才的APPKey吧,在AppDelegate里面需要注册使用.

#import"AppDelegate.h"

#import"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:

(UIApplication*)application{

[[EaseMobsharedInstance]applicationDidEnterBackground:

application];

}

-(void)applicationWillEnterForeground:

(UIApplication*)application{

[[EaseMobsharedInstance]applicationWillEnterForeground:

application];

}

-(void)applicationDidBecomeActive:

(UIApplication*)application{

//Restartanytasksthatwerepaused(ornotyetstarted)whiletheapplicationwasinactive.Iftheapplicationwaspreviouslyinthebackground,optionallyrefreshtheuserinterface.

}

-(void)applicationWillTerminate:

(UIApplication*)application{

[[EaseMobsharedInstance]applicationWillTerminate:

application];

}

@end

登陆界面登陆界面

看看这部分的代码吧.

#import"ViewController.h"

#import"RegisterViewController.h"

#import"FriendListViewController.h"

#import

@interfaceViewController()

@property(nonatomic,strong)UITextField*userNameTextField;//用户名

@property(nonatomic,strong)UITextField*passwordTextField;//密码

@property(nonatomic,strong)UIButton*loginButton;//登陆按钮

@property(nonatomic,strong)UIButton*registerButton;//注册按钮

@end

@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=@"请输入用户名";

[self.viewaddSubview:

_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:

25];

[self.viewaddSubview:

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;

[self.viewaddSubview:

_passwordTextField];

_loginButton=[UIButtonbuttonWithType:

UIButtonTypeSystem];

_loginButton.frame=CGRectMake(170,300,50,50);

_loginButton.titleLabel.font=[UIFontsystemFontOfSize:

25];

[_loginButtonsetTitle:

@"登陆"forState:

UIControlStateNormal];

[_loginButtonaddTarget:

selfaction:

@selector(didClickLoginButton)forControlEvents:

UIControlEventTouchUpInside];

[self.viewaddSubview:

_loginButton];

_registerButton=[UIButtonbuttonWithType:

UIButtonTypeSystem];

_registerButton.frame=CGRectMake(170,410,50,50);

_registerButton.titleLabel.font=[UIFontsystemFontOfSize:

25];

[_registerButtonsetTitle:

@"注册"forState:

UIControlStateNormal];

[_registerButtonaddTarget:

selfaction:

@selector(jumpToRegister)forControlEvents:

UIControlEventTouchUpInside];

[self.viewaddSubview:

_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.

}

@end

这个界面很简单,大部分都是界面搭建,当点击登陆时会调用

[[EaseMobsharedInstance].chatManagerasyncLoginWithUsername:

_userNameTextField.textpassword:

_passwordTextField.textcompletion:

^(NSDictionary*loginInfo,EMError*error)

这个方法有2个参数,一个用户名,一个是密码,很容易猜到这就是验证你的用户名和密码是否正确,如果正确就跳转到好友列表界面,如果不对就会在控制台打印相应的错误.

点击注册就跳转到注册界面.

注册界面注册界面

由于第一次登陆时没有账号,所以先得注册.先看看代码:

#import"RegisterViewController.h"

#import

@interfaceRegisterViewController()

@property(nonatomic,strong)UITextField*userNameTextField;//用户名

@property(nonatomic,strong)UITextField*passwordTextField;//密码

@property(nonatomic,strong)UIButton*registerButton;//注册按钮

@end

@implementationRegisterViewController

-(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=@"请输入用户名";

[self.viewaddSubview:

_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:

25];

[self.viewaddSubview:

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;

[self.viewaddSubview:

_passwordTextField];

_registerButton=[UIButtonbuttonWithType:

UIButtonTypeSystem];

_registerButton.frame=CGRectMake(170,330,50,50);

_registerButton.titleLabel.font=[UIFontsystemFontOfSize:

25];

[_registerButtonsetTitle:

@"注册"forState:

UIControlStateNormal];

[_registerButtonaddTarget:

selfaction:

@selector(didClickedRegisterButton:

)forControlEvents:

UIControlEventTouchUpInside];

[self.viewaddSubview:

_registerButton];

UIButton*backButton=[UIButtonbuttonWithType:

UIButtonTypeSystem];

backButton.frame=CGRectMake(170,280,50,50);

backButton.titleLabel.font=[UIFontsystemFontOfSize:

25];

[backButtonsetTitle:

@"返回"forState:

UIControlStateNormal];

[backButtonaddTarget:

selfaction:

@selector(backAction)forControlEvents:

UIControlEventTouchUpInside];

[self.viewaddSubview:

backButton];

}

-(void)backAction

{

[selfdismissViewControllerAnimated:

YEScompletion:

nil];

}

-(void)touchesBegan:

(NSSetUITouch*>*)toucheswithEvent:

(UIEvent*)event

{

//点击屏幕时让键盘回收

[_passwordTextFieldresignFirstResponder];

[_userNameTextFieldresignFirstResponder];

}

-(void)didClickedRegisterButton:

(id)sender

{

//登陆和注册有3种方法:

1.同步方法2.通过delegate回调的异步方法。

3.block异步方法

//其中官方推荐使用block异步方法,所以我这里就用block异步方法

//开始注册

[[EaseMobsharedInstance].chatManagerasyncRegisterNewAccount:

_userNameTextField.textpassword:

_passwordTextField.textwithCompletion:

^(NSString*username,NSString*password,EMError*error){

if(!

error){

NSLog(@"注册成功");

[selfdismissViewControllerAnimated:

YEScompletion:

nil];

}

else

{

NSLog(@"%@",error);

}

}onQueue:

dispatch_get_main_queue()];

}

-(void)didReceiveMemoryWarning{

[superdidReceiveMemoryWarning];

//Disposeofanyresourcesthatcanberecreated.

}

/*

#pragmamark-Navigation

//Inastoryboard-basedapplication,youwilloftenwanttodoalittlepreparationbeforenavigation

-(void)prepareForSegue:

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 高等教育 > 理学

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1