- 主要核心代码
EMError *error = [[EMClient sharedClient]registerWithUsername:self.textfield_1.text password:self.textfield_2.text];
- 简单图形

屏幕快照 2016-05-06 下午9.38.53.png
- 简单构建注册页面
@interface RegisterViewController ()
//声明属性
@property (nonatomic, strong) UILabel *label_1;//登录
@property (nonatomic, strong) UILabel *label_2;//密码
@property (nonatomic, strong) UILabel *label_3;//密码
@property (nonatomic, strong) UITextField *textfield_1;//用户输入
@property (nonatomic, strong) UITextField *textfield_2;//密码1
@property (nonatomic, strong) UITextField *textfield_3;//确认密码
@property (nonatomic, strong) UIActivityIndicatorView *activityIndicatorView;//风火轮
@end
#pragma mark-------label\textfield的懒加载
- (UILabel *)label_1{
if (!_label_1) {
_label_1 = [[UILabel alloc] initWithFrame:CGRectMake(50, 200, 100, 40)];
_label_1.text = @"用户名";
_label_1.backgroundColor = [UIColor cyanColor];
}
return _label_1;
}
- (UILabel *)label_2{
if (!_label_2) {
_label_2 = [[UILabel alloc] initWithFrame:CGRectMake(50, 260, 100, 40)];
_label_2.text = @"密码";
_label_2.backgroundColor = [UIColor cyanColor];
}
return _label_2;
}
- (UILabel *)label_3{
if (!_label_3) {
_label_3 = [[UILabel alloc] initWithFrame:CGRectMake(50, 320, 100, 40)];
_label_3.text = @"确认密码";
_label_3.backgroundColor = [UIColor cyanColor];
}
return _label_3;
}
- (UITextField *)textfield_1{
if (!_textfield_1) {
_textfield_1 = [[UITextField alloc] initWithFrame:CGRectMake(170, 200, 150, 40)];
_textfield_1.placeholder = @"请输入用户名";
_textfield_1.backgroundColor = [UIColor cyanColor];
}
return _textfield_1;
}
- (UITextField *)textfield_2{
if (!_textfield_2) {
_textfield_2 = [[UITextField alloc] initWithFrame:CGRectMake(170, 260, 150, 40)];
_textfield_2.placeholder = @"请输入密码";
_textfield_2.backgroundColor = [UIColor cyanColor];
}
return _textfield_2;
}
- (UITextField *)textfield_3{
if (!_textfield_3) {
_textfield_3 = [[UITextField alloc] initWithFrame:CGRectMake(170, 320, 150, 40)];
_textfield_3.placeholder = @"请确认密码";
_textfield_3.backgroundColor = [UIColor cyanColor];
}
return _textfield_3;
}
- (void)viewDidLoad {
[super viewDidLoad];
[self.view addSubview:self.label_1];
[self.view addSubview:self.label_2];
[self.view addSubview:self.label_3];
[self.view addSubview:self.textfield_1];
[self.view addSubview:self.textfield_2];
[self.view addSubview:self.textfield_3];
self.navigationItem.title = @"注册";
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"完成" style: UIBarButtonItemStylePlain target:self action:@selector(RegisterAction:)];//声明一个登陆时所需要调用的方法
}
#pragma mark------- 登陆时所需要调用的回调方法
- (void)RegisterAction:(UIButton *)button{
//调用判断的方法
BOOL isnull = [self isNull];
if (isnull) { //ifnull = yes; 就可以进行注册行为
//当今行注册操作的时候,显示风火轮
[self.activityIndicatorView startAnimating];
/**
由于注册操作是与网络相关的,在中国告诉网络还没普及,有些用户可能使用的
还是2G网络.或者是在一些写好不佳的地方,这时候注册操作可能就比较耗时,我们
可以将注册操作放到子线程中.不影响其它代码执行.
*/
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
EMError *error = [[EMClient sharedClient]registerWithUsername:self.textfield_1.text password:self.textfield_2.text];
if (error == nil) {
NSLog(@"注册成功");
//返回主线程
dispatch_async(dispatch_get_main_queue(), ^{
[self.activityIndicatorView stopAnimating];//停止风火轮的转动
[self.activityIndicatorView removeFromSuperview];//从主界面移除
[self.navigationController popToRootViewControllerAnimated:YES];/.返回登录界面
});
}else{//说明有错误
EMErrorCode errorcode = error.code;
NSString *errorString = @"未知错误";
switch (errorcode) {
case EMErrorUserAlreadyExist:
errorString = @"用户已存在";
break;
default:
errorString = @"请检查网络";
NSLog(@"错误码为---%d",errorcode);
break;
}
//回主线程,做和 UI 相关的操作.
dispatch_async(dispatch_get_main_queue(), ^{
//风火轮停止
[self.activityIndicatorView stopAnimating];
//从主界面移除.
[self.activityIndicatorView removeFromSuperview];
//弹窗,提示错误信息
[self myAlerMessage:errorString];
});
NSLog(@"错误码:%u",errorcode);
NSLog(@"错误描述:%@",error.errorDescription);
}
});
}
}
#pragma mark-------在注册过程中,让用户感知到正在注册.所以使用了平常被称作风火轮的视图
- (UIActivityIndicatorView *)activityIndicatorView{
if (!_activityIndicatorView) {
_activityIndicatorView = [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
_activityIndicatorView.center = self.view.center;
_activityIndicatorView.activityIndicatorViewStyle = UIActivityIndicatorViewStyleGray;
_activityIndicatorView.color = [UIColor redColor];
//直接添加在页面上的话.
[self.view addSubview:_activityIndicatorView];
}
return _activityIndicatorView;
}
- 在注册时提醒用户在注册时,用户名及密码项不能为空.所以用了一个提示框来提醒用户
#pragma mark------提示框;
- (void)myAlerMessage:(NSString*)msg{
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"友情提示" message: msg preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *sureAction = [UIAlertAction actionWithTitle:@"确定" style: UIAlertActionStyleCancel handler:nil];
[alertController addAction:sureAction];
[self presentViewController:alertController animated:YES completion:nil];
}
// 判断用户是否将必要信息输入完整
- (BOOL)isNull{
BOOL isEmpty = YES;
if (self.textfield_1.text.length == 0) {
[self myAlerMessage:@"用户名不能为空"];
return NO;
}
if (self.textfield_2.text.length == 0) {
[self myAlerMessage:@"密码不能为空"];
return NO;
}
if (self.textfield_3.text.length == 0 || ![self.textfield_3.text isEqualToString:self.textfield_2.text]) {
[self myAlerMessage:@"两次密码不一致"];
return NO;
}
return isEmpty;
}