首页 > 移动平台 > 详细

手机短信验证码时间倒计时

时间:2016-08-01 15:48:32      阅读:361      评论:0      收藏:0      [点我收藏+]

app 进入后台后,实现手机短信验证码时间不间断倒计时

//当app进入手机后台和手机前台时获取时间
- (void)applicationDidEnterBackground:(UIApplication *)application {
    NSDate* dat = [NSDate dateWithTimeIntervalSinceNow:0];
    NSTimeInterval a=[dat timeIntervalSince1970] * 1000;
    NSString *timeString = [NSString stringWithFormat:@"%.0f", a];
    NSDictionary *dic = @{@"time":timeString};
    
    
    [[NSNotificationCenter defaultCenter]postNotificationName:@"background" object:nil userInfo:dic];
}

- (void)applicationWillEnterForeground:(UIApplication *)application {
    NSDate* dat = [NSDate dateWithTimeIntervalSinceNow:0];
    NSTimeInterval a=[dat timeIntervalSince1970] * 1000;
    NSString *timeString = [NSString stringWithFormat:@"%.0f", a];
    NSDictionary *dic = @{@"time":timeString};
    
    [[NSNotificationCenter defaultCenter]postNotificationName:@"foreground" object:nil userInfo:dic];

}
//登录界面实现通知调用


@property (nonatomic) NSInteger secondCountDown;
@property (nonatomic) NSInteger integer1;
@property (nonatomic) NSInteger integer2;



-(void)viewWillAppear:(BOOL)animated
{
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(appGoForeground:) name:@"foreground" object:nil];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(appGoBackground:) name:@"background" object:nil];
    
    self.navigationController.navigationBarHidden = YES;

}

- (void)appGoBackground:(NSNotification *)notification
{
    
    _integer1 = [notification.userInfo[@"time"] integerValue];
    
}

- (void)appGoForeground:(NSNotification *)notification
{
    NSInteger integer = [notification.userInfo[@"time"] integerValue];
    
    _integer2 = (integer - _integer1)/1000;
    
    _secondCountDown = _secondCountDown - _integer2;
    
}

 

手机短信验证码时间倒计时

原文:http://www.cnblogs.com/sayimba/p/5725656.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!