2012-08-24 66 views
0

我正在製作一個Cocoa應用程序,我希望它能夠進行每日更新。 (這個應用程序是供個人使用的)我希望它每天在特定的時間更新它,所以我設置了電腦在當時醒來。我在我的應用程序設置的通知觀察者的事情,如果應用程序獲取的計算機沒有清醒的通知,將進行這樣的功能:如何檢查NSDate是否在兩次之間?

- (void) receiveWakeNote: (NSNotification*) note 
{ 
    [self conductBeginning]; 
} 

我應該補充,以確保在特定的時間之間發生的喚醒通知,例如16:00至16:15之間,然後只執行

[self conductBeginning]; 

line。

回答

2
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"); 
+0

由於它出色的作品! –

0

是這樣的:

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; 
相關問題