iOS开发短信验证码封装 方便好用
---恢复内容开始---

1.RootViewControler
// Copyright © 2016年 Chason. All rights reserved.
//
#import "ViewController.h"
#import "MessageCordView.h"
//手机屏幕的宽和高
#define kScreenWidth [UIScreen mainScreen].bounds.size.width
#define KScreenHeight [UIScreen mainScreen].bounds.size.height
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor groupTableViewBackgroundColor];
MessageCordView *coreView = [[MessageCordView alloc] initWithFrame:CGRectMake(0, 200, kScreenWidth,50)];
[self.view addSubview:coreView];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
2.封装带输入框的短信验证码
// Copyright © 2016年 Chason. All rights reserved.
//
#import <UIKit/UIKit.h>
@interface MessageCordView : UIView
@property (nonatomic, strong) UITextField *rechargeField;
@property (nonatomic, strong) UIButton *button;
@property (nonatomic, strong) UILabel *label;
@property (nonatomic, strong) NSTimer *timer;
@end
// Copyright © 2016年 Chason. All rights reserved.
//
#import "MessageCordView.h"
#import "myView.h"
//手机屏幕的宽和高
#define kScreenWidth [UIScreen mainScreen].bounds.size.width
#define KScreenHeight [UIScreen mainScreen].bounds.size.height
static int countDownTime = 60;
@implementation MessageCordView
- (instancetype)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
UIView *rechargeView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, frame.size.width, frame.size.height)];
rechargeView.backgroundColor = [UIColor whiteColor];
[self addSubview:rechargeView];
UILabel *rechargeMoney = [[UILabel alloc] initWithFrame:CGRectMake(15, 10, 100, frame.size.height - 20)];
rechargeMoney.font = [UIFont systemFontOfSize:16];
rechargeMoney.textColor = [UIColor grayColor];
rechargeMoney.text = @"验证码";
rechargeMoney.font = [UIFont systemFontOfSize:20];
[rechargeView addSubview:rechargeMoney];
_rechargeField = [[UITextField alloc] initWithFrame:CGRectMake(110, 10, frame.size.width - 230, frame.size.height - 20)];
_rechargeField.clearButtonMode = UITextFieldViewModeWhileEditing;
_rechargeField.placeholder = @"请输入验证码";
_rechargeField.borderStyle = UITextBorderStyleNone;
[rechargeView addSubview:_rechargeField];
_button = [myView creatButtonWithFrame:CGRectMake(frame.size.width - 105, 5, 90, frame.size.height - 10) title:@"获取验证码" tag:1000 tintColor:[UIColor whiteColor] backgroundColor:[UIColor colorWithRed:18/255.0 green:129/255.0 blue:201/255.0 alpha:1]];
_button.layer.cornerRadius = 10;
_button.font = [UIFont systemFontOfSize:14];
[_button addTarget:self action:@selector(getVerificationCode) forControlEvents:UIControlEventTouchUpInside];
[rechargeView addSubview:_button];
}
return self;
}
- (void)getVerificationCode
{
_label = [[UILabel alloc]initWithFrame:self.button.bounds];
_timer = [[NSTimer alloc]init];
_timer= [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(countDown) userInfo:nil repeats:YES];
[_timer setFireDate:[NSDate distantPast]];
//请求服务器发送短信验证码
//......
}
- (void)countDown
{
countDownTime--;
if (countDownTime<=0||countDownTime ==60) {
_label.hidden = YES;
_button.hidden = NO;
_button.userInteractionEnabled = YES;
[_timer setFireDate:[NSDate distantFuture]];
countDownTime =60;
}else{
_label.hidden = NO;
_label.text = [NSString stringWithFormat:@"(%ds)重新获取",countDownTime];
_label.font = [UIFont systemFontOfSize:12];
_label.backgroundColor = [UIColor colorWithRed:18/255.0 green:129/255.0 blue:201/255.0 alpha:1];
_label.layer.cornerRadius = 8;
_label.clipsToBounds = YES;
_label.textAlignment = NSTextAlignmentCenter;
_label.textColor= [UIColor whiteColor];
[_button addSubview:_label];
_button.userInteractionEnabled = NO;
}
}
@end
3.创建获取验证码button
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
@interface myView : NSObject
+(UIButton *)creatButtonWithFrame:(CGRect)frame title:(NSString *)title tag:(NSInteger)tag tintColor:(UIColor *)tintColor backgroundColor:(UIColor *)backgroundColor;
@end
#import "myView.h"
@implementation myView
+(UIButton *)creatButtonWithFrame:(CGRect)frame title:(NSString *)title tag:(NSInteger)tag tintColor:(UIColor *)tintColor backgroundColor:(UIColor *)backgroundColor{
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setTitle:title forState:UIControlStateNormal];
button.tag = tag;
button.backgroundColor = backgroundColor;
button.tintColor= tintColor;
button.frame = frame;
[button setTitleColor:tintColor forState:UIControlStateNormal];
return button;
}
@end
如果您觉得博文对您有帮助, 希望您能打发给博主一点茶水钱, 您的支持就是博主最大的动力!

