// // Message_SMS_VC.m // jiaPei // // Created by apple on 2017/1/9. // Copyright © 2017年 JCZ. All rights reserved. // #import "Message_SMS_VC.h" #import "LoginViewController.h" #import "Message_Set_VC.h" //这个代表可以输入数字和换行,请注意这个n,如果不写这个,Done按键将不会触发,如果用在SearchBar中,将会不触发Search事件 #define NUMBERS @"0123456789n" @interface Message_SMS_VC () { //记住变星号前的字符串 NSString * _phoneStr; } @end @implementation Message_SMS_VC - (void)viewDidLoad { [super viewDidLoad]; self.title = @"手机号验证"; self.view.backgroundColor = backGroundColor; [self configNavigationBarDismissNav]; //将初始总时间设置为60妙 self.sumTime = 60; [self.view addSubview:self.phoneTF]; [self.view addSubview:self.verityTF]; [self.view addSubview:self.sendVerityBut]; [self.view addSubview:self.verityCodeBut]; [self.sendVerityBut addTarget:self action:@selector(VerityAction) forControlEvents:UIControlEventTouchUpInside]; [self.verityCodeBut addTarget:self action:@selector(verityCodeAction:) forControlEvents:UIControlEventTouchUpInside]; //对传入手机号码的处理 if (!_isCanEditNum) { _phoneTF.enabled = NO; } _phoneTF.text = _phoneNumber; //_phoneTF.text = @"15188352230"; if (_phoneTF.text.length > 0) { _phoneStr = _phoneTF.text; _phoneTF.text = [_phoneTF.text stringByReplacingCharactersInRange:NSMakeRange(3, 4) withString:@"****"]; } } //-(void)backBtnAction //{ // [self dismissViewControllerAnimated:YES completion:nil]; //} -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { [super touchesBegan:touches withEvent:event]; if ([_phoneTF isFirstResponder]) { [_phoneTF resignFirstResponder]; } if ([_verityTF isFirstResponder]) { [_verityTF resignFirstResponder]; } } #pragma mark - 发送按钮 - (void)VerityAction { [RQ_SHARE_FUNCTION checkMobPolicyOnResult:^(BOOL success) { if (success) { //判断用户是否输入手机号码 手机号码是否正确 if(self.phoneTF.text.length == 0) { [self alertActionWithTitle:@"提示" message:@"请输入手机号码" isCanceAction:NO]; return; } if (self.phoneTF.text.length != 11) { // NSLog(@"电话号码输入有误"); [self alertActionWithTitle:@"提示" message:@"电话号码输入有误" isCanceAction:NO]; return; } if (![Util connectedToNetWork]) { showMsgUnconnect(); return; } //每次将要发送验证码的时候需要将总时间变为60秒 self.sumTime = 60; //关闭按钮的交互 self.sendVerityBut.enabled = NO; //打开计时器 [self.timer setFireDate:[NSDate distantPast]]; //说明手机号码正确,可以进行发送验证码的操作 [self sendVerityAction]; }else { ShowMsg(@"授权失败"); return; } }]; } //发送验证码 - (void)sendVerityAction { NSString * telStr = _phoneStr ? _phoneStr : self.phoneTF.text; ShowHUD(); [SMSSDK getVerificationCodeByMethod:SMSGetCodeMethodSMS phoneNumber:telStr zone:@"86" template:nil result:^(NSError *error) { RemoveHUD(); if (error) { if (error.code == 477) { [self alertActionWithTitle:@"提示" message:@"当天验证次数已超上限" isCanceAction:NO]; }else{ [self alertActionWithTitle:@"提示" message:@"请检查手机号码是否正确" isCanceAction:NO]; } } else { //NSLog(@"验证码已发送"); [self alertActionWithTitle:@"提示" message:@"验证码已发送" isCanceAction:NO]; } }]; } #pragma mark - 检测按钮 //判断验证码是否正确 - (void)verityCodeAction:(UIButton*)sender { if (self.verityTF.text.length == 0) { // NSLog(@"请输入验证码"); [self alertActionWithTitle:@"提示" message:@"请输入验证码" isCanceAction:NO]; return; } if (![Util connectedToNetWork]) { showMsgUnconnect(); return; } //将验证码发送到服务器,判断验证码是否正确 [self checkVerityCode]; } - (void)checkVerityCode { NSString * telStr = _phoneStr.length > 0 ? _phoneStr : self.phoneTF.text; ShowHUD(); [SMSSDK commitVerificationCode:self.verityTF.text phoneNumber:telStr zone:@"86" result:^(NSError *error) { RemoveHUD(); if (error) { ShowMsg(@"验证码错误"); // //说明60秒已经结束并且还没有得到验证码,这时候我们应该将定时器暂停或者销毁 // [self.sendVerityBut setTitle:@"再次发送" forState:UIControlStateNormal]; // //打开用户交互 // self.sendVerityBut.enabled = YES; // //销毁定时器 // [self.timer invalidate]; // self.timer = nil; } else { // NSLog(@"验证成功"); NSLog(@"%@",self.presentingViewController); if ([self.presentingViewController isKindOfClass:[LoginViewController class]]) { Message_Set_VC * setMsgVC = [[Message_Set_VC alloc]init]; setMsgVC.loginCode = self.loginCode; [self navPushHideTabbarToVC:setMsgVC]; return; } Message_Set_VC * setMsgVC = [[Message_Set_VC alloc]init]; setMsgVC.loginCode = self.loginCode; [self navPushHideTabbarToVC:setMsgVC]; } }]; } #pragma mark - 懒加载 - (UITextField*)phoneTF { if (!_phoneTF) { UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 40, kSize.width, 60)]; view.backgroundColor = backGroundColor; [self.view addSubview:view]; [view addViewWithRect:CGRectMake(kSize.width - 91, 50, 1, 40)]; _phoneTF = [[UITextField alloc] initWithFrame:CGRectMake(20, 50, 200, 40)]; //_phoneTF.borderStyle = UITextBorderStyleRoundedRect; _phoneTF.placeholder = @"请输入手机号码"; _phoneTF.keyboardType = UIKeyboardTypeNumberPad; _phoneTF.delegate = self; } return _phoneTF; } - (UITextField *)verityTF { if (!_verityTF) { UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 140, kSize.width, 60)]; view.backgroundColor = backGroundColor; [self.view addSubview:view]; _verityTF = [[UITextField alloc] initWithFrame:CGRectMake(20, 150, kSize.width - 40, 40)]; //_verityTF.borderStyle = UITextBorderStyleRoundedRect; _verityTF.placeholder = @"请输入验证码"; _verityTF.keyboardType = UIKeyboardTypeNumberPad; _verityTF.delegate = self; } return _verityTF; } - (UIButton*)sendVerityBut { if (!_sendVerityBut) { _sendVerityBut = [[UIButton alloc] initWithFrame:CGRectMake(kSize.width - 90, 50, 80, 40)]; //_sendVerityBut.backgroundColor = [UIColor lightGrayColor]; //_sendVerityBut.layer.cornerRadius = 10; [_sendVerityBut setTitle:@"发送" forState:UIControlStateNormal]; [_sendVerityBut setTitleColor:defGreen forState:UIControlStateNormal]; } return _sendVerityBut; } - (UIButton *)verityCodeBut { if (!_verityCodeBut) { _verityCodeBut = [[UIButton alloc] initWithFrame:CGRectMake(20, 240, kSize.width - 40, 40)]; _verityCodeBut.backgroundColor = defGreen; _verityCodeBut.layer.cornerRadius = 10; [_verityCodeBut setTitle:@"确定" forState:UIControlStateNormal]; } return _verityCodeBut; } - (NSTimer*)timer { if (!_timer) { _timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(timerAction) userInfo:nil repeats:YES]; //暂停 [_timer setFireDate:[NSDate distantFuture]]; } return _timer; } //计时器回调方法 - (void)timerAction { //把按钮的标题变为倒计时 self.sumTime--;//每次执行倒计时方法,让总时间-1 // NSString *butTitle = @"";//按钮标题 if (self.sumTime >= 0) { [self.sendVerityBut setTitle:[NSString stringWithFormat:@"%ds",self.sumTime] forState:UIControlStateNormal]; } else { //说明60秒已经结束并且还没有得到验证码,这时候我们应该将定时器暂停或者销毁 [self.sendVerityBut setTitle:@"再次发送" forState:UIControlStateNormal]; //打开用户交互 self.sendVerityBut.enabled = YES; //销毁定时器 [self.timer invalidate]; self.timer = nil; } } #pragma mark - 当视图消失 - (void)viewDidDisappear:(BOOL)animated { [super viewDidDisappear:animated]; self.sumTime = 60; [self.timer invalidate]; self.timer = nil; [self.sendVerityBut setTitle:@"发送" forState:UIControlStateNormal]; // self.phoneTF.text = nil; self.verityTF.text = nil; } #pragma mark - 提示框 - (void)alertActionWithTitle:(NSString*)title message:(NSString*)message isCanceAction:(BOOL)isCancleAction{ self.alertC = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleAlert]; //create the actions if (isCancleAction) { UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) { }]; [self.alertC addAction:cancelAction]; } UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) { }]; [self.alertC addAction:okAction]; [self presentViewController:self.alertC animated:YES completion:nil]; } #pragma mark - 限制输入字符 -(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{ NSCharacterSet *cs; //invertedSet方法是去反字符,把所有的除了NUMBERS 里的字符都找出来(包含去空格功能) cs = [[NSCharacterSet characterSetWithCharactersInString:NUMBERS]invertedSet]; // componentsJoinedByString 用于根据一个字符串来将数组连接成一个新的字符串。 NSString *filtered = [[string componentsSeparatedByCharactersInSet:cs]componentsJoinedByString:@""]; //按cs分离出数组,数组按@""分离出字符串 BOOL canChange = [string isEqualToString:filtered]; if (_verityTF == textField) { return canChange; } return YES; //这样写了以后,输入非数字时不能输入 } #pragma mark 统一跳转 + (void)goSMSFormVC:(UIViewController *)preVC phoneNumber:(NSString *)phoneNum loginCode:(NSString *)aLoginCode canEditNum:(BOOL)canEdit { Message_SMS_VC *smsVC = [[Message_SMS_VC alloc] init]; smsVC.phoneNumber = @""; if (phoneNum.length > 0) { smsVC.phoneNumber = phoneNum; } smsVC.loginCode = @""; if (aLoginCode > 0) { smsVC.loginCode = aLoginCode; } smsVC.isCanEditNum = canEdit; UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:smsVC]; nav.modalTransitionStyle = UIModalTransitionStyleCrossDissolve; [[RQ_SHARE_FUNCTION topViewController] presentViewController:nav animated:NO completion:nil]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } @end