EventKit
Accessing different types of calendars
Section titled “Accessing different types of calendars”Accessing the array of calendars
Section titled “Accessing the array of calendars”To access the array of EKCalendars, we use the calendarsForEntityType method:
let calendarsArray = eventStore.calendarsForEntityType(EKEntityType.Event) as! [EKCalendar]Iterating through calendars
Section titled “Iterating through calendars”Just use a simple for loop:
for calendar in calendarsArray{ //...}Accessing the calendar title and color
Section titled “Accessing the calendar title and color”let calendarColor = UIColor(CGColor: calendar.CGColor)let calendarTitle = calendar.titleObjective-C
Section titled “Objective-C”UIColor *calendarColor = [UIColor initWithCGColor: calendar.CGColor];NSString *calendarTitle = calendar.title;Requesting Permission
Section titled “Requesting Permission”Your app can’t access your reminders and your calendar without permission. Instead, it must show an alert to user, requesting him/her to grant access to events for the app.
To get started, import the EventKit framework:
import EventKitObjective-C
Section titled “Objective-C”#import <EventKit/EventKit.h>Making an EKEventStore
Section titled “Making an EKEventStore”Then, we make an EKEventStore object. This is the object from which we can access calendar and reminders data:
let eventStore = EKEventStore()Objective-C
Section titled “Objective-C”EKEventStore *eventStore = [[EKEventStore alloc] init];Note
Making an `EKEventStore` object every time we need to access calendar is not efficient. Try to make it once and use it everywhere in your code.Checking Availability
Section titled “Checking Availability”Availability has three different status: Authorized, Denied and Not Determined. Not Determined means the app needs to grant access.
To check availability, we use authorizationStatusForEntityType() method of the EKEventStore object:
switch EKEventStore.authorizationStatusForEntityType(EKEntityTypeEvent){ case .Authorized: //... case .Denied: //... case .NotDetermined: //... default: break}Objective-C
Section titled “Objective-C”switch ([EKEventStore authorizationStatusForEntityType:EKEntityTypeEvent]){ case EKAuthorizationStatus.Authorized: //... break; case EKAuthorizationStatus.Denied: //... break; case EKAuthorizationStatus.NotDetermined: //... break; default: break;}Requesting Permission
Section titled “Requesting Permission”Put the following code in NotDetermined case:
eventStore.requestAccessToEntityType(EKEntityTypeEvent, completion: { [weak self] (userGrantedAccess, _) -> Void in if userGrantedAccess{ //access calendar }}Adding an event
Section titled “Adding an event”Creating the event object
Section titled “Creating the event object”var event = EKEvent(eventStore: eventStore)Objective-C
Section titled “Objective-C”EKEvent *event = [EKEvent initWithEventStore:eventStore];Setting related calendar, title and dates
Section titled “Setting related calendar, title and dates”event.calendar = calendarevent.title = "Event Title"event.startDate = startDate //assuming startDate is a valid NSDate objectevent.endDate = endDate //assuming endDate is a valid NSDate objectAdding event to calendar
Section titled “Adding event to calendar”try { do eventStore.saveEvent(event, span: EKSpan.ThisEvent)} catch let error as NSError { //error}Objective-C
Section titled “Objective-C”NSError *error;BOOL *result = [eventStore saveEvent:event span:EKSpanThisEvent error:&error];if (result == NO){ //error}