---恢复内容结束---
iOS开发短信验证码封装 方便好用的更多相关文章
- iOS开发——短信验证码
作为iOS开发者,大家都应该知道ShareSDK这个比较出名的第三方分享(带统计功能)开发工具包. 他的官网今年年初发布了短信验证码的SDK.本文介绍它的短信验证码SDK.(部分过程参考官网如何集成的 ...
- ios发送短信验证码计时器的swift实现
转载自:http://www.jianshu.com/p/024dd2d6e6e6# Update: Xcode 8.2.1 Swift 3 先介绍一下 属性观测器(Property Observer ...
- iOS开发——自动填充短信验证码
苹果在iOS 12,改进了一个很人性化的小细节.在做短信验证码功能的时候,自动获取短信中的验证码,然后点击填充即可.不用再向之前那样麻烦,自己看到弹出的短信信息后,死记硬背,再一个个敲上去,害怕背错了 ...
- iOS点击获取短信验证码按钮
概述 iOS点击获取短信验证码按钮, 由于 Demo整体测试运行效果 , 整个修改密码界面都已展现, 并附送正则表达式及修改密码逻辑. 详细 代码下载:http://www.demodashi.com ...
- 云平台短信验证码通知短信java/php/.net开发实现
一.本文目的 大部分平台都有一个接入发送短信验证码.通知短信的需求.虽然市场上大部分平台的接口都只是一个非常普通的HTTP-GET请求,但终归有需要学习和借鉴使用的朋友. 本文的初衷是主要提供学习便利 ...
- App开发(Android与php接口)之:短信验证码
最近和同学们一起开发一个自主项目,要用到短信验证码,在网上搜索了很久,看到一个推荐贴,提到了很多不错的短信服务商.经过测试,帖子中提到的服务商他们的短信到达率和到达速度也都不错.最后,由于经费问题,我 ...
- iOS使用技能 - 短信,语言验证码的获取与验证小结
最近有学习一个小技能,这里小结一下,分享给大家,互相交流. 首先是大体步骤: 在mob官网注册,然后添加短信验证的应用 使用cocoapods导入框架 Podfile文件: platform :ios ...
- flask开发restful api系列(5)-短信验证码
我们现在开发app,注册用户的时候,不再像web一样,发送到个人邮箱了,毕竟个人邮箱在移动端填写验证都很麻烦,一般都采用短信验证码的方式.今天我们就讲讲这方面的内容. 首先,先找一个平台吧.我们公司找 ...
- Android开发之短信验证码示例
在说Android中的短信验证码这个知识点前,我们首先来了解下聚合数据 聚合数据介绍 聚合数据是一家国内最大的基础数据API提供商,专业从事互联网数据服务.免费提供从天气查询.空气质量.地图坐标到金融 ...
随机推荐
- rails框架配置
rails框架默认有三个模式development(开发),production(上线),test(测试) Development config.cache_classes = false 每次请求都 ...
- go Context的使用
控制并发有两种经典的方式,一种是WaitGroup,另外一种就是Context WaitGroup的使用 WaitGroup可以用来控制多个goroutine同时完成 func main() { va ...
- Java——String类总结---18.09.27
1.String类 String类在java.lang包中,java使用String类创建一个字符串变量,字符串变量属于对象.java把String类声明的final类,不能有类.String类对象创 ...
- sqlserver分区视图中分区列的规则
分区列规则 分区列存在于每个成员表上,并且通过 CHECK 约束标识特定表中的可用数据.分区列必须遵守如下规则: 每个基表都拥有键值由 CHECK 约束所强制的分区列.每个表的 CHECK 约束的键范 ...
- 洛咕 P2463 [SDOI2008]Sandy的卡片
哈希水过. 首先这是一段delta相同的序列,按照套路差分一下,b[i]=a[i]-a[i-1],然后就是这些序列的最长公共子段 由于数据范围很小,就可以二分,枚举第一个序列的子段然后每个子序列暴力c ...
- Python 学习 第五篇:语句和语法
Python程序是语句构成的,语句包含表达式,表达式嵌套在语句中,包含变量和常量,用于处理对象.Python的语法实质上是由表达式.语句和代码块构成的.语句是由表达式构成的,代码块是由多个语句构成的复 ...
- Print Spooler 服务自动停止
1)先在服务里停止并禁用Print Spooler : 2)删除此文件夹下的所有文件,C:\Windows\System32\spool\PRINTERS\ : 3)删除注册表 HKEY_LOCAL_ ...
- JAVA使用qrcode生成二维码(带logo/不带logo)
/** * */ package qrcode; import java.awt.Color; import java.awt.Graphics2D; import java.awt.Image; i ...
- Altium软件中Unknowpin的详细解决办法
1.Altium软件中Unknowpin第一种原因:PCB封装缺失遗漏,直接加入对应的封装即可.点击箭头指示处的Add,接着点击OK之后,再点击图中所示处Browse...选择封装库的封装即可. 2. ...
- 浅谈ajax同步、异步的问题
最近实习的时候看到过firefox的同步.异步的警告,想着概念不是那么清楚,于是整理了一下ajax同步异步方面的知识.我是小白,做个笔记. 首先就是概念问题,ajax根据async进行区分同步和异步过 ...