gpt4 book ai didi

ios - 仅在周一发出本地通知

转载 作者:行者123 更新时间:2023-11-30 12:47:17 24 4
gpt4 key购买 nike

我尝试在每周一发送本地通知。假设我有一个场景,我必须在一个月内每周一发送服药提醒。所以一个月内总共有4个通知。我的代码如下,但我无法弄清楚以下内容;1)如何发送特定日期的通知2)如何限制通知的最大结束日期。

发送通知的代码如下;

let notification = UILocalNotification()
notification.alertBody = "Take Medication" notification.alertAction = "open" // text that is displayed after "slide to..." on the lock screen - defaults to "slide to view"
notification.fireDate = NSDate()
notification.userInfo = ["title": "notification app", "UUID": "Some Unique Guid"]
UIApplication.sharedApplication().scheduleLocalNotification(notification)

有人可以帮忙吗?问候,尼娜

最佳答案

将 NSWeekCalendarUnit 添加到 NSDateComponents 并将重复间隔设置为 NSWeekCalendarUnit。例如:

NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDate *now = [NSDate date];
NSDateComponents *componentsForFireDate = [calendar components:(NSYearCalendarUnit | NSWeekCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit| NSSecondCalendarUnit | NSWeekdayCalendarUnit) fromDate: now];
[componentsForFireDate setWeekday: 2] ; // Monday
[componentsForFireDate setHour: 18] ; // 4PM
[componentsForFireDate setMinute:0] ;
[componentsForFireDate setSecond:0] ;

//...
notification.repeatInterval = NSWeekCalendarUnit;

关于ios - 仅在周一发出本地通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41483207/

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