2

我是新来的NSCalander, NSdates, 和NSDateComponents

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

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

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

谢谢

4

1 回答 1

6

有一个代码片段可以将 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] ;

请享用

于 2014-01-23T07:35:31.690 回答