ios封装获取验证码

在开发应用中,我们有很多时候都用到注册或者找回密码,我们都需要获取验证码,在很多时候我们使用起来都会在使用处进行方法实现,那样使得代码的耦合性降低,最近在开发中我也遇到这样的问题,所以对其进行 一个简单的封装首先,我们创建一个继承于NSObject的类verification
在其的.h 中我们创建几个方法

+(verification *)shareVer;
-(void )verificationAction:(UITextField *)phoneText verBtn:(UIButton *)verbtn;
-(NSString *)string;

然后在其.m中进行方法实现,这里我已倒计60秒为例

1.实例变量初始化

+(verification *)shareVer{
    static dispatch_once_t onceToken;
    static GJKverification *manager = nil;
    dispatch_once(&onceToken, ^{
        manager = [[verification alloc] init];
    });
    return manager;
}

2.获取验证码方法实现

-(void)verificationAction:(UITextField *)phoneText verBtn:(UIButton *)verbtn {
    NSString *mobile = phoneText.text;
    if (![Utils isAValidChinaMobileNumber:mobile]) {
        [MBHUDHelper showWarningWithText:@"请输入正确的手机号"];
        return ;
    }
    NSDictionary *dict = @{@"mobile" : phoneText.text};
    // 调用查询手机号的接口  
     if ([[responseObject objectForKey:@"data"] integerValue]) {
            [MBHUDHelper showWarningWithText:@"该手机号已经被注册"];
            [self resumeVerifyButtonStatus:phoneText verBtn:verbtn];
            return ;
        } else {
            [self setCountDownState:verbtn mobileText:phoneText];
            NSString *verificationUrl = [NSString stringWithFormat:@"%@/%@",kMobileRegister, mobile];
           //调用获取验证码的接口
            [ success:^(id responseObject) {
                self.verfString = [responseObject objectForKey:@"data"];
                NSLog(@"验证码%@",self.verfString);
            } failure:^(NSError *error, GJKError *JJError) {

            }];

        }
    } failure:^(NSError *error, GJKError *JJError) {

    }];
}

3.逻辑关系处理

- (void)stopTimer{
    if (self.countDownTimer != nil) {
        [self.countDownTimer invalidate];
        self.countDownTimer = nil;
    }
}


-(void)setCountDownState:(UIButton *)verbtn  mobileText:(UITextField *)phoneTextField{
    self.stopTime = [[NSDate date] timeIntervalSince1970];
    self.stopTime += kMaxRemainSecond;
    verbtn.enabled = NO;
    int nowSince = [[NSDate date] timeIntervalSince1970];
    NSString *str = [NSString stringWithFormat:@"%ds后失效",self.stopTime - nowSince];
    [verbtn setBackgroundImage:[UIImage imageNamed:@"huoquyanzhengma"] forState:UIControlStateNormal];
    [verbtn setTitle:str forState:UIControlStateNormal];
    [self stopTimer];
    self.countDownTimer = [NSTimer scheduledTimerWithTimeInterval:1.0f block:^{
        [self countDownAction:verbtn mobileText:phoneTextField];
    } repeats:YES];
}

- (void)countDownAction:(UIButton*)verbtn mobileText:(UITextField *)phoneTextField{
    if ([[NSDate date] timeIntervalSince1970] > self.stopTime) {
        [self resumeVerifyButtonStatus:phoneTextField verBtn:verbtn];
    } else {
        [self showCurrentRemainSecond:verbtn];
    }
}

- (void)showCurrentRemainSecond:(UIButton *)verbtn {

    int nowSince = [[NSDate date] timeIntervalSince1970];
    [verbtn setBackgroundImage:[UIImage imageNamed:@"huoquyanzhengma"] forState:UIControlStateNormal];
    NSString *str = [NSString stringWithFormat:@"%ds后失效",self.stopTime - nowSince];
    [verbtn setTitle:str forState:UIControlStateNormal];
}

- (void)resumeVerifyButtonStatus:(UITextField*)phoneTextField verBtn:(UIButton *)verbtn{
    [self stopTimer];
    verbtn.enabled = YES;
    phoneTextField.enabled = YES;
    [verbtn setBackgroundImage:[UIImage imageNamed:@"dianjihuoqu"] forState:UIControlStateNormal];
    [verbtn setTitle:@"" forState:UIControlStateNormal];
}

-(NSString *)string {
    return self.verfString;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值