[英]how to remove certain / Specific event using EventKit
我需要刪除具有特定標題的事件,希望我可以基於eventID / Identifier刪除/刪除該事件。 但是我不知道如何在代碼中做到這一點。 我不知道如何為事件提供標識符並根據其標識符/標題將其刪除。
這是我用來保存事件的代碼:
let eventStore = EKEventStore()
let newEvent = EKEvent(eventStore: eventStore)
newEvent.calendar = eventStore.defaultCalendarForNewEvents
newEvent.title = self.eventNameTextField.text ?? "Some Event Name"
newEvent.startDate = timeDatePicker.date
newEvent.endDate = endTimeDatePicker.date
newEvent.notes = "Ini adalah catatan"
newEvent.location = "Jalan Sunda kelapa no.60"
let eventAlarm = EKAlarm(relativeOffset: -60 * 10) // 10 minutes before the start date
newEvent.alarms = [eventAlarm]
do {
try eventStore.save(newEvent, span: .thisEvent)
print("Event has been saved")
} catch {
let alert = UIAlertController(title: "Event could not be saved", message: (error as NSError).localizedDescription, preferredStyle: .alert)
let OKAction = UIAlertAction(title: "OK", style: .default, handler: nil)
alert.addAction(OKAction)
self.present(alert, animated: true, completion: nil)
}
我知道我可以使用evenStore.remove()
,但是該方法需要EKEvent
實例。 我不了解如何使用該方法刪除特定事件,如果我可以根據事件的標識符刪除事件,會更容易
實際上, EKEvent
實例具有一個名為eventIdentifier
的僅獲取屬性。 您無法修改此標識符,但是可以在保存事件之后獲取它。 所以:
do {
try eventStore.save(newEvent, span: .thisEvent)
let id = newEvent.eventIdentifier ?? "NO ID"
//Save your ID in your database or anywhere else so you can retrieve the event later
print("Event has been saved with id \(id)")
} catch {
let alert = UIAlertController(title: "Event could not be saved", message: (error as NSError).localizedDescription, preferredStyle: .alert)
let OKAction = UIAlertAction(title: "OK", style: .default, handler: nil)
alert.addAction(OKAction)
self.present(alert, animated: true, completion: nil)
}
然后,您可以使用其標識符獲取事件
let event = eventStore.event(withIdentifier: id)
然后將此EKEvent
傳遞給eventStore.remove()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.