Deleting reminders from calendar in Swift

人盡茶涼 提交于 2019-12-25 05:48:10

问题


I use the following function to retrieve my calendar:

func retrieveCalendar() -> EKCalendar? {

    appDelegate = UIApplication.sharedApplication().delegate
        as? AppDelegate

    var myCalendar:  EKCalendar?
    let calendars = appDelegate!.eventStore!.calendarsForEntityType(EKEntityTypeReminder) as! [EKCalendar]
    let filteredCalendars = calendars.filter {$0.title == "MedicalCalendar"}
    if filteredCalendars.isEmpty {
        println("could not find reminder calendar 'MedicalCalendar'")
        return nil
    } else {
        myCalendar = filteredCalendars[0]
        return myCalendar!
    }

}

However, anytime I add new events to the calendar I'd like to check if they already exist there. I figured out that the easiest approach would be to delete all reminders and load new ones again. I tried:

self.retrieveCalendar()?.reset()

But it does not work. How can I remove reminders from calendar?(either one at a time or all of them at once)


回答1:


To check reminders you have to call the method fetchRemindersMatchingPredicate() in conjunction with predicateForRemindersInCalendars or predicateForIncompleteRemindersWithDueDateStarting:ending:calendars: or predicateForCompletedRemindersWithCompletionDateStarting:ending:calendars:

For example if you want to delete all expired reminders in the past until now, use something like this

  • assumed properties:

    var calendar : EKCalendar // current calendar
    let eventStore : EKEventStore // current event store
    
  • code

    func removeExpiredReminders() {
    
         let pastPredicate = eventStore.predicateForIncompleteRemindersWithDueDateStarting(nil, ending:NSDate(), calendars:[calendar])
    
        eventStore.fetchRemindersMatchingPredicate(pastPredicate) { foundReminders in
    
            let remindersToDelete = !foundReminders.isEmpty
            for reminder in foundReminders  as! [EKReminder] {
                self.eventStore.removeReminder(reminder, commit: false, error: nil)
            }
            if remindersToDelete {
                self.eventStore.commit(nil)
            }
        }
    }
    

in the loop you can check for further conditions



来源:https://stackoverflow.com/questions/31456504/deleting-reminders-from-calendar-in-swift

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!