iOS开发--搞定烦躁的验证码

我们开发的爱啪啪里面几乎都有登陆注册系统,这时候就少不了验证码的发挥了。每次都得写这些重复的代码,没有营养又不得不写。今天下班时间,将验证码这一功能封装一下。
准备:Mob -- SDK
我们公司采用的短信验证系统时候Mob的SDK。
所以下面代码出现的网络请求都是Mob SDK 中的方法。
首先看下Mob 给的代码示例:

/**
 *  @from                    v1.1.1
 *  @brief                   获取验证码(Get verification code)
 *
 *  @param method            获取验证码的方法(The method of getting verificationCode)
 *  @param phoneNumber       电话号码(The phone number)
 *  @param zone              区域号,不要加"+"号(Area code)
 *  @param customIdentifier  自定义短信模板标识 该标识需从官网http://www.mob.com上申请,审核通过后获得。(Custom model of SMS.  The identifier can get it  from http://www.mob.com  when the application had approved)
 *  @param result            请求结果回调(Results of the request)
 */

[SMSSDK getVerificationCodeByMethod:SMSGetCodeMethodSMS phoneNumber:@"159****1689"
                                                               zone:@"86"
                                                   customIdentifier:nil
                                                             result:^(NSError *error){
       if (!error) {
            NSLog(@"获取验证码成功");
        } else {
            NSLog(@"错误信息:%@",error);
        }];




[SMSSDK commitVerificationCode:self.verifyCodeField.text phoneNumber:_phone zone:_areaCode result:^(NSError *error) {
 
            if (!error) {
                NSLog(@"验证成功");
            }
            else
            {
                NSLog(@"错误信息:%@",error);
            }
        }];

好了,准备活动做完了,我们来理一理这个验证码怎么做。

准备的类:UIButton NSTimer
其实实现这个功能有很多方法,思路也是很清楚
在用户 按下 获取验证码的时候,令button 的title 倒计时改变(NSTimer)并令button userInteractionEnabled = NO 当计时结束的时候 释放计时器,并且button的title改成原本的样子和userInteractionEnabled = YES
实现起来很简单。
不过我们怎么封装这个验证码的方法呢。
1.自定义button类
2.使用button的Category 来实现封装这个功能。(楼主 采用这种方法)
虽然在button 的 结构里搞了一些网络请求,有点不合理。但整体上,实际效果是不错的。

我们来看下具体的操作
h文件

#import <UIKit/UIKit.h>

typedef void(^handelBlock)(NSError *error);
@interface UIButton (VerificationCode)

//获取验证码
- (void)getCode:(NSString *)phone
          block:(handelBlock)block;
//验证验证码
- (void)VerificationCode:(NSString *)phone
                    Code:(NSString *)code
                   block:(handelBlock)block;
@end

m文件

#import "UIButton+VerificationCode.h"
#import <SMS_SDK/SMSSDK.h>
#import <objc/runtime.h>

const NSString *beginTimeKey;
const NSString *timerKey;
@implementation UIButton (VerificationCode)

#pragma mark --  public
//获取验证码
- (void)getCode:(NSString *)phone
          block:(handelBlock)block {
    self.userInteractionEnabled = NO;
    [SMSSDK getVerificationCodeByMethod:SMSGetCodeMethodSMS phoneNumber:phone zone:@"86" customIdentifier:nil result:^(NSError *error) {
        block(error);
    }];
    [self configureTimer];
     
}
//验证验证码
- (void)VerificationCode:(NSString *)phone
                    Code:(NSString *)code
                   block:(handelBlock)block {
    [SMSSDK commitVerificationCode:code  phoneNumber:phone zone:@"86" result:^(NSError *error) {
        block(error);
    }];

}

#pragma mark 计时器
- (void)configureTimer {
    NSInteger beginTime = 60;

    NSTimer *timer =[NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timeClock) userInfo:nil repeats:YES];
    [[NSRunLoop currentRunLoop] addTimer:timer forMode:NSRunLoopCommonModes];

    [self setTimer:timer];
    [self setBeginTime:beginTime];

}
- (void)timeClock {
    [self setBeginTime:([self beginTime]-1)];
    NSInteger beginTime = [self beginTime];
    NSTimer *timer = [self timer];
    if (beginTime == 0) {
        [timer invalidate];
        self.userInteractionEnabled = YES;
        [self setTitle:@"获取验证码" forState:UIControlStateNormal];

    }else {
        self.userInteractionEnabled = NO;
        [self setTitle:[NSString stringWithFormat:@"%lds",(long)beginTime] forState:UIControlStateNormal];
    }
    
    
}
#pragma mark -- set get
- (NSInteger)beginTime {
    return [objc_getAssociatedObject(self, &beginTimeKey) integerValue];
}
- (void)setBeginTime:(NSInteger)beginTime {
    objc_setAssociatedObject(self, &beginTimeKey, @(beginTime), OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
- (NSTimer *)timer {
    return objc_getAssociatedObject(self, &timerKey);
}
- (void)setTimer:(NSTimer *)timer {
     objc_setAssociatedObject(self, &timerKey, timer, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
@end

这边验证验证码的方法放在button的类目里面,有点不是很合理。但如果分散了其他代码里面,又很恶心。多以索性就一起放在这个类目里面。用起来挺爽哒。

222.gif

让我们一起来消灭重复没有营养的代码吧0.0

代码下载-- github地址:https://github.com/dongqihouse/VerificationCode.git

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 176,099评论 25 709
  • 谨以此文,献给天下已经远行的父亲 你们在比远方更远的地方,是否依然安好 又是父亲节,总有一些孩子如我,惊觉父亲已孑...
    风雨侠客阅读 2,958评论 2 2
  • 对音乐无热爱,平时也听听流行歌曲和那些说不上来节奏的所谓摇滚,而对于外文流行歌曲更是只听名字,无奈心听第二遍,在英...
    DDM兜兜麦阅读 1,387评论 0 0
  • 美国华盛顿邮报最近评选出十大奢侈品:竟然无一与物质有关。 1.生命的觉悟。 2.一颗自由,喜悦与充满爱的心。 3....
    奢奢阅读 2,688评论 0 0

友情链接更多精彩内容