EventKit & EventKitUI

İlayda Şahin
3 min readNov 30, 2023

--

Hello folks! If you are looking for an information about usages and integration of EventKit & EventKitUI, I think this article can be what you need! Keep reading 🚀

EventKit and EventKitUI are frameworks provide access the calendar, display and modify calendar items in an app.

Even these frameworks can be used for the same aim in the applications, there are differences between usage of them.

  • EventKitUI provides view controllers for viewing and editing calendar data. Native iOS calendar screen is displayed and users can select repetition, set alert and add the event to the calendar they select.
  • EventKit, does not provide view controllers to manage calendar data. It can be used for more practical actions.
  • The other important difference is about access permission. With EventKitUI, apps can add events with no permission after iOS17.
  • But with EventKit apps need to get write only access permission.

❗️But of course, there is need full access to fetch or update existing events for both two frameworks.

Usage of EventKit

// Firstly we need to create an event store
let store = EKEventStore()

// Then create an event and set the details
let event = EKEvent(eventStore: store)
event.calendar = store.defaultCalendarForNewEvents
event.title = "WWDC23 Keynote"
let startDateComponents = DateComponents(year: 2023, month: 6, day: 5, hour: 20)
let startDate = Calendar.current.date(from: startDateComponents)
event.endDate = Calendar.current.date(byAdding: .hour, value: 2, to: startDate ?? .now)
event.timeZone = TimeZone(identifier: "America/Los_Angeles")
event.location = "1 Apple Park Way, Cupertino, CA, United States"
event.notes = "Kick off an exhilarating week of technology and community."

After the preparation of event, we need to ask for write-only access to users. If the user accept the request, then the event can be saved successfully, otherwise it cannot be.

guard try await store.requestWriteOnlyAccessToEvents() else { return }

❗️For write-only access, NSCalendarsWriteOnlyAccessUsageDescription must be added to Info.plist.

try self.store.save(wwdcEvent, span: .thisEvent)

Usage of EventKitUI

For this process, an event is first created and the details of this event are set as in EventKit. After that EKEventEditViewController should be created and presented. This view controller is the event editing screen we are used to from the calendar app.

let eventEditViewController = EKEventEditViewController()
eventEditViewController.event = techMeetingEvent
eventEditViewController.eventStore = store
eventEditViewController.editViewDelegate = self
present(eventEditViewController, animated: true)

Fetching Events

❗️To fetch existing events from the calendar, there is need to full access request. For that, NSCalendarsFullAccessUsageDescription must be added to Info.plist. Only if the app needs to display events, update or remove events then there should be full access request.

guard try await store.requestFullAccessToEvents() else { return }

Then, a time period must be created to specify which time period events will be fetched. The calendars parameter is to search for events in, but it can be nil to search events in all calendars.

guard let interval = Calendar.current.dateInterval(of: .year, for: Date()) else { return }
let predicate = store.predicateForEvents(withStart: interval.start,
end: interval.end,
calendars: nil)
let events = store.events(matching: predicate)

I hope you find this information helpful for your EventKit&EventKitUI exploration.

And for those who are eager to explore the practical side, the small example is available on GitLab. Feel free to check it out https://github.com/ilaydasahinn/EventKitAndEventKitUI.

If you have any questions, thoughts, or even if you just want to share your own experiences in this field, I’d love to hear from you!🤜🏼🤛🏼

Happy coding! 💻🔍🌟

References

--

--