123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130 |
- //
- // BasePhoneViewController.m
- // XenonSDK
- //
- // Created by SAGESSE on 2019/5/29.
- // Copyright © 2019 SAGESSE. All rights reserved.
- //
- #import "XenonSDK.h"
- #import "BasePhoneViewController.h"
- @interface BasePhoneViewController () <UITextFieldDelegate>
- @end
- @implementation BasePhoneViewController
- - (void)countdown {
-
- NSInteger t = self.endTime - CACurrentMediaTime() + 0.5;
- if (t < 0) {
- self.sendButton.enabled = true;
- [self.timer invalidate];
- self.timer = nil;
- self.sendButton = nil;
- return;
- }
- [self.sendButton setTitle:[NSString stringWithFormat:@"%zd秒后重试", t]
- forState:UIControlStateDisabled];
- }
- /*发送验证码**/
- - (void)startCounter {
-
- // Create a timer.
- NSTimer* timer = [NSTimer scheduledTimerWithTimeInterval:0.5 target:self selector:@selector(countdown) userInfo:nil repeats:YES];
-
- self.endTime = CACurrentMediaTime() + 60;
- self.sendButton.enabled = NO;
- self.timer = timer;
- }
- - (void)viewDidLoad {
- [super viewDidLoad];
- // Do any additional setup after loading the view.
- }
- - (void)viewDidAppear:(BOOL)animated {
- [super viewDidAppear:animated];
-
- if (self.sendButton != nil) {
- [self startCounter];
- }
- }
- - (void)viewDidDisappear:(BOOL)animated {
- [super viewDidDisappear:animated];
-
- [self.timer invalidate];
- self.timer = nil;
- }
- - (void)securityCodeForPrepare:(UIButton*)sender {
- // Resign editing.
- [self.view endEditing:YES];
-
- // Must enter phone.
- NSString* phone = self.phoneTextField.text;
- if (phone.length == 0) {
- [XSNetwork showHudFailure:@"请输入手机号"];
- return;
- }
-
- // Check the phone is vaild.
- if (!sdk_verifyPhone(phone)) {
- [XSNetwork showHudFailure:@"请输入正确的手机号格式"];
- return;
- }
-
- // Send SMS security code.
- [XSNetwork sendSMSWithPhone:phone type:self.smsType complete:^(id object, NSError *error) {
- if (error != nil) {
- [XSNetwork showHudFailure:error];
- return;
- }
-
- // Start typing security code.
- self.sendButton = sender;
- [self.securityCodeTextField becomeFirstResponder];
- [self startCounter];
-
- [XSNetwork showHudSuccess:@"验证码发送成功"];
- }];
- }
- - (void)securityCodeForVerify:(id)sender {
- // Resign editing.
- [self.view endEditing:YES];
-
- // Some thing.
- }
- - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
- if (textField == self.phoneTextField && [string hasPrefix:@"+"]) {
- id str = [string stringByTrimmingCharactersInSet:NSCharacterSet.decimalDigitCharacterSet.invertedSet];
- textField.text = [str substringFromIndex:11];
- return NO;
- }
- return YES;
- }
- - (BOOL)textFieldShouldReturn:(UITextField *)textField {
-
- if (textField.returnKeyType == UIReturnKeyDone) {
- if (textField == self.securityCodeTextField) {
- [self securityCodeForVerify:textField];
- }
- return false;
- }
-
- if (textField == self.phoneTextField) {
- [self.securityCodeTextField becomeFirstResponder];
- }
-
- return YES;
- }
- @end
|