在开发应用中,我们有很多时候都用到注册或者找回密码,我们都需要获取验证码,在很多时候我们使用起来都会在使用处进行方法实现,那样使得代码的耦合性降低,最近在开发中我也遇到这样的问题,所以对其进行 一个简单的封装首先,我们创建一个继承于NSObject的类verification
在其的.h 中我们创建几个方法
+(verification *)shareVer;
-(void )verificationAction:(UITextField *)phoneText verBtn:(UIButton *)verbtn;
-(NSString *)string;
然后在其.m中进行方法实现,这里我已倒计60秒为例
1.实例变量初始化
+(verification *)shareVer{
static dispatch_once_t onceToken;
static GJKverification *manager = nil;
dispatch_once(&onceToken, ^{
manager = [[verification alloc] init];
});
return manager;
}
2.获取验证码方法实现
-(void)verificationAction:(UITextField *)phoneText verBtn:(UIButton *)verbtn {
NSString *mobile = phoneText.text;
if (![Utils isAValidChinaMobileNumber:mobile]) {
[MBHUDHelper showWarningWithText:@"请输入正确的手机号"];
return ;
}
NSDictionary *dict = @{@"mobile" : phoneText.text};
// 调用查询手机号的接口
if ([[responseObject objectForKey:@"data"] integerValue]) {
[MBHUDHelper showWarningWithText:@"该手机号已经被注册"];
[self resumeVerifyButtonStatus:phoneText verBtn:verbtn];
return ;
} else {
[self setCountDownState:verbtn mobileText:phoneText];
NSString *verificationUrl = [NSString stringWithFormat:@"%@/%@",kMobileRegister, mobile];
//调用获取验证码的接口
[ success:^(id responseObject) {
self.verfString = [responseObject objectForKey:@"data"];
NSLog(@"验证码%@",self.verfString);
} failure:^(NSError *error, GJKError *JJError) {
}];
}
} failure:^(NSError *error, GJKError *JJError) {
}];
}
3.逻辑关系处理
- (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;
}
网友评论