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

本文完整Demo

項(xiàng)目中很常用的發(fā)送短信驗(yàn)證碼倒計(jì)時(shí)功能(登錄、注冊(cè)、忘記密碼、修改支付密碼等)


Untitled.gif

對(duì)于這種功能大家可能覺得再簡(jiǎn)單不過,會(huì)有多種實(shí)現(xiàn)方式,但是僅僅是實(shí)現(xiàn)倒計(jì)時(shí)功能就沒必要拿出來說了,本文想要解決的問題是:在實(shí)現(xiàn)倒計(jì)時(shí)后,應(yīng)用進(jìn)入后臺(tái)一段時(shí)間再回到前臺(tái),或應(yīng)用從當(dāng)前頁面返回前一頁面或者跳轉(zhuǎn)至其他頁面后再回到當(dāng)前頁面,倒計(jì)時(shí)依然繼續(xù)的問題。本文只是筆者提供的一種解決方案供大家參考,如果存在問題或者有其他更優(yōu)質(zhì)的解決方案,希望讀者留言。

在只考慮進(jìn)入后臺(tái)不考慮頁面變換的情況下,筆者最先想到的方案是:增加進(jìn)入后臺(tái)程序存活的時(shí)間。在需要的頁面先添加程序進(jìn)入后臺(tái)的通知
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(appWillEnterForegroundNotification) name:UIApplicationDidEnterBackgroundNotification object:nil];

通知方法中實(shí)現(xiàn):

- (void)appWillEnterForegroundNotification{
    UIApplication*   app = [UIApplication sharedApplication];
    __block    UIBackgroundTaskIdentifier bgTask;
    //申請(qǐng)一個(gè)后臺(tái)執(zhí)行的任務(wù) iOS9后大概能爭(zhēng)取到3分鐘 ,對(duì)于60s倒計(jì)時(shí)足夠用,如果時(shí)間更長的話需要借助默認(rèn)音頻等(容易引起上架問題)
    bgTask = [app beginBackgroundTaskWithExpirationHandler:^{
        dispatch_async(dispatch_get_main_queue(), ^{
          if (bgTask != UIBackgroundTaskInvalid){
              bgTask = UIBackgroundTaskInvalid;
          }
        });
    }];
}
之后筆者為了解決頁面變換的問題,借鑒了別人提供的思路,即單列模式封裝倒計(jì)時(shí)功能,此種方案可同時(shí)解決進(jìn)入后臺(tái)及頁面變換的問題

聲明文件中:

#import <Foundation/Foundation.h>

#define kLoginCountDownCompletedNotification            @"kLoginCountDownCompletedNotification"
#define kFindPasswordCountDownCompletedNotification     @"kFindPasswordCountDownCompletedNotification"
#define kRegisterCountDownCompletedNotification         @"kRegisterCountDownCompletedNotification"
#define kModifyPhoneCountDownCompletedNotification      @"kModifyPhoneCountDownCompletedNotification"

#define kLoginCountDownExecutingNotification            @"kLoginCountDownExecutingNotification"
#define kFindPasswordCountDownExecutingNotification     @"kFindPasswordCountDownExecutingNotification"
#define kRegisterCountDownExecutingNotification         @"kRegisterCountDownExecutingNotification"
#define kModifyPhoneCountDownExecutingNotification      @"kModifyPhoneCountDownExecutingNotification"

NS_ASSUME_NONNULL_BEGIN

typedef NS_ENUM(NSInteger, kCountDownType) {
    kCountDownTypeLogin,
    kCountDownTypeFindPassword,
    kCountDownTypeRegister,
    kCountDownTypeModifyPhone,
};

@interface SecureCodeTimerManager : NSObject

DEF_SINGLETON(SecureCodeTimerManager)
- (void)timerCountDownWithType:(kCountDownType)countDownType;
- (void)cancelTimerWithType:(kCountDownType)countDownType;
@end

NS_ASSUME_NONNULL_END

實(shí)現(xiàn)文件中:

#define kMaxCountDownTime 60//倒計(jì)時(shí)時(shí)間,可自定義

#import "SecureCodeTimerManager.h"

@interface SecureCodeTimerManager ()
@property (nonatomic, assign) kCountDownType countDonwnType;
@property (nonatomic, nullable, strong) dispatch_source_t loginTimer;//登錄界面倒計(jì)時(shí)timer
@property (nonatomic, nullable, strong) dispatch_source_t findPwdTimer;//找回密碼界面倒計(jì)時(shí)timer
@property (nonatomic, nullable, strong) dispatch_source_t registerTimer;//注冊(cè)界面倒計(jì)時(shí)timer
@property (nonatomic, nullable, strong) dispatch_source_t modifyPhoneTimer;//修改手機(jī)號(hào)界面倒計(jì)時(shí)timer
@end

@implementation SecureCodeTimerManager

IMP_SINGLETON(SecureCodeTimerManager)

