2023 App ServicesSystem Services
WWDC23 · 22 min · App Services / System Services
Discover Calendar and EventKit
Discover how you can bring Calendar into your app and help people better manage their time. Find out how to create new events from your app, fetch events, and implement a virtual conference extension. We’ll also take you through some of the changes to calendar access levels that help your app stay connected without compromising the privacy of someone’s calendar data.
Watch at developer.apple.com ↗Chapters
Code shown on screen · 5 snippets
Adding an event with EventKitUI
// Create an event store
let store = EKEventStore()
// Create an event
let event = EKEvent(eventStore: store)
event.title = "WWDC23 Keynote"
let startDateComponents = DateComponents(year: 2023, month: 6, day: 5, hour: 10)
let startDate = Calendar.current.date(from: startDateComponents)!
event.startDate = startDate
event.endDate = Calendar.current.date(byAdding: .hour, value: 2, to: startDate)!
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."
// Create a view controller
let eventEditViewController = EKEventEditViewController()
eventEditViewController.event = event
eventEditViewController.eventStore = store
eventEditViewController.editViewDelegate = self
// Present the view controller
present(eventEditViewController, animated: true) Siri Event Suggestions
// Create an INReservation
let spokenPhrase = “Lunch at Caffè Macs”
let reservationReference = INSpeakableString(vocabularyIdentifier: "df9bc3f5",
spokenPhrase: spokenPhrase,
pronunciationHint: nil)
let duration = INDateComponentsRange(start: myEventStart, end: myEventEnd)
let location = CLPlacemark(location: myCLLocation,
name: "Caffè Macs",
postalAddress: myAddress)
let reservation = INRestaurantReservation(itemReference: reservationReference,
reservationStatus: .confirmed,
reservationHolderName: "Jane Appleseed",
reservationDuration: duration,
restaurantLocation: location)
// Create an intent and response
let intent = INGetReservationDetailsIntent(reservationContainerReference:
reservationReference)
let intentResponse = INGetReservationDetailsIntentResponse(code: .success, userActivity: nil)
intentResponse.reservations = [reservation]
// Create an INInteraction
let interaction = INInteraction(intent: intent, response: intentResponse)
// Donate the interaction to the system
interaction.donate() Adding an event with write-only access
// Create an event store
let store = EKEventStore()
// Request write-only access
guard try await store.requestWriteOnlyAccessToEvents() else { return }
// Create an event
let event = EKEvent(eventStore: store)
event.calendar = store.defaultCalendarForNewEvents
event.title = "WWDC23 Keynote"
event.startDate = myEventStartDate
event.endDate = myEventEndDate
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."
// Save the event
guard try eventStore.save(event, span: .thisEvent) else { return } Fetch events
// Create an event store
let store = EKEventStore()
// Request full access
guard try await store.requestFullAccessToEvents() else { return }
// Create a predicate
guard let interval = Calendar.current.dateInterval(of: .month, for: Date()) else { return }
let predicate = store.predicateForEvents(withStart: interval.start,
end: interval.end,
calendars: nil)
// Fetch the events
let events = store.events(matching: predicate)
let sortedEvents = events.sorted { $0.compareStartDate(with: $1) == .orderedAscending } Virtual conference extension
// Create the extension target
class VirtualConferenceProvider: EKVirtualConferenceProvider {
// Provide the room types
override func fetchAvailableRoomTypes() async throws ->
[EKVirtualConferenceRoomTypeDescriptor] {
let title = "My Room"
let identifier = "my_room"
let roomType = EKVirtualConferenceRoomTypeDescriptor(title: title, identifier: identifier)
return [roomType]
}
// Provide the virtual conference
override func fetchVirtualConference(identifier: EKVirtualConferenceRoomTypeIdentifier)
async throws -> EKVirtualConferenceDescriptor {
let urlDescriptor = EKVirtualConferenceURLDescriptor(title: nil, url: myURL)
let details = "Enter the meeting code 12345 to enter the meeting."
return EKVirtualConferenceDescriptor(title: nil,
urlDescriptors: [urlDescriptor],
conferenceDetails: details)
}
} Resources
Related sessions
-
33 min -
30 min