项目中很常用的发送短信验证码倒计时功能(登录、注册、忘记密码、修改支付密码等)
Untitled.gif
对于这种功能大家可能觉得再简单不过,会有多种实现方式,但是仅仅是实现倒计时功能就没必要拿出来说了,本文想要解决的问题是:在实现倒计时后,应用进入后台一段时间再回到前台,或应用从当前页面返回前一页面或者跳转至其他页面后再回到当前页面,倒计时依然继续的问题。本文只是笔者提供的一种解决方案供大家参考,如果存在问题或者有其他更优质的解决方案,希望读者留言。
在只考虑进入后台不考虑页面变换的情况下,笔者最先想到的方案是:增加进入后台程序存活的时间。在需要的页面先添加程序进入后台的通知
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(appWillEnterForegroundNotification) name:UIApplicationDidEnterBackgroundNotification object:nil];
通知方法中实现:
- (void)appWillEnterForegroundNotification{
UIApplication* app = [UIApplication sharedApplication];
__block UIBackgroundTaskIdentifier bgTask;
//申请一个后台执行的任务 iOS9后大概能争取到3分钟 ,对于60s倒计时足够用,如果时间更长的话需要借助默认音频等(容易引起上架问题)
bgTask = [app beginBackgroundTaskWithExpirationHandler:^{
dispatch_async(dispatch_get_main_queue(), ^{
if (bgTask != UIBackgroundTaskInvalid){
bgTask = UIBackgroundTaskInvalid;
}
});
}];
}
之后笔者为了解决页面变换的问题,借鉴了别人提供的思路,即单列模式封装倒计时功能,此种方案可同时解决进入后台及页面变换的问题
声明文件中:
#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
实现文件中:
#define kMaxCountDownTime 60//倒计时时间,可自定义
#import "SecureCodeTimerManager.h"
@interface SecureCodeTimerManager ()
@property (nonatomic, assign) kCountDownType countDonwnType;
@property (nonatomic, nullable, strong) dispatch_source_t loginTimer;//登录界面倒计时timer
@property (nonatomic, nullable, strong) dispatch_source_t findPwdTimer;//找回密码界面倒计时timer
@property (nonatomic, nullable, strong) dispatch_source_t registerTimer;//注册界面倒计时timer
@property (nonatomic, nullable, strong) dispatch_source_t modifyPhoneTimer;//修改手机号界面倒计时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];
获取验证码按钮点击事件:
#pragma mark - 获取验证码按钮点击事件
-(void)secureCodeBtnDidClicked:(UIButton *)sender{
self.secureCodeBtn.enabled = NO;
// 创建定时器
[[SecureCodeTimerManager sharedInstance] timerCountDownWithType:kCountDownTypeLogin];
// 获取验证码网络请求
[self secureCodeRequest];
}
通知事件的处理
#pragma mark - NSNotification 处理倒计时事件
- (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];
}
结束定时器的方法:
[[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__; \
}
网友评论