其实你可以
首先,为“startTimeOfMyExoticTimer”创建一个默认值到 NSUserDefaults:
[[[NSUserDefaults] standardDefaults] setObject:[NSDate date] forKey:@"startTimeOfMyExoticTimer"];
然后启动计时器以检查有效时间范围是否结束:
[NSTimer scheduledTimerWithTimeInterval:0.1f target:self selector:@selector(checkIfTimeIsOver) userInfo:nil repeats:YES];
然后实现您的检查器方法:
-(void)checkIfTimeIsOver{
NSDate * now = [NSDate date];
NSDate * startTime = [[NSUserDefaults standardDefaults] objectForKey:@"startTimeOfMyExoticTimer"];
// Here compare your now and startTime objects. (subsract them) and see the difference between them. If your time range is to be set on 30 seconds. Then you should check if the time difference between those objects are bigger than 30 seconds.
}
即使设备被锁定,应用程序在后台等,这也将起作用。
这种方法对我有用,希望它也对你有用