gpt4 book ai didi

ios - UILocalNotification 设置 repeatCalendar

转载 作者:塔克拉玛干 更新时间:2023-11-02 09:28:54 24 4
gpt4 key购买 nike

我是 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com