我正在尝试设置在所有工作日触发的本地通知,即:从周一到周五下午 5 点,但不在周末。
在 Apple 的文档中,我发现这种方式可以触发所有天的通知:
var dateComponents = DateComponents()
dateComponents.calendar = Calendar.current
dateComponents.hour = 17 // 17:00 hours
// Create the trigger as a repeating event.
let trigger = UNCalendarNotificationTrigger(
dateMatching: dateComponents, repeats: true)
但我无法找到如何排除特定日期,即:星期六和星期日,并将时间设置为下午 5 点。有线索吗?
最佳答案
首先创建一个日期对象
let date = Date(timeIntervalSinceNow: 3600) // it may be any date whatever you want
从日期组件创建触发器:
let triggerWeekly = Calendar.current.dateComponents([.weekday,hour,.minute,.second,], from: date)
现在在 UNCalendarNotificationTrigger 中通过 repeats true 传递那个日期
let trigger = UNCalendarNotificationTrigger(dateMatching: triggerWeekly, repeats: true)
创建一个带时间的日期:
let formatter = DateFormatter()
formatter.dateFormat = "yyyy/MM/dd HH:mm"
let someDateTime = formatter.date(from: "2016/10/08 22:31") // put your date and time here
如果 Locale 特定时间:
formatter.locale = NSLocale(localeIdentifier: "en_US_POSIX")
关于ios - 在所有工作日触发本地 iOS 通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57060780/