私は特定のユーザの選択した日にローカル通知を発生させる目覚まし時計アプリケーションを作っています。つまり、ユーザがM、F、土を選択した場合、M/F /のみ。私が持っている問題は、私の血まみれの警報がその日に毎日発砲しているということです。どのように私はそれをユーザーが選択した日に制限できますか?最も近い月曜日に1つ、最も近い水曜日と金曜日に最も近いとそれらのそれぞれにnotification.repeatInterval = NSWeekCalendarUnit
を設定します。ここに私のコード特定の日にUILocalNotificationを繰り返す方法
//For testing purposes I want the alarm to fire only on Monday at 7:00am.
//This fires every day at 7:00am
[[UIApplication sharedApplication] cancelAllLocalNotifications];
NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDate *now = [NSDate date];
// set components for time 7:00 a.m. Monday
NSDateComponents *componentsForFireDate = [calendar components:(NSYearCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit| NSSecondCalendarUnit | NSWeekdayCalendarUnit) fromDate: now];
[componentsForFireDate setWeekday: 2] ;
[componentsForFireDate setHour: 7] ;
[componentsForFireDate setMinute:0] ;
[componentsForFireDate setSecond:0] ;
NSDate *fireDateOfNotification = [calendar dateFromComponents: componentsForFireDate];
// Create the notification
UILocalNotification *notification = [[UILocalNotification alloc] init] ;
notification.fireDate = fireDateOfNotification ;
notification.timeZone = [NSTimeZone localTimeZone] ;
notification.alertBody = [NSString stringWithFormat: @"Wake up!"] ;
notification.userInfo= [NSDictionary dictionaryWithObject:[NSString stringWithFormat:@"Waking Up time"] forKey:@"wakeUp"];
//Problem is here NSDayCalendarUnit makes my alarm fire every day
notification.repeatInterval= NSDayCalendarUnit ;
//notification.soundName=UILocalNotificationDefaultSoundName;
[email protected]"alarm-clock-ringing-01.wav";
[[UIApplication sharedApplication] scheduleLocalNotification:notification] ;
何らかの理由で私の日付が間違っています。つまり、NSLogを今すぐ行っているときです:2012-01-29 22:00:37 +0000 fireDateOfNotification:2012-01-01 21:58:00 +0000 –
あなたの問題を解決しましたか? – chancyWu