iOS to get the countdown effect of verification code

  • 2021-07-09 09:23:58
  • OfStack

In this paper, we share the specific code of iOS countdown to obtain verification code for your reference. The specific contents are as follows

1. Countdown to send the verification code, and the interface jump timing will be reset


/** Timing of resending SMS */
-(void)fireTimer{
 __block int timeout=180; // 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);

 dispatch_source_set_timer(_timer,dispatch_walltime(NULL, 0),1.0*NSEC_PER_SEC, 0); // Execute per second 

 dispatch_source_set_event_handler(_timer, ^{
  if(timeout<=0){ // End of countdown, close 
   dispatch_source_cancel(_timer);
   dispatch_async(dispatch_get_main_queue(), ^{
    // Set the button display of the interface   Set according to your own needs 
    [_changeCodeButton setTitle:@" Get verification code " forState:UIControlStateNormal];
    _changeCodeButton.userInteractionEnabled = YES;
   });
  }else{
   int seconds = timeout ;//% 60;
   NSString *strTime = [NSString stringWithFormat:@"%.2d", seconds];
   dispatch_async(dispatch_get_main_queue(), ^{
    // Set the button display of the interface   Set according to your own needs 
    //NSLog(@"____%@",strTime);
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:1];
    _changeCodeButton.titleLabel.font = [UIFont systemFontOfSize:14];
    [_changeCodeButton setTitle:[NSString stringWithFormat:@"(%@s) Post-retrieval ",strTime] forState:UIControlStateNormal];
    [UIView commitAnimations];
    _changeCodeButton.userInteractionEnabled = NO;
   });
   timeout--;
  }
 });
 dispatch_resume(_timer);
}

The above method was transferred from a certain blogger a long time ago. Because I can't find the address, it is very useful, so I will include it here for the time being. Forgive me.

2. Countdown to send verification code, and the timing will not reset after the interface jumps

It hasn't been sorted out yet. Continue to update

First, let's share other cases: IOS realizes the countdown function of verification code (1), and IOS realizes the countdown function of verification code (2)


Related articles: