我正在制作一个Cocoa应用程序,我希望它能够进行日常更新。(这个应用程序是个人使用的)我希望它每天在特定的时间进行更新,所以我把我的电脑设置为在那个时间醒来。我在我的应用程序中设置了一个通知观察员,如果应用程序收到计算机唤醒通知,它将执行此功能:
- (void) receiveWakeNote: (NSNotification*) note
{
[self conductBeginning];
}
我应该添加什么来确保唤醒通知发生在特定时间之间,比如16:00到16:15之间,然后只执行
[self conductBeginning];
行。
NSDate *now = [NSDate date];
NSUInteger units = NSHourCalendarUnit | NSMinuteCalendarUnit;
NSDateComponents *components = [[NSCalendar currentCalendar] components:units
fromDate:now];
if (components.hour == 16 && components.minute <= 15)
NSLog(@"it's time");
类似这样的东西:
NSDate * aDate = ...;
NSDate * bDate = ...;
NSTimeInterval a = [aDate timeIntervalSinceNow];
NSTimeInterval b = [bDate timeIntervalSinceNow];
NSTimeInterval min = a > b ? b : a;
NSTimeInterval max = a < b ? b : a;
NSTimeInterval now = CFAbsoluteTimeGetCurrent();
bool result = now >= min && now <= max;