我已经硬编码了这个小时,以帮助我在每个小时的顶部触发本地通知,但是出了点问题。不过,我不知道这是我测试它的方式、我的 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