iOS如何实现验证码登录丨MobTech
开发工具:Xcode展示效果图
集成方式:手动导入SDK或者Pod集成
SDK版本支持:SDK支持Xcode 9.1.0, iOS8.0+及以上版本
文章图片
编写代码在项目中创建登录页面,编写代码
#import "ViewController.h"
#import @interface ViewController (){
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
【iOS如何实现验证码登录丨MobTech】至此,您已实现了短信验证码登录功能,愉快的玩耍吧。
推荐阅读
- 拆书2-5
- 如何在保护用户隐私的同时实现精准广告投放()
- 前端高频面试题(六)(附答案)
- 证券|2022上半年券商成绩单出炉:锦龙股份营收为负,仅6家券商归母净利实现正增长
- Deep|Deep Learning中如何选择GPU((一))
- 读|读 大学该如何度过 之想
- 汽车技术|谁能想到最先实现城市NOH量产的居然是魏牌?
- 如何安全快速地从|如何安全快速地从 Centos迁移到openEuler
- android|Android Studio 实现简易的个人信息录取(含源码)
- android|android studio 健身软件,基于Android的健身助手设计与实现.doc