ios - UILocalNotification 设置 repeatCalendar

标签 ios nsdate uilocalnotification nscalendar nsdatecomponent

我是 NSCalanderNSdatesNSDateComponents 的新手

基本上我有一个本地通知,我想根据用户的选择重复触发日期,比如说只在周日和周一。

我们应该为 UILocalNotification 使用 repeatCalendar 属性,但我不知道如何设置它。

所以任何人都可以用简单的代码行帮助我吗?

谢谢

最佳答案

有一个代码片段可以将 UILocalNotification 设置为在每周日 20:00 触发。

NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDate *now = [NSDate date];
NSDateComponents *componentsForFireDate = [calendar components:(NSYearCalendarUnit | NSWeekCalendarUnit|  NSHourCalendarUnit | NSMinuteCalendarUnit| NSSecondCalendarUnit | NSWeekdayCalendarUnit) fromDate: now];
[componentsForFireDate setWeekday: 1] ; //for fixing Sunday
[componentsForFireDate setHour: 20] ; //for fixing 8PM hour
[componentsForFireDate setMinute:0] ;
[componentsForFireDate setSecond:0] ;

NSDate *fireDateOfNotification = [calendar dateFromComponents: componentsForFireDate];
UILocalNotification *notification = [[UILocalNotification alloc]  init] ;
notification.fireDate = fireDateOfNotification ;
notification.timeZone = [NSTimeZone localTimeZone] ;
notification.alertBody = [NSString stringWithFormat: @"New updates!"] ;
notification.userInfo= [NSDictionary dictionaryWithObject:[NSString stringWithFormat:@"New updates added for that week!"] forKey:@"new"];
notification.repeatInterval= NSWeekCalendarUnit ;
notification.soundName=UILocalNotificationDefaultSoundName;

NSLog(@"notification: %@",notification);

[[UIApplication sharedApplication] scheduleLocalNotification:notification] ;

享受

关于ios - UILocalNotification 设置 repeatCalendar,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21301964/

相关文章:

ios - 需要将 NSString 转换为 Nsdate

swift - 将字符串转换为返回前一天的日期

ios - 每天在设定的时间在 Swift 中通知?

ios - 通过点击任何 View 上的本地通知来呈现 View Controller

ios - 在 iOS 上使用 openCV 的高动态范围成像产生乱码输出

ios - 没有启动画面的应用Xcode 5

ios - 处理应用程序 :willChangeStatusBarFrame:

ios - 将 NSMutableSet 和 NSMutableOrderedSet 桥接在一起的协议(protocol)

iphone - 在 UILocalNotification sound 中设置录制的音频

ios - Swift LocalNotification 最后触发日期