【代码笔记】手提式有线电话机验证码,代码笔记验证码

【代码笔记】手提式有线电话机验证码,代码笔记验证码

一,效果图。

图片 1

 

图片 2

二,代码。

RootViewController.h

#import <UIKit/UIKit.h>

@interface RootViewController : UIViewController

@end

 

RootViewController.m

图片 3

#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

图片 4

 

 

   

http://www.bkjia.com/IOSjc/1129417.htmlwww.bkjia.comtruehttp://www.bkjia.com/IOSjc/1129417.htmlTechArticle【代码笔记】手机验证码,代码笔记验证码
一,效果图。 二,代码。 RootViewController.h #import
UIKit/[email protected]
RootViewController : UIViewControl…

RootViewController.m

 

#import <UIKit/UIKit.h>

@interface RootViewController : UIViewController

@end

 

 

图片 5

RootViewController.h

 

#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

 

一,效果图。

图片 6

二,代码。

图片 7

图片 8

 

发表评论

电子邮件地址不会被公开。 必填项已用*标注

网站地图xml地图