BasePhoneViewController.m 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. //
  2. // BasePhoneViewController.m
  3. // XenonSDK
  4. //
  5. // Created by SAGESSE on 2019/5/29.
  6. // Copyright © 2019 SAGESSE. All rights reserved.
  7. //
  8. #import "XenonSDK.h"
  9. #import "BasePhoneViewController.h"
  10. @interface BasePhoneViewController () <UITextFieldDelegate>
  11. @end
  12. @implementation BasePhoneViewController
  13. - (void)countdown {
  14. NSInteger t = self.endTime - CACurrentMediaTime() + 0.5;
  15. if (t < 0) {
  16. self.sendButton.enabled = true;
  17. [self.timer invalidate];
  18. self.timer = nil;
  19. self.sendButton = nil;
  20. return;
  21. }
  22. [self.sendButton setTitle:[NSString stringWithFormat:@"%zd秒后重试", t]
  23. forState:UIControlStateDisabled];
  24. }
  25. /*发送验证码**/
  26. - (void)startCounter {
  27. // Create a timer.
  28. NSTimer* timer = [NSTimer scheduledTimerWithTimeInterval:0.5 target:self selector:@selector(countdown) userInfo:nil repeats:YES];
  29. self.endTime = CACurrentMediaTime() + 60;
  30. self.sendButton.enabled = NO;
  31. self.timer = timer;
  32. }
  33. - (void)viewDidLoad {
  34. [super viewDidLoad];
  35. // Do any additional setup after loading the view.
  36. }
  37. - (void)viewDidAppear:(BOOL)animated {
  38. [super viewDidAppear:animated];
  39. if (self.sendButton != nil) {
  40. [self startCounter];
  41. }
  42. }
  43. - (void)viewDidDisappear:(BOOL)animated {
  44. [super viewDidDisappear:animated];
  45. [self.timer invalidate];
  46. self.timer = nil;
  47. }
  48. - (void)securityCodeForPrepare:(UIButton*)sender {
  49. // Resign editing.
  50. [self.view endEditing:YES];
  51. // Must enter phone.
  52. NSString* phone = self.phoneTextField.text;
  53. if (phone.length == 0) {
  54. [XSNetwork showHudFailure:@"请输入手机号"];
  55. return;
  56. }
  57. // Check the phone is vaild.
  58. if (!sdk_verifyPhone(phone)) {
  59. [XSNetwork showHudFailure:@"请输入正确的手机号格式"];
  60. return;
  61. }
  62. // Send SMS security code.
  63. [XSNetwork sendSMSWithPhone:phone type:self.smsType complete:^(id object, NSError *error) {
  64. if (error != nil) {
  65. [XSNetwork showHudFailure:error];
  66. return;
  67. }
  68. // Start typing security code.
  69. self.sendButton = sender;
  70. [self.securityCodeTextField becomeFirstResponder];
  71. [self startCounter];
  72. [XSNetwork showHudSuccess:@"验证码发送成功"];
  73. }];
  74. }
  75. - (void)securityCodeForVerify:(id)sender {
  76. // Resign editing.
  77. [self.view endEditing:YES];
  78. // Some thing.
  79. }
  80. - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
  81. if (textField == self.phoneTextField && [string hasPrefix:@"+"]) {
  82. id str = [string stringByTrimmingCharactersInSet:NSCharacterSet.decimalDigitCharacterSet.invertedSet];
  83. textField.text = [str substringFromIndex:11];
  84. return NO;
  85. }
  86. return YES;
  87. }
  88. - (BOOL)textFieldShouldReturn:(UITextField *)textField {
  89. if (textField.returnKeyType == UIReturnKeyDone) {
  90. if (textField == self.securityCodeTextField) {
  91. [self securityCodeForVerify:textField];
  92. }
  93. return false;
  94. }
  95. if (textField == self.phoneTextField) {
  96. [self.securityCodeTextField becomeFirstResponder];
  97. }
  98. return YES;
  99. }
  100. @end