Message_SMS_VC.m 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. //
  2. // Message_SMS_VC.m
  3. // jiaPei
  4. //
  5. // Created by apple on 2017/1/9.
  6. // Copyright © 2017年 JCZ. All rights reserved.
  7. //
  8. #import "Message_SMS_VC.h"
  9. #import "LoginViewController.h"
  10. #import "Message_Set_VC.h"
  11. //这个代表可以输入数字和换行,请注意这个n,如果不写这个,Done按键将不会触发,如果用在SearchBar中,将会不触发Search事件
  12. #define NUMBERS @"0123456789n"
  13. @interface Message_SMS_VC ()<UITextFieldDelegate>
  14. {
  15. //记住变星号前的字符串
  16. NSString * _phoneStr;
  17. }
  18. @end
  19. @implementation Message_SMS_VC
  20. - (void)viewDidLoad {
  21. [super viewDidLoad];
  22. self.title = @"手机号验证";
  23. self.view.backgroundColor = backGroundColor;
  24. [self configNavigationBarDismissNav];
  25. //将初始总时间设置为60妙
  26. self.sumTime = 60;
  27. [self.view addSubview:self.phoneTF];
  28. [self.view addSubview:self.verityTF];
  29. [self.view addSubview:self.sendVerityBut];
  30. [self.view addSubview:self.verityCodeBut];
  31. [self.sendVerityBut addTarget:self action:@selector(VerityAction) forControlEvents:UIControlEventTouchUpInside];
  32. [self.verityCodeBut addTarget:self action:@selector(verityCodeAction:) forControlEvents:UIControlEventTouchUpInside];
  33. //对传入手机号码的处理
  34. if (!_isCanEditNum) {
  35. _phoneTF.enabled = NO;
  36. }
  37. _phoneTF.text = _phoneNumber;
  38. //_phoneTF.text = @"15188352230";
  39. if (_phoneTF.text.length > 0) {
  40. _phoneStr = _phoneTF.text;
  41. _phoneTF.text = [_phoneTF.text stringByReplacingCharactersInRange:NSMakeRange(3, 4) withString:@"****"];
  42. }
  43. }
  44. //-(void)backBtnAction
  45. //{
  46. // [self dismissViewControllerAnimated:YES completion:nil];
  47. //}
  48. -(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
  49. {
  50. [super touchesBegan:touches withEvent:event];
  51. if ([_phoneTF isFirstResponder]) {
  52. [_phoneTF resignFirstResponder];
  53. }
  54. if ([_verityTF isFirstResponder]) {
  55. [_verityTF resignFirstResponder];
  56. }
  57. }
  58. #pragma mark - 发送按钮
  59. - (void)VerityAction {
  60. [RQ_SHARE_FUNCTION checkMobPolicyOnResult:^(BOOL success) {
  61. if (success) {
  62. //判断用户是否输入手机号码 手机号码是否正确
  63. if(self.phoneTF.text.length == 0) {
  64. [self alertActionWithTitle:@"提示" message:@"请输入手机号码" isCanceAction:NO];
  65. return;
  66. }
  67. if (self.phoneTF.text.length != 11) {
  68. // NSLog(@"电话号码输入有误");
  69. [self alertActionWithTitle:@"提示" message:@"电话号码输入有误" isCanceAction:NO];
  70. return;
  71. }
  72. if (![Util connectedToNetWork]) {
  73. showMsgUnconnect();
  74. return;
  75. }
  76. //每次将要发送验证码的时候需要将总时间变为60秒
  77. self.sumTime = 60;
  78. //关闭按钮的交互
  79. self.sendVerityBut.enabled = NO;
  80. //打开计时器
  81. [self.timer setFireDate:[NSDate distantPast]];
  82. //说明手机号码正确,可以进行发送验证码的操作
  83. [self sendVerityAction];
  84. }else {
  85. ShowMsg(@"授权失败");
  86. return;
  87. }
  88. }];
  89. }
  90. //发送验证码
  91. - (void)sendVerityAction {
  92. NSString * telStr = _phoneStr ? _phoneStr : self.phoneTF.text;
  93. ShowHUD();
  94. [SMSSDK getVerificationCodeByMethod:SMSGetCodeMethodSMS phoneNumber:telStr zone:@"86" template:nil result:^(NSError *error) {
  95. RemoveHUD();
  96. if (error) {
  97. if (error.code == 477) {
  98. [self alertActionWithTitle:@"提示" message:@"当天验证次数已超上限" isCanceAction:NO];
  99. }else{
  100. [self alertActionWithTitle:@"提示" message:@"请检查手机号码是否正确" isCanceAction:NO];
  101. }
  102. } else {
  103. //NSLog(@"验证码已发送");
  104. [self alertActionWithTitle:@"提示" message:@"验证码已发送" isCanceAction:NO];
  105. }
  106. }];
  107. }
  108. #pragma mark - 检测按钮
  109. //判断验证码是否正确
  110. - (void)verityCodeAction:(UIButton*)sender {
  111. if (self.verityTF.text.length == 0) {
  112. // NSLog(@"请输入验证码");
  113. [self alertActionWithTitle:@"提示" message:@"请输入验证码" isCanceAction:NO];
  114. return;
  115. }
  116. if (![Util connectedToNetWork]) {
  117. showMsgUnconnect();
  118. return;
  119. }
  120. //将验证码发送到服务器,判断验证码是否正确
  121. [self checkVerityCode];
  122. }
  123. - (void)checkVerityCode {
  124. NSString * telStr = _phoneStr.length > 0 ? _phoneStr : self.phoneTF.text;
  125. ShowHUD();
  126. [SMSSDK commitVerificationCode:self.verityTF.text phoneNumber:telStr zone:@"86" result:^(NSError *error) {
  127. RemoveHUD();
  128. if (error) {
  129. ShowMsg(@"验证码错误");
  130. // //说明60秒已经结束并且还没有得到验证码,这时候我们应该将定时器暂停或者销毁
  131. // [self.sendVerityBut setTitle:@"再次发送" forState:UIControlStateNormal];
  132. // //打开用户交互
  133. // self.sendVerityBut.enabled = YES;
  134. // //销毁定时器
  135. // [self.timer invalidate];
  136. // self.timer = nil;
  137. } else {
  138. // NSLog(@"验证成功");
  139. NSLog(@"%@",self.presentingViewController);
  140. if ([self.presentingViewController isKindOfClass:[LoginViewController class]]) {
  141. Message_Set_VC * setMsgVC = [[Message_Set_VC alloc]init];
  142. setMsgVC.loginCode = self.loginCode;
  143. [self navPushHideTabbarToVC:setMsgVC];
  144. return;
  145. }
  146. Message_Set_VC * setMsgVC = [[Message_Set_VC alloc]init];
  147. setMsgVC.loginCode = self.loginCode;
  148. [self navPushHideTabbarToVC:setMsgVC];
  149. }
  150. }];
  151. }
  152. #pragma mark - 懒加载
  153. - (UITextField*)phoneTF {
  154. if (!_phoneTF) {
  155. UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 40, kSize.width, 60)];
  156. view.backgroundColor = backGroundColor;
  157. [self.view addSubview:view];
  158. [view addViewWithRect:CGRectMake(kSize.width - 91, 50, 1, 40)];
  159. _phoneTF = [[UITextField alloc] initWithFrame:CGRectMake(20, 50, 200, 40)];
  160. //_phoneTF.borderStyle = UITextBorderStyleRoundedRect;
  161. _phoneTF.placeholder = @"请输入手机号码";
  162. _phoneTF.keyboardType = UIKeyboardTypeNumberPad;
  163. _phoneTF.delegate = self;
  164. }
  165. return _phoneTF;
  166. }
  167. - (UITextField *)verityTF {
  168. if (!_verityTF) {
  169. UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 140, kSize.width, 60)];
  170. view.backgroundColor = backGroundColor;
  171. [self.view addSubview:view];
  172. _verityTF = [[UITextField alloc] initWithFrame:CGRectMake(20, 150, kSize.width - 40, 40)];
  173. //_verityTF.borderStyle = UITextBorderStyleRoundedRect;
  174. _verityTF.placeholder = @"请输入验证码";
  175. _verityTF.keyboardType = UIKeyboardTypeNumberPad;
  176. _verityTF.delegate = self;
  177. }
  178. return _verityTF;
  179. }
  180. - (UIButton*)sendVerityBut {
  181. if (!_sendVerityBut) {
  182. _sendVerityBut = [[UIButton alloc] initWithFrame:CGRectMake(kSize.width - 90, 50, 80, 40)];
  183. //_sendVerityBut.backgroundColor = [UIColor lightGrayColor];
  184. //_sendVerityBut.layer.cornerRadius = 10;
  185. [_sendVerityBut setTitle:@"发送" forState:UIControlStateNormal];
  186. [_sendVerityBut setTitleColor:defGreen forState:UIControlStateNormal];
  187. }
  188. return _sendVerityBut;
  189. }
  190. - (UIButton *)verityCodeBut {
  191. if (!_verityCodeBut) {
  192. _verityCodeBut = [[UIButton alloc] initWithFrame:CGRectMake(20, 240, kSize.width - 40, 40)];
  193. _verityCodeBut.backgroundColor = defGreen;
  194. _verityCodeBut.layer.cornerRadius = 10;
  195. [_verityCodeBut setTitle:@"确定" forState:UIControlStateNormal];
  196. }
  197. return _verityCodeBut;
  198. }
  199. - (NSTimer*)timer {
  200. if (!_timer) {
  201. _timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(timerAction) userInfo:nil repeats:YES];
  202. //暂停
  203. [_timer setFireDate:[NSDate distantFuture]];
  204. }
  205. return _timer;
  206. }
  207. //计时器回调方法
  208. - (void)timerAction {
  209. //把按钮的标题变为倒计时
  210. self.sumTime--;//每次执行倒计时方法,让总时间-1
  211. // NSString *butTitle = @"";//按钮标题
  212. if (self.sumTime >= 0) {
  213. [self.sendVerityBut setTitle:[NSString stringWithFormat:@"%ds",self.sumTime] forState:UIControlStateNormal];
  214. } else {
  215. //说明60秒已经结束并且还没有得到验证码,这时候我们应该将定时器暂停或者销毁
  216. [self.sendVerityBut setTitle:@"再次发送" forState:UIControlStateNormal];
  217. //打开用户交互
  218. self.sendVerityBut.enabled = YES;
  219. //销毁定时器
  220. [self.timer invalidate];
  221. self.timer = nil;
  222. }
  223. }
  224. #pragma mark - 当视图消失
  225. - (void)viewDidDisappear:(BOOL)animated {
  226. [super viewDidDisappear:animated];
  227. self.sumTime = 60;
  228. [self.timer invalidate];
  229. self.timer = nil;
  230. [self.sendVerityBut setTitle:@"发送" forState:UIControlStateNormal];
  231. // self.phoneTF.text = nil;
  232. self.verityTF.text = nil;
  233. }
  234. #pragma mark - 提示框
  235. - (void)alertActionWithTitle:(NSString*)title message:(NSString*)message isCanceAction:(BOOL)isCancleAction{
  236. self.alertC = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleAlert];
  237. //create the actions
  238. if (isCancleAction) {
  239. UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
  240. }];
  241. [self.alertC addAction:cancelAction];
  242. }
  243. UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
  244. }];
  245. [self.alertC addAction:okAction];
  246. [self presentViewController:self.alertC animated:YES completion:nil];
  247. }
  248. #pragma mark - 限制输入字符
  249. -(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
  250. NSCharacterSet *cs;
  251. //invertedSet方法是去反字符,把所有的除了NUMBERS 里的字符都找出来(包含去空格功能)
  252. cs = [[NSCharacterSet characterSetWithCharactersInString:NUMBERS]invertedSet];
  253. // componentsJoinedByString 用于根据一个字符串来将数组连接成一个新的字符串。
  254. NSString *filtered = [[string componentsSeparatedByCharactersInSet:cs]componentsJoinedByString:@""]; //按cs分离出数组,数组按@""分离出字符串
  255. BOOL canChange = [string isEqualToString:filtered];
  256. if (_verityTF == textField) {
  257. return canChange;
  258. }
  259. return YES;
  260. //这样写了以后,输入非数字时不能输入
  261. }
  262. #pragma mark 统一跳转
  263. + (void)goSMSFormVC:(UIViewController *)preVC phoneNumber:(NSString *)phoneNum loginCode:(NSString *)aLoginCode canEditNum:(BOOL)canEdit
  264. {
  265. Message_SMS_VC *smsVC = [[Message_SMS_VC alloc] init];
  266. smsVC.phoneNumber = @"";
  267. if (phoneNum.length > 0) {
  268. smsVC.phoneNumber = phoneNum;
  269. }
  270. smsVC.loginCode = @"";
  271. if (aLoginCode > 0) {
  272. smsVC.loginCode = aLoginCode;
  273. }
  274. smsVC.isCanEditNum = canEdit;
  275. UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:smsVC];
  276. nav.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
  277. [[RQ_SHARE_FUNCTION topViewController] presentViewController:nav animated:NO completion:nil];
  278. }
  279. - (void)didReceiveMemoryWarning {
  280. [super didReceiveMemoryWarning];
  281. // Dispose of any resources that can be recreated.
  282. }
  283. @end