短信驗(yàn)證碼倒計(jì)時(shí) - 全局驗(yàn)證碼

#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 <SSKeychain.h>
#import "UINavigationController+FDFullscreenPopGesture.h"
#import "NSDate+Utilities.h"

#define countDown 300
@interface LoginAndRegViewController ()
{
    // 倒計(jì)時(shí)時(shí)間
    NSInteger _time;
    // 全局模型
    GlobalModel *_globalModel;
    AccountTool *_accountTool;
}
@property (nonatomic, strong) NSTimer *timer;
// 手機(jī)號(hào)碼Field
@property (weak, nonatomic) IBOutlet UITextField *phoneNumTF;
// 驗(yàn)證碼Field
@property (weak, nonatomic) IBOutlet UITextField *checkCodeTF;
// 獲取驗(yàn)證碼按鈕
@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) {// 手機(jī)號(hào)碼不為空
        self.phoneNumTF.text = [_accountTool getPhoneNum];
        if ([_accountTool getUserToken]) {
            [self requestDataWithLogin];
        }
    }
    
    // 監(jiān)聽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) {
            // 驗(yàn)證碼時(shí)間內(nèi)
            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{
            // 驗(yàn)證碼時(shí)間外
            _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 按鈕響應(yīng)
// 登錄按鈕
- (IBAction)loginBtnDidClick:(UIButton *)sender {
    // 校驗(yàn)
    if (![StringUtil isMobile:self.phoneNumTF.text]) {
        [AlertTool alertWithTipStr:@"手機(jī)號(hào)碼錯(cuò)誤,請(qǐng)重新輸入"];
        return;
    }
    
    // keychain中保存的賬戶數(shù)組
    NSArray *accountArr = [SSKeychain accountsForService:KeychainService];
    for (NSDictionary *dict in accountArr) {
        // 判斷賬戶數(shù)組的賬戶是否與textField中的相匹配
        if ([dict[@"acct"] isEqualToString:self.phoneNumTF.text]) {
            // keychain中保存過
            [self requestDataWithLogin];
        }
    }
    // keychain中沒保存過
    [self requestDataWithReg];
}

// 獲取驗(yàn)證碼按鈕
- (IBAction)checkCodeBtnDidClick:(UIButton *)sender {
    // 驗(yàn)證手機(jī)號(hào)碼
    if (![StringUtil isMobile:self.phoneNumTF.text]) {
        [AlertTool alertWithTipStr:@"手機(jī)號(hào)碼錯(cuò)誤,請(qǐng)重新輸入"];
        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];
    // 驗(yàn)證碼請(qǐng)求
    [self requestDataWithValidCode];
    
}
#pragma mark 網(wǎng)絡(luò)請(qǐng)求

#pragma mark - sources and delegates 代理、協(xié)議方法

#pragma mark - getters and setters 屬性的設(shè)置和獲取方法

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

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

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容