在Swift中删除日历中的提醒

时间:2015-07-16 14:04:09

标签: ios swift eventkit

我使用以下功能来检索我的日历:

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!
    }

}

但是,每当我向日历添加新事件时,我都想检查它们是否已存在。我发现最简单的方法是删除所有提醒并再次加载新提醒。我试过了:

self.retrieveCalendar()?.reset()

但它不起作用。如何从日历中删除提醒?(一次一个或一次全部删除)

1 个答案:

答案 0 :(得分:2)

要查看提醒,您必须与fetchRemindersMatchingPredicate()predicateForRemindersInCalendarspredicateForIncompleteRemindersWithDueDateStarting:ending:calendars:

一起调用方法predicateForCompletedRemindersWithCompletionDateStarting:ending:calendars:

例如,如果您想要删除过去所有过期的提醒,请使用类似

的内容
  • 假设属性:

    var calendar : EKCalendar // current calendar
    let eventStore : EKEventStore // current event store
    
  • 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)
            }
        }
    }
    

在循环中,您可以检查更多条件