码迷,mamicode.com
首页 > 移动开发 > 详细

手机验证码

时间:2014-07-07 13:18:28      阅读:258      评论:0      收藏:0      [点我收藏+]

标签:style   blog   color   art   cti   for   

 

一个软件在用户注册的时候,往往是需要验证码的。也就是要先把验证码发在手机上,然后,用户在软件中输入,若验证码正确,注册用户才可登陆。

 

- (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(@"----%d---",[d second]);
    if([d second] < 60 && [d second] > 0) {
        NSString *miao = [NSString stringWithFormat:@"%d",[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{
    //将电话号码发送到服务器,服务器返回验证码。当验证码和注册用户输入一样的时候,则可以进行下一步操作。
}

 

 

 

手机验证码,布布扣,bubuko.com

手机验证码

标签:style   blog   color   art   cti   for   

原文地址:http://www.cnblogs.com/yang-guang-girl/p/3815512.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!