Message_SMS_VC.m 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  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 configNavigationBar];
  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. // self.phoneNumber = @"13017654923";
  38. _phoneTF.text = _phoneNumber;
  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. //判断用户是否输入手机号码 手机号码是否正确
  62. if(self.phoneTF.text.length == 0) {
  63. [self alertActionWithTitle:@"提示" message:@"请输入手机号码" isCanceAction:NO];
  64. return;
  65. }
  66. if (self.phoneTF.text.length != 11) {
  67. // NSLog(@"电话号码输入有误");
  68. [self alertActionWithTitle:@"提示" message:@"电话号码输入有误" isCanceAction:NO];
  69. return;
  70. }
  71. if (![Util connectedToNetWork]) {
  72. showMsgUnconnect();
  73. return;
  74. }
  75. //每次将要发送验证码的时候需要将总时间变为60秒
  76. self.sumTime = 60;
  77. //关闭按钮的交互
  78. self.sendVerityBut.enabled = NO;
  79. //打开计时器
  80. [self.timer setFireDate:[NSDate distantPast]];
  81. //说明手机号码正确,可以进行发送验证码的操作
  82. [self sendVerityAction];
  83. }];
  84. }
  85. //发送验证码
  86. - (void)sendVerityAction {
  87. // NSString * telStr = _phoneStr ? _phoneStr : self.phoneTF.text;
  88. // [MBProgressHUD showLoadToView:self.view];
  89. // [SMSSDK getVerificationCodeByMethod:SMSGetCodeMethodSMS phoneNumber:telStr zone:@"86" template:nil result:^(NSError *error) {
  90. //
  91. // [MBProgressHUD hideHUDForView:self.view];
  92. // if (error) {
  93. // if (error.code == 477) {
  94. // [self alertActionWithTitle:@"提示" message:@"当天验证次数已超上限" isCanceAction:NO];
  95. // }else{
  96. // if (error.description) {
  97. // if (![error.description isEqualToString:@""]) {
  98. // [self alertActionWithTitle:@"提示" message:error.description isCanceAction:NO];
  99. // }else {
  100. // [self alertActionWithTitle:@"提示" message:@"请检查手机号码是否正确" isCanceAction:NO];
  101. // }
  102. // }else {
  103. // [self alertActionWithTitle:@"提示" message:@"请检查手机号码是否正确" isCanceAction:NO];
  104. // }
  105. //
  106. // }
  107. //
  108. // } else {
  109. // //NSLog(@"验证码已发送");
  110. // [self alertActionWithTitle:@"提示" message:@"验证码已发送" isCanceAction:NO];
  111. // }
  112. // }];
  113. }
  114. #pragma mark - 检测按钮
  115. //判断验证码是否正确
  116. - (void)verityCodeAction:(UIButton*)sender {
  117. if (self.verityTF.text.length == 0) {
  118. // NSLog(@"请输入验证码");
  119. [self alertActionWithTitle:@"提示" message:@"请输入验证码" isCanceAction:NO];
  120. return;
  121. }
  122. if (![Util connectedToNetWork]) {
  123. showMsgUnconnect();
  124. return;
  125. }
  126. //将验证码发送到服务器,判断验证码是否正确
  127. [self checkVerityCode];
  128. }
  129. - (void)checkVerityCode {
  130. // NSString * telStr = _phoneStr.length > 0 ? _phoneStr : self.phoneTF.text;
  131. // [MBProgressHUD showLoadToView:self.view];
  132. // [SMSSDK commitVerificationCode:self.verityTF.text phoneNumber:telStr zone:@"86" result:^(NSError *error) {
  133. //
  134. // [MBProgressHUD hideHUDForView:self.view];
  135. // if (error) {
  136. // ShowMsg(@"验证码错误");
  137. //// //说明60秒已经结束并且还没有得到验证码,这时候我们应该将定时器暂停或者销毁
  138. //// [self.sendVerityBut setTitle:@"再次发送" forState:UIControlStateNormal];
  139. //// //打开用户交互
  140. //// self.sendVerityBut.enabled = YES;
  141. //// //销毁定时器
  142. //// [self.timer invalidate];
  143. //// self.timer = nil;
  144. // } else {
  145. //// NSLog(@"验证成功");
  146. // if ([self.presentingViewController isKindOfClass:[LoginViewController class]]) {
  147. // Message_Set_VC * setMsgVC = [[Message_Set_VC alloc]init];
  148. // setMsgVC.loginCode = self.loginCode;
  149. // [self navPushHideTabbarToVC:setMsgVC];
  150. // }
  151. //
  152. //
  153. // }
  154. // }];
  155. }
  156. #pragma mark - 懒加载
  157. - (UITextField*)phoneTF {
  158. if (!_phoneTF) {
  159. UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 40, kSize.width, 60)];
  160. view.backgroundColor = [UIColor whiteColor];
  161. [self.view addSubview:view];
  162. [view addViewWithRect:CGRectMake(kSize.width - 91, 50, 1, 40)];
  163. _phoneTF = [[UITextField alloc] initWithFrame:CGRectMake(20, 50, 200, 40)];
  164. //_phoneTF.borderStyle = UITextBorderStyleRoundedRect;
  165. _phoneTF.placeholder = @"请输入手机号码";
  166. _phoneTF.keyboardType = UIKeyboardTypeNumberPad;
  167. _phoneTF.delegate = self;
  168. }
  169. return _phoneTF;
  170. }
  171. - (UITextField *)verityTF {
  172. if (!_verityTF) {
  173. UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 140, kSize.width, 60)];
  174. view.backgroundColor = [UIColor whiteColor];
  175. [self.view addSubview:view];
  176. _verityTF = [[UITextField alloc] initWithFrame:CGRectMake(20, 150, kSize.width - 40, 40)];
  177. //_verityTF.borderStyle = UITextBorderStyleRoundedRect;
  178. _verityTF.placeholder = @"请输入验证码";
  179. _verityTF.keyboardType = UIKeyboardTypeNumberPad;
  180. _verityTF.delegate = self;
  181. }
  182. return _verityTF;
  183. }
  184. - (UIButton*)sendVerityBut {
  185. if (!_sendVerityBut) {
  186. _sendVerityBut = [[UIButton alloc] initWithFrame:CGRectMake(kSize.width - 90, 50, 80, 40)];
  187. //_sendVerityBut.backgroundColor = [UIColor lightGrayColor];
  188. //_sendVerityBut.layer.cornerRadius = 10;
  189. [_sendVerityBut setTitle:@"发送" forState:UIControlStateNormal];
  190. [_sendVerityBut setTitleColor:defGreen forState:UIControlStateNormal];
  191. }
  192. return _sendVerityBut;
  193. }
  194. - (UIButton *)verityCodeBut {
  195. if (!_verityCodeBut) {
  196. _verityCodeBut = [[UIButton alloc] initWithFrame:CGRectMake(20, 240, kSize.width - 40, 40)];
  197. _verityCodeBut.backgroundColor = defGreen;
  198. _verityCodeBut.layer.cornerRadius = 10;
  199. [_verityCodeBut setTitle:@"确定" forState:UIControlStateNormal];
  200. }
  201. return _verityCodeBut;
  202. }
  203. - (NSTimer*)timer {
  204. if (!_timer) {
  205. _timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(timerAction) userInfo:nil repeats:YES];
  206. //暂停
  207. [_timer setFireDate:[NSDate distantFuture]];
  208. }
  209. return _timer;
  210. }
  211. //计时器回调方法
  212. - (void)timerAction {
  213. //把按钮的标题变为倒计时
  214. self.sumTime--;//每次执行倒计时方法,让总时间-1
  215. // NSString *butTitle = @"";//按钮标题
  216. if (self.sumTime >= 0) {
  217. [self.sendVerityBut setTitle:[NSString stringWithFormat:@"%ds",self.sumTime] forState:UIControlStateNormal];
  218. } else {
  219. //说明60秒已经结束并且还没有得到验证码,这时候我们应该将定时器暂停或者销毁
  220. [self.sendVerityBut setTitle:@"再次发送" forState:UIControlStateNormal];
  221. //打开用户交互
  222. self.sendVerityBut.enabled = YES;
  223. //销毁定时器
  224. [self.timer invalidate];
  225. self.timer = nil;
  226. }
  227. }
  228. #pragma mark - 当视图消失
  229. - (void)viewDidDisappear:(BOOL)animated {
  230. [super viewDidDisappear:animated];
  231. self.sumTime = 60;
  232. [self.timer invalidate];
  233. self.timer = nil;
  234. [self.sendVerityBut setTitle:@"发送" forState:UIControlStateNormal];
  235. // self.phoneTF.text = nil;
  236. self.verityTF.text = nil;
  237. }
  238. #pragma mark - 提示框
  239. - (void)alertActionWithTitle:(NSString*)title message:(NSString*)message isCanceAction:(BOOL)isCancleAction{
  240. self.alertC = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleAlert];
  241. //create the actions
  242. if (isCancleAction) {
  243. UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
  244. }];
  245. [self.alertC addAction:cancelAction];
  246. }
  247. UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
  248. }];
  249. [self.alertC addAction:okAction];
  250. [self presentViewController:self.alertC animated:YES completion:nil];
  251. }
  252. #pragma mark - 限制输入字符
  253. -(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
  254. NSCharacterSet *cs;
  255. //invertedSet方法是去反字符,把所有的除了NUMBERS 里的字符都找出来(包含去空格功能)
  256. cs = [[NSCharacterSet characterSetWithCharactersInString:NUMBERS]invertedSet];
  257. // componentsJoinedByString 用于根据一个字符串来将数组连接成一个新的字符串。
  258. NSString *filtered = [[string componentsSeparatedByCharactersInSet:cs]componentsJoinedByString:@""]; //按cs分离出数组,数组按@""分离出字符串
  259. BOOL canChange = [string isEqualToString:filtered];
  260. if (_verityTF == textField) {
  261. return canChange;
  262. }
  263. return YES;
  264. //这样写了以后,输入非数字时不能输入
  265. }
  266. #pragma mark 统一跳转
  267. + (void)goSMSFormVC:(UIViewController *)preVC phoneNumber:(NSString *)phoneNum loginCode:(NSString *)aLoginCode canEditNum:(BOOL)canEdit
  268. {
  269. Message_SMS_VC *smsVC = [[Message_SMS_VC alloc] init];
  270. smsVC.phoneNumber = @"";
  271. if (phoneNum.length > 0) {
  272. smsVC.phoneNumber = phoneNum;
  273. }
  274. smsVC.loginCode = @"";
  275. if (aLoginCode > 0) {
  276. smsVC.loginCode = aLoginCode;
  277. }
  278. smsVC.isCanEditNum = canEdit;
  279. UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:smsVC];
  280. nav.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
  281. nav.modalPresentationStyle = UIModalPresentationFullScreen;
  282. [preVC presentViewController:nav animated:YES completion:nil];
  283. }
  284. - (void)didReceiveMemoryWarning {
  285. [super didReceiveMemoryWarning];
  286. // Dispose of any resources that can be recreated.
  287. }
  288. @end