- 主要核心代碼
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;
}