我是 NSCalander
、NSdates
和 NSDateComponents
的新手
基本上我有一个本地通知,我想根据用户的选择重复触发日期,比如说只在周日和周一。
我们应该为 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/