One.
Two, code.
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:nibnam Eornil Bundle:nibbundleornil]; if (self) {//Custom initialization} return to self;} -(void) viewdidload{[Super Viewdidload]; Do any additional setup after loading the view. The bool value defaults to No. NSLog (@ "---timestart--%d-----", timestart); Send the verification code to the phone-button UIButton *sendyzm=[uibutton Buttonwithtype:uibuttontypecustom]; [Sendyzm Setframe:cgrectmake (50, 100, 250, 50)]; [Sendyzm Setbackgroundcolor:[uicolor Redcolor]; [Sendyzm settitle:@] Send the registration verification code to the phone "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]; The fire can be used to trigger a timer, even if the timer's firing time does not reach [systimer fires]; [Self getauthcode];} Always update the time the CAPTCHA was sent-(void) Timerfiremethod: (Nstimer *) timer{//define a Nscalendar object Nscalendar *cal = [Nscalendar currentcale Ndar]; Initialize target time ... Nsdatecomponents *endtime = [[Nsdatecomponents alloc] init]; Get current time 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 from the day of the month, time division seconds, but can only be taken once 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 (one, 2)] intvalue]; minute = [[DateString Substringwithrange:nsmakerange (2)] intvalue]; Second = [[DateString Substringwithrange:nsmakerange (2)] intvalue]; Timestart = NO; } [EndTime setyear:year]; [EndTime Setmonth:month]; [EndTime Setday:day]; [EndTime Sethour:hour]; [EndTime Setminute:minute]; [EndTime Setsecond:second]; Load the target time into date nsdate *todate = [cal Datefromcomponents:endtime]; Used to get specific jet lag, is to unify into Beijing time 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] < && [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:@ "Resend Verification Code (%@ seconds)", 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:@ "Resend The Verification Code" 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{//sends the phone number to the server, and the server returns the verification code. The next step can be done when the verification code is the same as the registered user input. }-(void) didreceivememorywarning{[Super didreceivememorywarning]; Dispose of any resources the can be recreated.} /* #pragma mark-navigation//in a storyboard-based application, you'll often want to do a little preparation before Nav igation-(void) Prepareforsegue: (Uistoryboardsegue *) Segue Sender: (ID) sender{//Get The new view controller using [Segu E Destinationviewcontroller]. Pass the selected object to the new view Controller.} */@end
"Code Note" Phone Verification code