1.不拿手机当前时间来倒计时
2.锁屏期间,计时器会停止,单靠服务器返回的剩余时间行不通
这时候可以这样解决
取手机已开机的运行时间
API 返回时: start Up time API返回剩余30秒
当前: 当前的start up time ?(新的剩余时间)
API 返回: self.iLeftTime = r.iLastRoLeftTime.intValue;
self.iStartUpTime = [NSDate uptime];
timer 方法里:NSTimeInterval nowUpTime = [NSDate uptime];
self.iNowLeftTime = self.iLeftTime +(self.iStartUpTime-nowUpTime);
if (self.iNowLeftTime <=0) {
[self.iCountDownTimer invalidate];
self.iCountDownTimer = nil;
return;
}
int min = self.iNowLeftTime/60;
int sec = self.iNowLeftTime%60;
self.iCountDownLabel.text = [NSString stringWithFormat:@"%02d:%02d",min,sec];
--------------------
#import <sys/sysctl.h>
+ (NSTimeInterval)uptime {
struct timeval boottime;
int mib[2] = {CTL_KERN, KERN_BOOTTIME};
size_t size = sizeof(boottime);
struct timeval now;
struct timezone tz;
gettimeofday(&now, &tz);
double uptime = -1;
if (sysctl(mib, 2, &boottime, &size, NULL, 0) != -1 && boottime.tv_sec != 0) {
uptime = now.tv_sec - boottime.tv_sec;
uptime += (double)(now.tv_usec - boottime.tv_usec) / 1000000.0;
}
return uptime;
}