我已經硬編碼了這個小時,以幫助我在每個小時的頂部觸發本地通知,但有些錯誤。雖然,我不知道它是我測試它的方式,我的Xcode行爲還是代碼本身。無論如何,這裏是我編寫的代碼,看看它,請指導我更容易或更好的方式來編寫這樣的事情。謝謝。有沒有更好的方法來計算重複間隔通知的小時代碼的頂部?
NSCalendar *calendar1 = [NSCalendar currentCalendar];
NSDateComponents *components = [calendar1 components:(NSHourCalendarUnit |NSMinuteCalendarUnit | NSSecondCalendarUnit) fromDate:[NSDate date]];
hour = [components hour];
min =[components minute];
sec =[components second];
NSLog(@"hour is %i",hour);
NSLog(@"min is %i",min);
NSLog(@"sec is %i",sec);
if (hour < 24) {
hour=hour+1;
} else {
hour=0;
然後..
[[UIApplication sharedApplication] cancelAllLocalNotifications];
NSCalendar *calendar = [NSCalendar autoupdatingCurrentCalendar] ;
NSDateComponents *componentsForReferenceDate = [[NSDateComponents alloc] init];
[componentsForReferenceDate setHour:hour];
[componentsForReferenceDate setMinute:0];
[componentsForReferenceDate setSecond:0];
NSDate *fireDateOfNotification = [calendar dateFromComponents: componentsForReferenceDate];
// Create the notification
你從來沒有說過什麼竟是出錯了? –