How to Get All Events Out of a Calendar (Swift)

Swift 4 How to get all events from calendar?

In case anyone is still wondering, Apple limited this to four years intentionally.

From predicateForEvents(withStart:end:calendars:):

For performance reasons, this method matches only those events within a four year time span. If the date range between startDate and endDate is greater than four years, it is shortened to the first four years.

If you want a range longer than that, I guess you'll have to wrap this in your own function to split it into four year chunks.

How to delete all events from the default calendar

There is no removeAll method.

You need to use the methods of EKEventStore to query and remove the desired events.

At a high level you need to:

  1. Request authorization to access calendar events.
  2. Get a reference to desired the EKCalendar.
  3. Create a predicate for the events you wish to query. You need a date range and the calendar.
  4. Enumerate the events that match the predicate.
  5. Delete each enumerated event.

All of the needed APIs are in the EKEventStore class. See its documentation for specifics.

How to fetch my app's events from calendar?

func getEvents() -> [EKEvent] {
var allEvents: [EKEvent] = []

// calendars
let calendars = self.eventStore.calendars(for: .event)

// iterate over all selected calendars
for (_, calendar) in calendars.enumerated() where isCalendarSelected(calendar.calendarIdentifier) {

// predicate for today (start to end)
let predicate = self.eventStore.predicateForEvents(withStart: self.initialDates.first!, end: self.initialDates.last!, calendars: [calendar])

let matchingEvents = self.eventStore.events(matching: predicate)

// iterate through events
for event in matchingEvents {
allEvents.append(event)
}
}

return allEvents
}

this code will gvie you all events and then you have to filter it by title or something which can be identify that this event is from your app :)

Swift 4.2 EventKit get all Events to the selected calendar on TableView didSelectedRowAt

You have to pass the calendar in the calendars parameter of predicateForEvents

let rowCalendar = calendars![indexPath.row]
let calendarTitle = rowCalendar.title
print(calendarTitle)

...

if let anAgo = oneDayAgo, let aNow = oneDayFromNow {
predicate = eventStore.predicateForEvents(withStart: anAgo, end: aNow, calendars: [rowCalendar])
}

And please declare the data source array as non-optional empty array as you are force unwrapping it anyway

var calendars = [EKCalendar]()


Related Topics



Leave a reply



Submit