我正在制作闹钟应用程序,该应用程序在特定用户选定日期触发本地通知,即如果用户选择了M,F,Sat,则应在每M/F/Sat时触发闹钟只要。我的问题是,当时我的血腥警报正在发射。我如何限制它到用户选定的日子?这里是我的代码如何在特定日期获得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 – 2012-01-29 22:06:20
你解决了你的问题吗? – chancyWu 2014-01-23 08:34:40