短信验证码倒计时 - 全局验证码

#import "LoginAndRegViewController.h"
#import "UserModel.h"
#import "GlobalModel.h"
#import "AccountTool.h"
#import "NSStringTool.h"
#import "AlertTool.h"
#import "HttpHeader.h"
#import "StringUtil.h"
#import 
#import "UINavigationController+FDFullscreenPopGesture.h"
#import "NSDate+Utilities.h"

#define countDown 300
@interface LoginAndRegViewController ()
{
    // 倒计时时间
    NSInteger _time;
    // 全局模型
    GlobalModel *_globalModel;
    AccountTool *_accountTool;
}
@property (nonatomic, strong) NSTimer *timer;
// 手机号码Field
@property (weak, nonatomic) IBOutlet UITextField *phoneNumTF;
// 验证码Field
@property (weak, nonatomic) IBOutlet UITextField *checkCodeTF;
// 获取验证码按钮
@property (weak, nonatomic) IBOutlet UIButton *checkCodeBtn;
// 登录按钮
@property (weak, nonatomic) IBOutlet UIButton *loginBtn;
@end

@implementation LoginAndRegViewController
#pragma mark - view life circle  viewController生命周期方法
- (void)viewDidLoad {
    [super viewDidLoad];
    [self prepareUI];
    self.navigationController.fd_fullscreenPopGestureRecognizer.enabled = NO;
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:NO];
    if (self.isHiddenBackBtn) {
        self.backBtn.hidden = YES;
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

#pragma mark - custom methods  自定义方法
- (void)prepareUI{
    
    self.title = @"登录";
    
    _globalModel = [GlobalModel sharedGlobalModel];
    _accountTool = [[AccountTool alloc] init];
    
    self.loginBtn.enabled = NO;

    if ([_accountTool getPhoneNum] != nil) {// 手机号码不为空
        self.phoneNumTF.text = [_accountTool getPhoneNum];
        if ([_accountTool getUserToken]) {
            [self requestDataWithLogin];
        }
    }
    
    // 监听TextField.text改变
    [self.phoneNumTF addTarget:self action:@selector(textfieldDidChangedText:) forControlEvents:UIControlEventEditingChanged];
    [self.checkCodeTF addTarget:self action:@selector(checkCodeTFDidChangedText:) forControlEvents:UIControlEventEditingChanged];
    
    if (_globalModel.codeDate) {
        NSInteger countDownSec = [_globalModel.codeDate distanceInSecondsToDate:[NSDate date]];
        if (countDownSec < countDown) {
            // 验证码时间内
            self.checkCodeBtn.enabled = NO;
            _time = countDown - countDownSec;
            self.checkCodeBtn.titleLabel.text = [NSString stringWithFormat:@"%zi秒", _time];
            [self.checkCodeBtn setTitle:[NSString stringWithFormat:@"%zi秒", _time] forState:UIControlStateNormal];
            
            self.timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timeDown) userInfo:nil repeats:YES];
            return;
        }else{
            // 验证码时间外
            _time = countDown;
        }
    }
    _time = countDown;
}

- (void)timeDown
{
    _time --;
    
    if (_time == 0) {
        self.checkCodeBtn.titleLabel.text = @"重新获取";
        [self.checkCodeBtn setTitle:@"重新获取" forState:UIControlStateNormal];
        self.checkCodeBtn.enabled = YES;
        
        [_timer invalidate];
        _timer = nil;
        _time = countDown;
        return;
    }
    self.checkCodeBtn.titleLabel.text = [NSString stringWithFormat:@"%zi秒", _time];
    [self.checkCodeBtn setTitle:[NSString stringWithFormat:@"%zi秒", _time] forState:UIControlStateNormal];
    
}

//
- (void)textfieldDidChangedText:(UITextField *)textfield{
    if (textfield.text.length == 11 && [StringUtil isMobile:textfield.text] && self.checkCodeTF.text.length != 0) {
        self.loginBtn.enabled = YES;
    }else{
        self.loginBtn.enabled = NO;
    }
}

- (void)checkCodeTFDidChangedText:(UITextField *)textfield{
    if (self.checkCodeTF.text.length != 0) {
        self.loginBtn.enabled = YES;
    }else{
        self.loginBtn.enabled = NO;
    }
}

#pragma mark 按钮响应
// 登录按钮
- (IBAction)loginBtnDidClick:(UIButton *)sender {
    // 校验
    if (![StringUtil isMobile:self.phoneNumTF.text]) {
        [AlertTool alertWithTipStr:@"手机号码错误,请重新输入"];
        return;
    }
    
    // keychain中保存的账户数组
    NSArray *accountArr = [SSKeychain accountsForService:KeychainService];
    for (NSDictionary *dict in accountArr) {
        // 判断账户数组的账户是否与textField中的相匹配
        if ([dict[@"acct"] isEqualToString:self.phoneNumTF.text]) {
            // keychain中保存过
            [self requestDataWithLogin];
        }
    }
    // keychain中没保存过
    [self requestDataWithReg];
}

// 获取验证码按钮
- (IBAction)checkCodeBtnDidClick:(UIButton *)sender {
    // 验证手机号码
    if (![StringUtil isMobile:self.phoneNumTF.text]) {
        [AlertTool alertWithTipStr:@"手机号码错误,请重新输入"];
        return;
    }
    self.checkCodeBtn.enabled = NO;
    self.checkCodeBtn.titleLabel.text = [NSString stringWithFormat:@"%zi秒", _time];
    [self.checkCodeBtn setTitle:[NSString stringWithFormat:@"%zi秒", _time] forState:UIControlStateNormal];
    
    self.timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timeDown) userInfo:nil repeats:YES];
    _globalModel.codeDate = [NSDate date];
    // 验证码请求
    [self requestDataWithValidCode];
    
}
#pragma mark 网络请求

#pragma mark - sources and delegates 代理、协议方法

#pragma mark - getters and setters 属性的设置和获取方法

-(void)dealloc{
    [self.timer invalidate];
    self.timer = nil;
}
@end

NSDateUtils
https://github.com/hychen1024/NSDateUtils

你可能感兴趣的:(短信验证码倒计时 - 全局验证码)