Message_SMS_VC.m 11 KB

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