关于ios:iOS如何实现验证码登录丨MobTech

5次阅读

共计 3840 个字符,预计需要花费 10 分钟才能阅读完成。

开发工具:Xcode
集成形式:手动导入 SDK 或者 Pod 集成
SDK 版本反对:SDK 反对 Xcode 9.1.0, iOS8.0+ 及以上版本

展现效果图

编写代码在我的项目中创立登录页面,编写代码

#import "ViewController.h"
#import <SMS_SDK/SMSSDK.h>

@interface ViewController ()<UITextFieldDelegate>{NSInteger _count;}
@property (nonatomic,strong)UITextField *phNumTF;
@property (nonatomic,strong)UITextField *codeTF;
@property (nonatomic,strong)UIButton *getCodeButton;
@property (nonatomic,strong)UIButton *loginButton;
@property (nonatomic,strong)NSTimer *timer;

@end

@implementation ViewController

- (void)viewDidLoad {[super viewDidLoad];

    UILabel *titleLabel = [[UILabel alloc]init];
    titleLabel.frame =  CGRectMake((self.view.frame.size.width-100)/2, 100, 100, 30);
    titleLabel.text = @"登录";
    titleLabel.textAlignment =  NSTextAlignmentCenter;
    titleLabel.font = [UIFont systemFontOfSize:16];
    titleLabel.textColor = [UIColor blackColor];
    [self.view addSubview:titleLabel];

    UILabel *phNumLabel = [[UILabel alloc]init];
    phNumLabel.frame =  CGRectMake(50, titleLabel.frame.origin.y+200, 70, 30);
    phNumLabel.text = @"手机号码";
    phNumLabel.font = [UIFont systemFontOfSize:16];
    phNumLabel.textColor = [UIColor blackColor];
    [self.view addSubview:phNumLabel];

    self.phNumTF = [[UITextField alloc]initWithFrame:CGRectMake(130, phNumLabel.center.y-25, self.view.frame.size.width - 150, 50)];
    self.phNumTF.placeholder = @"请输出手机号码";
    self.phNumTF.textColor = [UIColor blackColor];
    self.phNumTF.font = [UIFont systemFontOfSize:18];
    [self.view addSubview:self.phNumTF];

    UILabel *codeLabel = [[UILabel alloc]init];
    codeLabel.frame =  CGRectMake(50, phNumLabel.frame.origin.y+50, 50, 30);
    codeLabel.text = @"验证码";
    codeLabel.font = [UIFont systemFontOfSize:16];
    codeLabel.textColor = [UIColor blackColor];
    [self.view addSubview:codeLabel];

    self.codeTF = [[UITextField alloc]initWithFrame:CGRectMake(self.phNumTF.frame.origin.x, codeLabel.center.y-25, self.view.frame.size.width - 220, 50)];
    self.codeTF.placeholder = @"请输出的短信验证码";
    self.codeTF.textColor = [UIColor blackColor];
    self.codeTF.font = [UIFont systemFontOfSize:18];
    [self.view addSubview:self.codeTF];

    self.getCodeButton = [UIButton buttonWithType:UIButtonTypeCustom];
    self.getCodeButton.frame = CGRectMake(self.codeTF.frame.origin.y-30, self.codeTF.center.y - 15, 70, 30);
    [self.getCodeButton setTitle:@"获取验证码" forState:UIControlStateNormal];
    [self.getCodeButton setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
    self.getCodeButton.titleLabel.font = [UIFont systemFontOfSize:13];
    [self.getCodeButton addTarget:self action:@selector(getCodeButtonClick) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.getCodeButton];

    self.loginButton = [UIButton buttonWithType:UIButtonTypeCustom];
    self.loginButton.frame = CGRectMake(80, self.codeTF.frame.origin.y+200, self.view.frame.size.width - 160, 50);
    self.loginButton.backgroundColor = [UIColor blueColor];
    [self.loginButton setTitle:@"登录" forState:UIControlStateNormal];
    self.loginButton.titleLabel.font = [UIFont systemFontOfSize:18];
    [self.loginButton addTarget:self action:@selector(loginButtonClick) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.loginButton];



}

// 获取短信验证码
- (void)getCodeButtonClick{
    self.getCodeButton.enabled =NO;
    _count = 120;
    [self.getCodeButton setTitle:@"120s 后从新发送" forState:UIControlStateNormal];
    self.timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timerFired:) userInfo:nil repeats:YES];
}

// 定时器
-(void)timerFired:(NSTimer *)timer
{if (_count !=1) {
        _count -=1;
        [self.getCodeButton setTitle:[NSString stringWithFormat:@"%lds 后从新发送",_count] forState:UIControlStateNormal];
        // 获取短信验证码
        [SMSSDK getVerificationCodeByMethod:SMSGetCodeMethodSMS phoneNumber:self.phNumTF.text zone:@"86" template:@"" result:^(NSError *error) {if (!error)
                {// 申请胜利}
                else
                {// error}
            }];
    } else {[timer invalidate];
        self.getCodeButton.enabled = YES;
        [self.getCodeButton setTitle:@"从新发送" forState:UIControlStateNormal];

    }
}

// 登录
- (void)loginButtonClick{
    // 提交短信验证码
    [SMSSDK commitVerificationCode:self.codeTF.text phoneNumber:self.phNumTF.text zone:@"86"result:^(NSError *error) {if (!error)
            {// 验证胜利}
            else
            {// error}
        }];
}
@end

至此,您已实现了短信验证码登录性能,欢快的游玩吧。

正文完
 0