【代碼筆記】手機驗證碼,代碼筆記驗證碼

來源:互聯網
上載者:User

【代碼筆記】手機驗證碼,代碼筆記驗證碼

一,。

 

二,代碼。

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

 

 

  

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.