一,效果图。

二,代码。

RootViewController.h

#import <UIKit/UIKit.h>

@interface RootViewController : UIViewController

@end

RootViewController.m

#import "RootViewController.h"

@interface RootViewController ()

@end

@implementation RootViewController
{
NSTimer* sysTimer;
BOOL timeStart;
} - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
} - (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view. //BOOL值默认为NO。
NSLog(@"---timeStart--%d-----",timeStart); //发送验证码到手机上-Button
UIButton *sendYZM=[UIButton buttonWithType:UIButtonTypeCustom];
[sendYZM setFrame:CGRectMake(50, 100, 250, 50)];
[sendYZM setBackgroundColor:[UIColor redColor]];
[sendYZM setTitle:@"发送注册验证码到手机上" forState:UIControlStateNormal];
[sendYZM setTitleColor:[UIColor colorWithRed:66/255.0 green:66/255.0 blue:221/255.0 alpha:1.0] forState:UIControlStateNormal];
[sendYZM addTarget:self action:@selector(doClickButton:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:sendYZM]; }
-(void)doClickButton:(UIButton *)btn
{
timeStart=YES;
sysTimer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timerFireMethod:) userInfo:nil repeats:YES];
// 可以通过fire这个方法去触发timer,即使timer的firing time没有到达
[sysTimer fire];
[self getAuthCode]; } //随时更新验证码发送的时间
- (void)timerFireMethod:(NSTimer *)timer {
//定义一个NSCalendar对象
NSCalendar *cal = [NSCalendar currentCalendar];
//初始化目标时间...
NSDateComponents *endTime = [[NSDateComponents alloc] init];
//得到当前时间
NSDate *today = [NSDate date];
NSDate *date = [NSDate dateWithTimeInterval:60 sinceDate:today];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
NSString *dateString = [dateFormatter stringFromDate:date]; static int year;
static int month;
static int day;
static int hour;
static int minute;
static int second; if(timeStart) {
//从NSDate中取出年月日,时分秒,但是只能取一次
year = [[dateString substringWithRange:NSMakeRange(0, 4)] intValue];
month = [[dateString substringWithRange:NSMakeRange(5, 2)] intValue];
day = [[dateString substringWithRange:NSMakeRange(8, 2)] intValue];
hour = [[dateString substringWithRange:NSMakeRange(11, 2)] intValue];
minute = [[dateString substringWithRange:NSMakeRange(14, 2)] intValue];
second = [[dateString substringWithRange:NSMakeRange(17, 2)] intValue];
timeStart = NO;
} [endTime setYear:year];
[endTime setMonth:month];
[endTime setDay:day];
[endTime setHour:hour];
[endTime setMinute:minute];
[endTime setSecond:second]; //把目标时间装载入date
NSDate *todate = [cal dateFromComponents:endTime]; //用来得到具体的时差,是为了统一成北京时间
unsigned int unitFlags = NSYearCalendarUnit| NSMonthCalendarUnit| NSDayCalendarUnit| NSHourCalendarUnit| NSMinuteCalendarUnit| NSSecondCalendarUnit; NSDateComponents *d = [cal components:unitFlags fromDate:today toDate:todate options:0];
NSLog(@"----%ld---",[d second]);
if([d second] < 60 && [d second] > 0) {
NSString *miao = [NSString stringWithFormat:@"%ld",[d second]];
NSLog(@"---miao----%@",miao); UIButton *sendYZM=[UIButton buttonWithType:UIButtonTypeCustom];
[sendYZM setFrame:CGRectMake(50, 100, 250, 50)];
[sendYZM setBackgroundColor:[UIColor redColor]];
[sendYZM setTitle:[NSString stringWithFormat:@"重新发送验证码(%@秒)",miao] forState:UIControlStateNormal];
[sendYZM setTitleColor:[UIColor colorWithRed:66/255.0 green:66/255.0 blue:221/255.0 alpha:1.0] forState:UIControlStateNormal];
[self.view addSubview:sendYZM]; }else if([d second] == 0) {
[sysTimer invalidate]; UIButton *sendYZM=[UIButton buttonWithType:UIButtonTypeCustom];
[sendYZM setFrame:CGRectMake(50, 100, 250, 50)];
[sendYZM setBackgroundColor:[UIColor redColor]];
[sendYZM setTitle:@"重新发送验证码" forState:UIControlStateNormal];
[sendYZM setTitleColor:[UIColor colorWithRed:66/255.0 green:66/255.0 blue:221/255.0 alpha:1.0] forState:UIControlStateNormal];
[sendYZM addTarget:self action:@selector(doClickButton:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:sendYZM];
}
}
- (void)getAuthCode{
//将电话号码发送到服务器,服务器返回验证码。当验证码和注册用户输入一样的时候,则可以进行下一步操作。
} - (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
} /*
#pragma mark - Navigation // In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/ @end
 
 

【代码笔记】iOS-手机验证码的更多相关文章

  1. ios 手机验证码用户注册(倒计时15秒)

    // // ViewController.m // register手机验证码注册 // // Created by zzqqrr on 17/8/28. // Copyright (c) 2017年 ...

  2. asp.net限时发送手机验证码

    html代码 <p> <strong>手机验证码:</strong> <asp:TextBox ID="code" runat=" ...

  3. Web程序员开发App系列 - 调试Android和IOS手机代码(补图)

    Web程序员开发App系列 Web程序员开发App系列 - 认识HBuilder Web程序员开发App系列 - 申请苹果开发者账号 Web程序员开发App系列 - 调试Android和iOS手机代码 ...

  4. 注册页面手机验证码无跳转接收[html+js+ajax+php]

    [学习笔记] 来源:注册时需要使用短信验证码,但是注册的时候,点击接收验证码时,会产生跳转(尼玛,这不是我想要的啊!o(╥﹏╥)o) 查询资料和看书之后,知道使用js+ajax可以实现,就从网上找了一 ...

  5. 利用exif.js解决ios手机上传竖拍照片旋转90度问题

    html5+canvas进行移动端手机照片上传时,发现ios手机上传竖拍照片会逆时针旋转90度,横拍照片无此问题:Android手机没这个问题. 因此解决这个问题的思路是:获取到照片拍摄的方向角,对非 ...

  6. C#发送手机验证码

    C#发送手机验证码,平台有很多,我就说说其中的1个平台 测试环境:.net2.0 测试效果:速度还可以,10秒内接收短信 1.去http://www.yuntongxun.com注册,会送8元测试金额 ...

  7. IOS开发笔记 IOS如何访问通讯录

    IOS开发笔记  IOS如何访问通讯录 其实我是反对这类的需求,你说你读我的隐私,我肯定不愿意的. 幸好ios6.0 以后给了个权限控制.当打开app的时候你可以选择拒绝. 实现方法: [plain] ...

  8. 解决ios手机上传竖拍照片旋转90度问题

    html5+canvas进行移动端手机照片上传时,发现ios手机上传竖拍照片会逆时针旋转90度,横拍照片无此问题:Android手机没这个问题. 因此解决这个问题的思路是:获取到照片拍摄的方向角,对非 ...

  9. django 发送手机验证码

    一.流程分析: 1.用户在项目前端,输入手机号,然后点击[获取验证码],将手机号发到post到后台. 2.后台验证手机号是否合法,是否已被占用,如果通过验证,则生成验证码,并通过运行脚本,让短信运营商 ...

  10. 微信小程序获取手机验证码

    一种比较常见的功能获取手机验证码 先看效果图: 其实这个功能实现起来很简单,主要就是调取第三方接口,拿到返回值验证的问题 直接上代码吧: wxml页面: <view class='changeI ...

随机推荐

  1. HT For Web 拓扑图背景设置

    HT For Web 的HTML5拓扑图组件graphView背景设置有多种途径可选择: divBackground:通过css设置graphView对应的div背景 Painter:通过graphV ...

  2. Java魔法堂:类加载机制入了个门

    一.前言 当在CMD/SHELL中输入 $ java Main<CR><LF> 后,Main程序就开始运行了,但在运行之前总得先把Main.class及其所依赖的类加载到JVM ...

  3. DirectShowNet 使用摄像头录像+录音

    http://www.cnblogs.com/endv/p/6052511.html // ------------------------------------------------------ ...

  4. JS实现日程安排 日程安排插件

    代码: <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="EmpWeekPla ...

  5. jQuery uploadify在谷歌和火狐浏览器下无法上传

    原因: 由于jQuery uploadify是借助flash来实现上传的,每一次向后台发送数据流请求时,ie会自动把本地cookie存储捆绑在一起发送给服务器.但firefox.chrome不会这样做 ...

  6. android 开发环境搭建

    http://www.cnblogs.com/bjzhanghao/archive/2012/11/14/2769409.html http://jingyan.baidu.com/article/7 ...

  7. WebView输入框提示

    做基于WebView应用时,页面上有一个输入框,当输入的文字过多时,超过输入框的行数时,输入框能够滚动,这时间问题来了,输入的提示箭头会移动到输入框外,如何解决这个问题呢,查找chromium源码如下 ...

  8. Mysql 大小写问题

    今天发布程序的时候,日志报错找不到表,但是系统中已经存在表,最后发现是sql大小写的问题,mysql默认设置导致这些执行失败. 1.用ROOT登录,修改/etc/my.cnf 2.在[mysqld]下 ...

  9. python 局部变量和全局变量 global

    当你在函数定义内声明变量的时候,它们与函数外具有相同名称的其他变量没有任何关系,即变量名称对于函数来说是 局部 的.这称为变量的 作用域 .所有变量的作用域是它们被定义的块,从它们的名称被定义的那点开 ...

  10. 修复 XE7 update1 发布 iOS 8.x 实机问题

      1. 开启工程目录下面的 Entitlement.TemplateiOS.xml 档案. 2. 加入二行: <key>application-identifier</key> ...