- 主要核心代碼
EMError *error = [[EMClient sharedClient]registerWithUsername:self.textfield_1.text password:self.textfield_2.text];
- 簡(jiǎn)單圖形
屏幕快照 2016-05-06 下午9.38.53.png
- 簡(jiǎn)單構(gòu)建注冊(cè)頁(yè)面
@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;//確認(rèn)密碼
@property (nonatomic, strong) UIActivityIndicatorView *activityIndicatorView;//風(fēng)火輪
@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 = @"確認(rèn)密碼";
_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 = @"請(qǐng)輸入用戶名";
_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 = @"請(qǐng)輸入密碼";
_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 = @"請(qǐng)確認(rèn)密碼";
_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 = @"注冊(cè)";
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"完成" style: UIBarButtonItemStylePlain target:self action:@selector(RegisterAction:)];//聲明一個(gè)登陸時(shí)所需要調(diào)用的方法
}
#pragma mark------- 登陸時(shí)所需要調(diào)用的回調(diào)方法
- (void)RegisterAction:(UIButton *)button{
//調(diào)用判斷的方法
BOOL isnull = [self isNull];
if (isnull) { //ifnull = yes; 就可以進(jìn)行注冊(cè)行為
//當(dāng)今行注冊(cè)操作的時(shí)候,顯示風(fēng)火輪
[self.activityIndicatorView startAnimating];
/**
由于注冊(cè)操作是與網(wǎng)絡(luò)相關(guān)的,在中國(guó)告訴網(wǎng)絡(luò)還沒(méi)普及,有些用戶可能使用的
還是2G網(wǎng)絡(luò).或者是在一些寫(xiě)好不佳的地方,這時(shí)候注冊(cè)操作可能就比較耗時(shí),我們
可以將注冊(cè)操作放到子線程中.不影響其它代碼執(zhí)行.
*/
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(@"注冊(cè)成功");
//返回主線程
dispatch_async(dispatch_get_main_queue(), ^{
[self.activityIndicatorView stopAnimating];//停止風(fēng)火輪的轉(zhuǎn)動(dòng)
[self.activityIndicatorView removeFromSuperview];//從主界面移除
[self.navigationController popToRootViewControllerAnimated:YES];/.返回登錄界面
});
}else{//說(shuō)明有錯(cuò)誤
EMErrorCode errorcode = error.code;
NSString *errorString = @"未知錯(cuò)誤";
switch (errorcode) {
case EMErrorUserAlreadyExist:
errorString = @"用戶已存在";
break;
default:
errorString = @"請(qǐng)檢查網(wǎng)絡(luò)";
NSLog(@"錯(cuò)誤碼為---%d",errorcode);
break;
}
//回主線程,做和 UI 相關(guān)的操作.
dispatch_async(dispatch_get_main_queue(), ^{
//風(fēng)火輪停止
[self.activityIndicatorView stopAnimating];
//從主界面移除.
[self.activityIndicatorView removeFromSuperview];
//彈窗,提示錯(cuò)誤信息
[self myAlerMessage:errorString];
});
NSLog(@"錯(cuò)誤碼:%u",errorcode);
NSLog(@"錯(cuò)誤描述:%@",error.errorDescription);
}
});
}
}
#pragma mark-------在注冊(cè)過(guò)程中,讓用戶感知到正在注冊(cè).所以使用了平常被稱作風(fēng)火輪的視圖
- (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];
//直接添加在頁(yè)面上的話.
[self.view addSubview:_activityIndicatorView];
}
return _activityIndicatorView;
}
- 在注冊(cè)時(shí)提醒用戶在注冊(cè)時(shí),用戶名及密碼項(xiàng)不能為空.所以用了一個(gè)提示框來(lái)提醒用戶
#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;
}