iOS 注册界面

注册

使用 xib搭建界面 主要有以下代码

  • 对手机号的输入显示格式 3-4-4格式的 如157 xxxx xxxx
  • 发送验证码按钮的控制 以及倒计时
  • 注册按钮的状态控制

与后台约定 点击发送验证码按钮发送请求 需要参数

  • 手机号码

返回参数:

  • status 状态码
  • code 验证码
  • JSESSIONID

status 表示手机号码的状态

  • 已注册
  • 未注册
iOS 注册界面_第1张图片
注册界面

手写对三个输入框进行监听

    [self.phoneInput addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
    [self.passwordInput addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
    [self.verificationCodeInput addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];


输入框相关操作

定义四个标志变量


{
    BOOL allOK;
    BOOL phoeOK;
    BOOL codeOk;
    BOOL passwordOk;
}

对三个输入框的长度进行限制

  • 手机号码 11位
  • 验证码 6 位
  • 密码 6~20位

只有三个输入框全部满足要求时
注册按钮才可以交互

#pragma mark Textfiled相关
-(void)textFieldDidChange:(UITextField * )textField{
    if (textField == self.phoneInput) {
        if (textField.text.length < 13)   phoeOK = NO;
        if (textField.text.length >= 13) {
            _sendVerCode.enabled = YES;
            phoeOK = YES;
            textField.text = [textField.text substringToIndex:13];
        }
        if(textField.text.length < 13) _sendVerCode.enabled = NO;
        
    }
    
    
    
    
    if (textField == _verificationCodeInput) {
        if(textField.text.length < 6) codeOk = NO;
        if(textField.text.length==6) codeOk = YES;
        
        if (textField.text.length >= 6) {
            textField.text = [textField.text substringToIndex:6];
        }
    }
    
    
    
    if (textField == _passwordInput) {
        if(textField.text.length>=6&&textField.text.length<=20)passwordOk = YES;
        else passwordOk = NO;
        if (textField.text.length >= 20) {
            textField.text = [textField.text substringToIndex:20];
        }
    }
    
    if(phoeOK&&passwordOk&&codeOk){
        _registBtu.enabled = YES;
    }else{
      _registBtu.enabled = NO;
    }
    
    
}


手机号码的格式 判断 到发送请求时会截取空白符号

-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
    if (textField==_phoneInput) {
        if (range.length == 0){
            if (textField.text.length == 3 ||textField.text.length == 8) {
                textField.text = [NSString stringWithFormat:@"%@ ",textField.text];
            }
        }
    }
    return YES;
}

发送验证码请求

首先对手机号码"洗白操作"

+(NSString *)removeSpace:(NSString *)str{
    NSMutableString *strM = [NSMutableString stringWithString:str];
    [strM replaceOccurrencesOfString:@" " withString:@"" options:NSLiteralSearch range:NSMakeRange(0, strM.length)];
    return [strM copy];
}


接着手机号码的验证 验证手机号码是否符合规定

+ (BOOL) validateMobile:(NSString *)mobile
{
    //手机号以13, 15,18开头,八个 \d 数字字符
    NSString *phoneRegex = @"^((13[0-9])|(15[^4,\\D])|(18[0,0-9]))\\d{8}$";
    NSPredicate *phoneTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",phoneRegex];
    return [phoneTest evaluateWithObject:mobile];
}

发送网络请求 解析状态码

   [HBNetRequest Get:[NSString stringWithFormat:@"%@?ulogin=%@",REGISTFRIST,phoneNum] para:nil
                 complete:^(id data) {
                     [HBAuxiliary saveCookie];
                     NSUInteger   status =  [data[@"status"] integerValue] ;
                     if(status ==0)  [self hadRegist];
                     if(status ==1) {
                         [self startCountDown];
                         _respondCode =data[@"code"];
                    }
                     [_hud hide:YES];
                     
                 } fail:^(NSError *error) {
                 }];

手机已经注册的话提示Toast

[self.view makeToast:@"您的手机号已注册" duration:2.0 position:CSToastPositionCenter];

未注册的话解析code 开始倒计时
参考


#pragma mark 倒计时相关
-(void)startCountDown {
    _countDown = 60;
    _sendVerCode.enabled = NO;
    _timer = [NSTimer timerWithTimeInterval:1.0 target:self selector:@selector(timerFired:) userInfo:nil repeats:YES]; // 需要加入手动RunLoop,需要注意的是在NSTimer工作期间self是被强引用的
    [[NSRunLoop currentRunLoop] addTimer:_timer forMode:NSRunLoopCommonModes]; // 使用NSRunLoopCommonModes才能保证RunLoop切换模式时,NSTimer能正常工作。
}
- (void)stopTimer {
    if (_timer) {
        [_timer invalidate];
    }
}
-(void)timerFired:(NSTimer *)timer {
    switch (_countDown) {
        case 1:
            [_sendVerCode setTitle:@"发送验证码" forState:UIControlStateNormal];
            _sendVerCode.enabled = YES;
            [self stopTimer];
            break;
        default:
            _countDown -=1;
            [_sendVerCode setTitle:[NSString stringWithFormat:@"重发(%d)",_countDown] forState:UIControlStateNormal];
            break;
    }
}

-(void)dealloc {
    [[NSNotificationCenter defaultCenter]removeObserver:self name:UIApplicationDidEnterBackgroundNotification object:nil];
    [[NSNotificationCenter defaultCenter]removeObserver:self name:UIApplicationWillEnterForegroundNotification object:nil];
    [self stopTimer]; // 如果没有在合适的地方销毁定时器就会内存泄漏啦,delloc也不可能执行。正确的销毁定时器这里可以不用写这个方法了,这里只是提个醒
}
//避免重新回到前台错误
-(void)setupNotification {
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(enterBG) name:UIApplicationDidEnterBackgroundNotification object:nil];
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(enterFG) name:UIApplicationWillEnterForegroundNotification object:nil];
}

