如何在某些日子获得UILocalNotification重复

我正在制作一个闹钟应用程序,它会在某一天select某个用户时触发一个本地通知,例如,如果用户select了M,F,Sat,则应该每M / F / Sat只发出一个闹钟。 我的问题是,当时我的血腥警报正在发射。 我怎样才能限制它到用户选定的日子? 这是我的代码

//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 am 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; notification.soundName=@"alarm-clock-ringing-01.wav"; [[UIApplication sharedApplication] scheduleLocalNotification:notification] ; 

如果实际上只让本地通知触发一次,但是当您的设备收到通知,并重新发布相同的通知,它会起作用吗? 你的应用程序不需要运行这个工作。

当您的应用程序未运行时,您可以在应用程序委托的applicationDidFinishLaunching中处理本地通知:

 UILocalNotification *localNotif = [launchOptions objectForKey:UIApplicationLaunchOptionsLocalNotificationKey]; // handles notification when application is relaunched after being terminated // not when app is already in the foreground running. if(localNotif) { // repeat the same local notification again [[UIApplication sharedApplication] scheduleLocalNotification:localNotif]; } 

注意:repeatInterval将按照您指定的单位执行相同的操作(在这种情况下,“day”,无论您指定为通知date,每天都会重复)

你可以做三个警报:一个是最近的星期一,最近的星期三和最近的星期五,并设置notification.repeatInterval = NSWeekCalendarUnit每个。