简体   繁体   English

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

[英]how to remove certain / Specific event using EventKit

I need to remove an event with certain/specific title, I hope that I can delete/remove the event based on the eventID/Identifier. 我需要删除具有特定标题的事件,希望我可以基于eventID / Identifier删除/删除该事件。 but I don't know how to do that in code. 但是我不知道如何在代码中做到这一点。 I don't know how to give identifier to the event and remove it based on their identifier/title. 我不知道如何为事件提供标识符并根据其标识符/标题将其删除。

here is the code I use to save the event: 这是我用来保存事件的代码:

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

I know that I can use evenStore.remove() , but that method needs EKEvent instance. 我知道我可以使用evenStore.remove() ,但是该方法需要EKEvent实例。 I don't understand how to remove a specific event if using that method, it will be easier if I can remove the event based on their identifier 我不了解如何使用该方法删除特定事件,如果我可以根据事件的标识符删除事件,会更容易

Actually an EKEvent instance has a get-only attribute called eventIdentifier . 实际上, EKEvent实例具有一个名为eventIdentifier的仅获取属性。 You can't modify this identifier, but you can get it after you save the event . 您无法修改此标识符,但是可以在保存事件之后获取它。 So: 所以:

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

Then you can get the event using its identifier 然后,您可以使用其标识符获取事件

let event = eventStore.event(withIdentifier: id)

and then pass this EKEvent to eventStore.remove() 然后将此EKEvent传递给eventStore.remove()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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