繁体   English   中英

如何使用EventKit删除某些/特定事件

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM