iOS 每日一记之 -----点击验证码按钮 获取验证码
来源:互联网 发布:python为什么叫爬虫 编辑:程序博客网 时间:2024/06/05 09:46
1.首先创建一个获取验证码的按钮 按钮绑定方法 然后实现getAuthCodeBtnClick的方法
// 初始化获取验证码按钮
self.getAuthCodeBtn = [[UIButton alloc] initWithFrame:CGRectMake(SCREEN_WIDTH - 24 - 135, 57, 135, 36)];
[self.getAuthCodeBtn addTarget:self action:@selector(getAuthCodeBtnClick) forControlEvents:UIControlEventTouchUpInside];
self.getAuthCodeBtn.layer.masksToBounds = YES;
self.getAuthCodeBtn.titleLabel.font = FontNeveLightWithSize(12.0);
self.getAuthCodeBtn.layer.borderWidth = 1;
[self.getAuthCodeBtn setTitleColor:AppBlueColor forState:UIControlStateNormal];
[self.getAuthCodeBtn setTitleColor:HintTextColor forState:UIControlStateDisabled];
self.getAuthCodeBtn.backgroundColor = [UIColor whiteColor];
[whiteView addSubview:self.getAuthCodeBtn];
[self updateButtonState:ButtonStateClickable];
2.实现getAuthCodeBtn的点击方法
/ 点击获取验证码按钮
- (void)getAuthCodeBtnClick {
NSLog(@"%@", NSStringFromSelector(_cmd));
NSString *phoneNumber = [self.phoneNumberTextField.text stringByReplacingOccurrencesOfString:@" " withString:@""];
// 验证手机号码
if (phoneNumber.length == 0) {
[self showHUDErrorWithText:@"Please enter your phone number"];
} else {
[self.authCodeTextField becomeFirstResponder];
// 开始倒数计时
[self startWithSeconds:60];
[self updateButtonState:ButtonStateDisable];
NSString *countryCode = self.countryCodeLabel.text;
phoneNumber = [NSString stringWithFormat:@"%@ %@", countryCode, phoneNumber];
[HttpManager getAuthCodeByPhoneNumber:phoneNumber chc_type:@"1" success:^(AFHTTPRequestOperation *operation, id responseObject) {
userRegisterInfo = (NSDictionary *)responseObject[@"result"];
NSInteger rtnCode = [responseObject[@"code"] integerValue];
if (rtnCode == 0) {
NSString *msg = [NSString stringWithFormat:@"Verification code sent to your %@ Phone Number", phoneNumber];
[self showHUDDoneWithText:msg];
// self.authCodeTextField.text = userRegisterInfo[@"ci_password"];
} else if (rtnCode == 1) {// 已经注册过了
[self resetTimer];
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:nil message:@"This number is already registered to a NiHao account. Do you want to log in to NiHao now?" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"OK", nil];
[alertView show];
} else {
[self resetTimer];
[self processServerErrorWithCode:rtnCode andErrorMsg:[responseObject objectForKey:@"message"]];
}
} failBlock:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Error: %@", error);
[self showHUDNetError];
[self stop];
}];
}
}
3.启动定时器
/ 开始倒数计时
-(void)startWithSeconds:(int)totalSecond {
_totalSeconds = totalSecond;
_second = totalSecond;
_timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(timerStart:) userInfo:nil repeats:YES];
_startDate = [NSDate date];
[[NSRunLoop currentRunLoop] addTimer:_timer forMode:NSRunLoopCommonModes];
}
4定时器的执行方法
-(void)timerStart:(NSTimer *)theTimer {
double deltaTime = [[NSDate date] timeIntervalSinceDate:_startDate];
_second = _totalSeconds - (int)(deltaTime + 0.5) ;
if (_second < 0.0) {
[self stop];
} else {
NSString *title = [NSString stringWithFormat:@"%d seconds", _second];
[self.getAuthCodeBtn setTitle:title forState:UIControlStateNormal];
[self.getAuthCodeBtn setTitle:title forState:UIControlStateDisabled];
}
}
5.定时器的关闭和重启代码
// 停止倒数计时
- (void)stop {
[self stopTimerWithText:@"Resend Code"];
}
// 重置倒数计时
- (void)resetTimer {
[self stopTimerWithText:@"Get verification code"];
}
- (void)stopTimerWithText:(NSString *)text {
if (_timer) {
if ([_timer respondsToSelector:@selector(isValid)]) {
if ([_timer isValid]) {
[_timer invalidate];
_second = _totalSeconds;
[self.getAuthCodeBtn setTitle:text forState:UIControlStateNormal];
[self.getAuthCodeBtn setTitle:text forState:UIControlStateDisabled];
[self updateButtonState:ButtonStateClickable];
}
}
}
}
- iOS 每日一记之 -----点击验证码按钮 获取验证码
- 【iOS】点击按钮获取验证码控件
- iOS点击获取短信验证码按钮
- 点击读秒获取验证码按钮实现
- 点击按钮获取验证码,倒计时。
- angularjs 实现点击按钮获取验证码
- 获取验证码按钮,点击后倒计时功能的实现
- 验证码,按钮一分钟内不可点击。
- 笔记-点击获取验证码
- iOS - 验证码,倒计时按钮
- ios开发获取验证码按钮读秒效果
- iOS 之获取短信验证码倒计时
- JSP验证码生成 点击按钮更新
- Android 实现点击按钮获取验证码 60秒后重新获取效果
- Android 实现点击按钮获取验证码 60秒后重新获取效果
- Android 实现点击按钮获取验证码 60秒后重新获取效果
- 【Android自定义View实战】之获取验证码倒计时按钮
- iOS 输入手机号和验证码的TextField,使获取验证码,提交订单 按钮可用
- Oracle-触发器、SQL语句优化
- 【codevs2188】最长上升子序列 标题党系列
- 07-Windows Server 2012 R2 会话远程桌面-标准部署-RD授权
- Maven学习总结
- Android--内存管理机制
- iOS 每日一记之 -----点击验证码按钮 获取验证码
- Android AlertDialog.Builder详解
- 第7周 项目2 - 建立链队算法库
- 08-Windows Server 2012 R2 会话远程桌面-标准部署-使用PowerShell进行部署2-1
- groovy使用HTTPBuilder发送HTTP请求
- UVA10635 Prince and Princess 动态规划
- java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0
- __block在arc和非arc下含义是否一样
- 09-Windows Server 2012 R2 会话远程桌面-标准部署-使用PowerShell进行部署2-2