Creating and saving calendar events

Creating and saving calendar events

Create and configure calendar events to save them to the user calendar.

If you need to create an event in a user’s calendar from a SwiftUI app, you can use the EventKit framework. EventKit provides native access to the user’s device calendar, allowing you to create, read, and manage events directly.
Before interacting with the calendar, you must declare the reason for access in your app’s Info.plist.

Add the following key with a user-facing description:

  • Privacy – Calendars Full Access Usage Description (NSCalendarsFullAccessUsageDescription)

This message appears when iOS requests permission from the user to access the event data on the phone.

The main entry point for calendar operations is EKEventStore. Create a single instance and reuse it whenever possible.

import EventKit

let eventStore = EKEventStore()

On iOS 17+, use requestFullAccessToEvents(completion:) to prompt the user to grant read and write access to event data.

do {
    let granted = try await eventStore.requestFullAccessToEvents()
    if granted {
        print("Permission granted.")
    } else {
        print("Permission denied.")
    }
} catch {
    print("Error requesting access: \(error.localizedDescription)")
}

Note: This method is asynchronous because it depends on user interaction.

Once you guarantee that you’ve asked permission to access event data, you can start creating and saving events. To create a new event, create a new EKEvent linked to your event store:

let event = EKEvent(eventStore: eventStore)

Set the event details, such as title, start date, end date, and the calendar to which you want to save it. The default calendar for new events can be retrieved via the defaultCalendarForNewEvents property of the event store.

event.title = "Important Appointment"
event.startDate = Date() // Replace with your desired start date
event.endDate = Date().addingTimeInterval(3600) // 1 hour later
event.calendar = eventStore.defaultCalendarForNewEvents

Save the event using save(_:span:). The span parameter specifies whether you are saving only this instance (thisEvent) or future instances of a recurring event (futureEvents).

do {
    try eventStore.save(event, span: .thisEvent)
    print("Event saved!")
} catch {
    print("Error saving event: \(error.localizedDescription)")
}