注: 以下に更新があります。
前の質問で、1 時間ごとの繰り返し間隔に分を指定することについて尋ねました。ただし、日付コンポーネントを試すように求める 2 つの回答があり、実行しました。ここにあります:
[[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
UILocalNotification *notification = [[UILocalNotification alloc] init] ;
notification.fireDate = fireDateOfNotification;
notification.timeZone = [NSTimeZone localTimeZone] ;
notification.alertBody = [NSString stringWithFormat: @"You are missed!"] ;
notification.alertAction = @"View";
notification.userInfo= [NSDictionary dictionaryWithObject:[NSString stringWithFormat:@"Some information"] forKey:@"information"];
notification.repeatInterval= NSHourCalendarUnit ;
notification.soundName = @"Appnotifisound.wav";
notification.applicationIconBadgeNumber = 1;
[[UIApplication sharedApplication] scheduleLocalNotification:notification] ;
コードは私には正しいと思われ、毎時間の初めに通知を送信する必要があると言われているように機能するはずです。しかし、そうではありません!コードが完成しました。プロパティを設定し、NSCalendar が存在する場合でも正しく合成しました。それは完璧に構築され、実行されますが、私のシミュレーターでは 1 時間ごとに通知が送信されることはありません!? 間違っていると思われるものを取得できませんでした。さらに情報が必要な場合はお知らせください。または、コードの他の部分をコピーして、不足しているものを見つけるのに役立ててください。ありがとうございました。
更新:これが問題になる可能性がある場合の時間のコーディング方法です..
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;