- (void)timerCountDownWithType:(kCountDownType)countDownType {
    
    _countDonwnType = countDownType;
    
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    dispatch_source_t _timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, queue);
    dispatch_source_set_timer(_timer, dispatch_walltime(NULL, 0), 1.0 * NSEC_PER_SEC, 0);
    
    NSTimeInterval seconds = kMaxCountDownTime;
    NSDate *endTime = [NSDate dateWithTimeIntervalSinceNow:seconds];
    dispatch_source_set_event_handler(_timer, ^{
        
        int interval = [endTime timeIntervalSinceNow];
        if (interval <= 0) {
            dispatch_source_cancel(_timer);
            dispatch_async(dispatch_get_main_queue(), ^{
                
                if ([_timer isEqual:self.loginTimer]) {
                    [[NSNotificationCenter defaultCenter] postNotificationName:kLoginCountDownCompletedNotification object:@(interval)];
                } else if ([_timer isEqual:self.findPwdTimer]) {
                    [[NSNotificationCenter defaultCenter] postNotificationName:kFindPasswordCountDownCompletedNotification object:@(interval)];
                } else if ([_timer isEqual:self.registerTimer]) {
                    [[NSNotificationCenter defaultCenter] postNotificationName:kRegisterCountDownCompletedNotification object:@(interval)];
                } else if ([_timer isEqual:self.modifyPhoneTimer]) {
                    [[NSNotificationCenter defaultCenter] postNotificationName:kModifyPhoneCountDownCompletedNotification object:@(interval)];
                }
                
            });
        }
        else {
            dispatch_async(dispatch_get_main_queue(), ^{
                
                if ([_timer isEqual:self.loginTimer]) {
                    [[NSNotificationCenter defaultCenter] postNotificationName:kLoginCountDownExecutingNotification object:@(interval)];
                } else if ([_timer isEqual:self.findPwdTimer]) {
                    [[NSNotificationCenter defaultCenter] postNotificationName:kFindPasswordCountDownExecutingNotification object:@(interval)];
                } else if ([_timer isEqual:self.registerTimer]) {
                    [[NSNotificationCenter defaultCenter] postNotificationName:kRegisterCountDownExecutingNotification object:@(interval)];
                } else if ([_timer isEqual:self.modifyPhoneTimer]) {
                    [[NSNotificationCenter defaultCenter] postNotificationName:kModifyPhoneCountDownExecutingNotification object:@(interval)];
                }
                
            });
        }
    });
    
    if (self.countDonwnType == kCountDownTypeLogin) {
        self.loginTimer = _timer;
    } else if (self.countDonwnType == kCountDownTypeFindPassword) {
        self.findPwdTimer = _timer;
    } else if (self.countDonwnType == kCountDownTypeRegister) {
        self.registerTimer = _timer;
    } else if (self.countDonwnType == kCountDownTypeModifyPhone) {
        self.modifyPhoneTimer = _timer;
    }
   
    dispatch_resume(_timer);
}

- (void)cancelTimerWithType:(kCountDownType)countDownType {
    switch (countDownType) {
            case kCountDownTypeLogin:
            if (self.loginTimer) {
                dispatch_source_cancel(self.loginTimer);
                self.loginTimer = nil;
            }
            
            break;
            case kCountDownTypeRegister:
            if (self.registerTimer) {
                dispatch_source_cancel(self.registerTimer);
                self.registerTimer = nil;
            }
            
            break;
            case kCountDownTypeModifyPhone:
            if (self.modifyPhoneTimer) {
                dispatch_source_cancel(self.modifyPhoneTimer);
                self.modifyPhoneTimer = nil;
            }
            
            break;
            case kCountDownTypeFindPassword:
            if (self.findPwdTimer) {
                dispatch_source_cancel(self.findPwdTimer);
                self.findPwdTimer = nil;
            }
            
            break;
        default:
            break;
    }
}

@end

如何使用,例如在登錄頁面:

 // 添加通知
 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(loginTimerCountDownExecutingWithTimeOut:) name:kLoginCountDownExecutingNotification object:nil];
 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(loginTimerCountDownCompleted) name:kLoginCountDownCompletedNotification object:nil];

獲取驗(yàn)證碼按鈕點(diǎn)擊事件:

#pragma mark - 獲取驗(yàn)證碼按鈕點(diǎn)擊事件
-(void)secureCodeBtnDidClicked:(UIButton *)sender{
    self.secureCodeBtn.enabled = NO;
    // 創(chuàng)建定時(shí)器
    [[SecureCodeTimerManager sharedInstance] timerCountDownWithType:kCountDownTypeLogin];
    //  獲取驗(yàn)證碼網(wǎng)絡(luò)請(qǐng)求
    [self secureCodeRequest];
}

通知事件的處理

#pragma mark - NSNotification 處理倒計(jì)時(shí)事件
- (void)loginTimerCountDownExecutingWithTimeOut:(NSNotification *)notification {
    self.secureCodeBtn.enabled = NO;
    NSInteger timeOut = [notification.object integerValue];
    [self.secureCodeBtn setTitle: [NSString stringWithFormat:@"%lds",(long)timeOut] forState:(UIControlStateNormal)];
    [self.secureCodeBtn setTitleColor:KColor_font_blue forState:UIControlStateNormal];
}
     
- (void)loginTimerCountDownCompleted {
    self.secureCodeBtn.enabled = YES;
    [self.secureCodeBtn setTitle:  @"重新獲取" forState:(UIControlStateNormal)];
    [self.secureCodeBtn setTitleColor:KColor_font_blue forState:UIControlStateNormal];
}

結(jié)束定時(shí)器的方法:

 [[SecureCodeTimerManager sharedInstance] cancelTimerWithType:kCountDownTypeLogin];

單例宏定義:


#undef    DEF_SINGLETON
#define DEF_SINGLETON( __class ) \
+ (__class *)sharedInstance;

#undef    IMP_SINGLETON
#define IMP_SINGLETON( __class ) \
+ (__class *)sharedInstance \
{ \
static __class * __singleton__; \
static dispatch_once_t onceToken; \
dispatch_once( &onceToken, ^{ __singleton__ = [[__class alloc] init]; } ); \
return __singleton__; \
}
最后編輯于
?著作權(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ù)。

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