-(void)enterBG {
    _beforeDate = [NSDate date];
}
-(void)enterFG {
    NSDate * now = [NSDate date];
    int interval = (int)ceil([now timeIntervalSinceDate:_beforeDate]);
    //减掉后台那段时间
    int val = _countDown - interval;
    if(val > 1){
        _countDown -= interval;
    }else{
        _countDown = 1;
    }
}


避免当应用到后台时 计时停止,当应用退到后台时 订阅消息 并且 存下时间,当应用互道前台时取出时间 计算差值,接着倒计时。

注册

本地校验验证码 和密码格式正确后发送注册请求 参数

  • 验证码
  • 密码

返回参数后进行其他操作(注册成功后默认已经登录)会调到主页等或进行其他操作

 if([_respondCode isEqualToString:_verificationCodeInput.text]&&
       [HBAuxiliary validatePassword:_passwordInput.text]){//验证码正确 && 密码格式正确
        
        [_hud show:YES];
        [HBNetRequest Post: REGISTlast para:@{@"code"        : _verificationCodeInput.text,
                                              @"upassword"   : _passwordInput.text,
        }complete:^(id data) {
            
            NSInteger status = [data[@"status"] integerValue];
            if (status == 1) {
                NSDictionary *userDic = data[@"user"];
                HBUserItem *user = [[HBUserItem alloc] initWithDictionary:userDic error:nil];
                [HBUserItem saveUser:user];
                [HBAuxiliary saveCookie];

                [_hud hide:YES];
                [self hiddenKeyboardForTap];
                AppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
                appDelegate.window.rootViewController = [MainViewController new];
                
                
            }


        } fail:^(NSError *error) {
              [_hud hide:YES];
        }];


还有在适当的时候加上 键盘隐藏代码


- (void)hiddenKeyboardForTap{
    [_phoneInput resignFirstResponder];
    [_passwordInput resignFirstResponder];
    [_verificationCodeInput resignFirstResponder];
}

你可能感兴趣的:(iOS 注册界面)