-(void) startcountdownaction{
// Start Countdown codebtn is the BTN that sends the verification code, and also the countdown UI
self. Codebttn. Enabled = NO;
__block int timeout = ;
[self. CODEBTTN settitle:@ "A second " forstate:uicontrolstatenormal];
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);
Dispatch_source_set_timer(_timer,dispatch_walltime(NULL, 0),1.0* nsec_per_sec, 0);
Dispatch_source_set_event_handler(_timer, ^{
if(timeout<=0) {
dispatch_source_cancel(_timer);
dispatch_async(dispatch_get_main_queue(), ^{
// countdown end
self. Codebttn. Enabled = YES;
[self. CODEBTTN settitle:@ " Send verification Code " forstate:uicontrolstatenormal];
});
}Else{
int seconds = timeout% ;
nsstring *strtime = [nsstring stringwithformat:@ "%.2d seconds ", seconds];
dispatch_async(dispatch_get_main_queue(), ^{
[self. CODEBTTN settitle: Strtime forstate:uicontrolstatenormal];
});
timeout--;
}
});
dispatch_resume(_timer);
}
Gcd Method for Button Countdown