IOS get verification code read seconds, press the home button to re-enter the app, to ensure that the read second normal

Source: Internet
Author: User

http://blog.csdn.net/m13215519957/article/details/51802187

(IOS) mobile app many times will encounter registration, get verification Code countdown time, press the home button to enter the background, countdown will stop, here is a way to ensure that the time will go on normally.

1: Send notifications from appdelegate in each of these two methods

#define SENDNOTIFICATION (key) [[Nsnotificationcenter Defaultcenter] Postnotificationname:key object:self userInfo:  NIL]; Send Notifications

#define WEAKOBJ (o) autoreleasepool{} __weak typeof (O) o# #Weak = o; Referencing self

-(void) Applicationdidenterbackground: (uiapplication*) application{

// process to be processed when entering the background

sendnotification(kregisterbackgroundnoti)

}

-(void) Applicationwillenterforeground: (uiapplication*) application{

// process to be processed when entering the front desk

sendnotification(kregisterfrontnoti)

}

2: In the target controller, receive these two notifications, in the respective notification method to get the current system time, and then can be calculated from the press the home key to exit the application to re-enter the difference in the application

@property(nonatomic,assign)nsdate *backdate;//time to enter the background

@property(nonatomic,assign)nsdate *frontdate;//time to reopen the app

@property(nonatomic,assign)dispatch_source_t timer;

-(void) viewdidload {

[Super viewdidload];

    [[nsnotificationcenter defaultcenter] addobserver: self selector:@ Selector (Setbackgroundtask)  name: Kregisterbackgroundnoti object:nil< Span class= "S3",

[[nsnotificationcenter defaultcenter] addobserver:self selector:@ Selector(setfronttask) name:kregisterfrontnoti object:nil];

}

-(void) setbackgroundtask{

self. Backdate=[nsdate date];

}

-(void) setfronttask{

self. Frontdate=[nsdate date];

}

Here's the way to read the second.

-(void) getstarttime{

__block int timeout=; // countdown time

dispatch_queue_t queue = dispatch_get_global_queue(dispatch_queue_priority_default, 0);

dispatch_source_t timer = dispatch_source_create(dispatch_source_type_timer, 0, 0, queue);

self. Timer=timer;

Dispatch_source_set_timer(timer,dispatch_walltime(NULL, 0),1.0*  Nsec_per_sec, 0); // per second execution

@weakobj(self);

Dispatch_source_set_event_handler(timer, ^{

if(timeout<=0) { // countdown end, close

dispatch_source_cancel(timer);

dispatch_async(dispatch_get_main_queue(), ^{

// Set the interface button to display according to your own needs settings

//NSLog (@ " time to stop action ");

});

}Else{

if (selfweak. Frontdate&&selfweak. Backdate) {//This is the time difference between entering and exiting the application two times

nstimeinterval backdate = [Selfweak. Backdate timeintervalsincereferencedate];

nstimeinterval frontdate = [Selfweak. Frontdate timeintervalsincereferencedate];

int interval = frontdate-backdate;

Timeout=timeout-interval;

Selfweak. backdate=nil;

Selfweak. frontdate=nil;

}

int seconds = timeout% ;

nsstring *strtime = [nsstring stringwithformat:@ "%.2d", seconds];

dispatch_async(dispatch_get_main_queue(), ^{

//nslog (@ " execute once per second ");

});

timeout--;

}

});

dispatch_resume(timer);

}

3 Note: If you are still reading seconds, you are performing a return to the previous level of action, you should pause the queue

-(void) Viewdiddisappear: (BOOL) animated{

[Super viewdiddisappear: animated];

dispatch_suspend(self. Timer); /// Exit interface, read seconds are still in progress, this time the queue should be paused

}

IOS get verification code read seconds, press the home button to re-enter the app, to ensure that the read second normal

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.