iOS 如何实现验证码登录丨 MobTech
作者:MobTech袤博科技
- 2022 年 9 月 01 日 上海
本文字数:2428 字
阅读完需:约 8 分钟
开发工具: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
复制代码
至此,您已实现了短信验证码登录功能,愉快的玩耍吧。
划线
评论
复制
发布于: 刚刚阅读数: 5
MobTech袤博科技
关注
还未添加个人签名 2019.05.08 加入
还未添加个人简介
评论