iOS如何实现验证码登录丨MobTech

开发工具: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
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

MobTech袤博科技

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值