在開(kāi)發(fā)應(yīng)用中,我們有很多時(shí)候都用到注冊(cè)或者找回密碼,我們都需要獲取驗(yàn)證碼,在很多時(shí)候我們使用起來(lái)都會(huì)在使用處進(jìn)行方法實(shí)現(xiàn),那樣使得代碼的耦合性降低,最近在開(kāi)發(fā)中我也遇到這樣的問(wèn)題,所以對(duì)其進(jìn)行 一個(gè)簡(jiǎn)單的封裝首先,我們創(chuàng)建一個(gè)繼承于NSObject的類(lèi)verification
在其的.h 中我們創(chuàng)建幾個(gè)方法
+(verification *)shareVer;
-(void )verificationAction:(UITextField *)phoneText verBtn:(UIButton *)verbtn;
-(NSString *)string;
然后在其.m中進(jìn)行方法實(shí)現(xiàn),這里我已倒計(jì)60秒為例
1.實(shí)例變量初始化
+(verification *)shareVer{
static dispatch_once_t onceToken;
static GJKverification *manager = nil;
dispatch_once(&onceToken, ^{
manager = [[verification alloc] init];
});
return manager;
}
2.獲取驗(yàn)證碼方法實(shí)現(xiàn)
-(void)verificationAction:(UITextField *)phoneText verBtn:(UIButton *)verbtn {
NSString *mobile = phoneText.text;
if (![Utils isAValidChinaMobileNumber:mobile]) {
[MBHUDHelper showWarningWithText:@"請(qǐng)輸入正確的手機(jī)號(hào)"];
return ;
}
NSDictionary *dict = @{@"mobile" : phoneText.text};
// 調(diào)用查詢(xún)手機(jī)號(hào)的接口
if ([[responseObject objectForKey:@"data"] integerValue]) {
[MBHUDHelper showWarningWithText:@"該手機(jī)號(hào)已經(jīng)被注冊(cè)"];
[self resumeVerifyButtonStatus:phoneText verBtn:verbtn];
return ;
} else {
[self setCountDownState:verbtn mobileText:phoneText];
NSString *verificationUrl = [NSString stringWithFormat:@"%@/%@",kMobileRegister, mobile];
//調(diào)用獲取驗(yàn)證碼的接口
[ success:^(id responseObject) {
self.verfString = [responseObject objectForKey:@"data"];
NSLog(@"驗(yàn)證碼%@",self.verfString);
} failure:^(NSError *error, GJKError *JJError) {
}];
}
} failure:^(NSError *error, GJKError *JJError) {
}];
}
3.邏輯關(guān)系處理
- (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;
}