How to differentiate between event saved through my app and event saved by using calendar app using EventKit?

125 Views Asked by At

I have successfully saved the events of my application to local calendar using Eventkit but now I want to delete the events that I have saved using my application only. But the below code give me list of all events saved on calendar but I just want to delete my own saved event. How can I do that?

 let predicate = eventStore.predicateForEvents(withStart: start, end: endDate, calendars: [calendar])       
 let events = eventStore.events(matching: predicate)
2

There are 2 best solutions below

1
Juri Noga On

When saving events to calendar you can store EKEvent's eventIdentifier property.

And when you want to delete events that were created by you, you can query them by identifiers.

Note: store eventIdentfier only after calling eventStore.save(...) method.

1
Bhavik Modi On

You can only remove event by comparing Event Title with fetched events:

    var allEvents: [EKEvent] = []

    let eventStore = EKEventStore()
    let calendars = eventStore.calendars(for: .event)

    for calendar in calendars {

        // end date (about) one year from now
        let endDate = Date(timeIntervalSinceNow: 60*60*24*365)

        let predicate = eventStore.predicateForEvents(withStart: Date(), end: endDate as Date, calendars: [calendar])

        let events = eventStore.events(matching: predicate)

        allEvents.append(contentsOf: events)

    }

    for event in allEvents {
        print(event.title, "in", event.calendar.title)
    }