请注意,我们希望设置连续的事件集,以便轻松删除和编辑它们。
我写了以下代码:
let eventstore : EKEventStore = EKEventStore()
eventstore.requestAccess(to: EKEntityType.event, completion: { (isallow, error) -> Void in
var event:EKEvent!
if (event == nil) {
event = EKEvent(eventStore: eventstore)
event.title = self.getReminderTitleForCalendarEvent(dtDate: date)
event.notes = ""
event.startDate = date
event.endDate = date
event.calendar = eventstore.defaultCalendarForNewEvents
let ekrules: EKRecurrenceRule = EKRecurrenceRule.init(recurrenceWith: EKRecurrenceFrequency.monthly, interval:self.selectedFrequency!, end: nil)
event.recurrenceRules = [ekrules]
}
else // if there is already a event then update date
{
// if event is deleted by user
event.startDate = date
event.endDate = date
if let oldalarm = event.alarms?[0]// remove old date alarm
{
event.removeAlarm(oldalarm)
}
// Below code is to remove existing event
do
{
try eventstore.remove(event, span: .thisEvent)
//self.objselectedinfusion.strEventId = event.eventIdentifier
}
catch let error as NSError {
//print(error.localizedDescription)
}
}
let alarm = EKAlarm(relativeOffset: self.getTimeIntervalForAlert())
let alarm = EKAlarm(absoluteDate: reminderFireDate as Date)
event.addAlarm(alarm)
do
{
try eventstore.save(event, span: EKSpan.futureEvents, commit: true)
UserDefaults.standard.set(event.eventIdentifier, forKey: Constant.calendarEventID)
}
catch let error as NSError {
//print(error.localizedDescription)
}
})
此处selectedFrequency
是重复间隔,即几个月。
答案 0 :(得分:0)
class func getRepeatValue (_ option : String) -> EKRecurrenceRule?{
// ["Daily" , "Weekly" , "Monthly" ,"Yearly","None"]
// "daily" , "weekly" , "monthly" ,"yearly","none"
switch option {
case "Daily":
let rule = EKRecurrenceRule(recurrenceWith: EKRecurrenceFrequency.daily, interval: 1, end: nil)
//daily for 50 years
return rule
case "Weekly":
//on the same week day for 50 years
let rule = EKRecurrenceRule(recurrenceWith: EKRecurrenceFrequency.weekly, interval: 1, end: nil)
return rule
case "Monthly":
//on the same date of every month
let rule = EKRecurrenceRule(recurrenceWith: EKRecurrenceFrequency.monthly, interval: 1, end: nil)
return rule
case "Yearly":
//on the same date and month of the year
let rule = EKRecurrenceRule(recurrenceWith: EKRecurrenceFrequency.yearly, interval: 1, end: nil)
return rule
case "None":
return nil
default:
return nil
}
}