UNNotificationRequest
不提供任何东西来触发两周重复。
您UNCalendarNotificationTrigger
只能触发以下类型的重复:
weekly
, daily
, monthly
,yearly
switch repeatType {
case .daily:
newComponents.hour = components.hour
newComponents.minute = components.minute
break
case .weekly:
newComponents.hour = components.hour
newComponents.minute = components.minute
newComponents.weekday = components.weekday
break
case .monthly:
newComponents.hour = components.hour
newComponents.minute = components.minute
newComponents.day = components.day
break
case .none:
newComponents.hour = components.hour
newComponents.minute = components.minute
newComponents.day = components.day
newComponents.month = components.month
newComponents.year = components.year
break
case .annually:
newComponents.hour = components.hour
newComponents.minute = components.minute
newComponents.day = components.day
newComponents.month = components.month
break
}
尽管您可以轻松地使用EventKit
所有此类重复选项(如两周、季度、半年等)在这些事件中创建事件和警报。
我创建了这段代码来创建具有多种重复类型的事件:
let eventStore = EKEventStore()
eventStore.requestAccess(to: .event, completion: { (granted, error) in
if (granted) && (error == nil) {
let event = EKEvent(eventStore: eventStore)
event.title = "Countdown title"
event.startDate = Date()
event.notes = "Countdown is complete!"
event.isAllDay = false
event.calendar = eventStore.defaultCalendarForNewEvents
var frequency : EKRecurrenceFrequency = EKRecurrenceFrequency.daily
var interval = 1
switch repeatType {
case .daily:
//Repeat every day
frequency = EKRecurrenceFrequency.daily
interval = 1
break
case .weekly:
//Repeat every week
frequency = EKRecurrenceFrequency.weekly
interval = 1
break
case .biweekly:
//Repeat every 2 weeks
frequency = EKRecurrenceFrequency.weekly
interval = 2
break
case .monthly:
//Repeat every month
frequency = EKRecurrenceFrequency.monthly
interval = 1
break
case .quarterly:
//Repeat every 3 months
frequency = EKRecurrenceFrequency.monthly
interval = 3
break
case .halfYearly:
//Repeat every 6 months
frequency = EKRecurrenceFrequency.monthly
interval = 6
break
default:
// Repeat every year
frequency = EKRecurrenceFrequency.yearly
interval = 1
}
let alarm : EKAlarm = EKAlarm(relativeOffset: TimeInterval(exactly: 0)!)
event.addAlarm(alarm)
if interval > 0 {
event.addRecurrenceRule(EKRecurrenceRule(recurrenceWith: frequency, interval: interval, end: nil))
}
do {
try eventStore.save(event, span: .thisEvent)
} catch let error as NSError {
print(error.localizedDescription)
return
}
} else {
print(error?.localizedDescription ?? "no error")
}
})
请不要忘记导入EventKit
。