Add Event to Calendar iOS Tutorial

Using the EventKit framework it is possible to access calendars on an iOS Device. In this tutorial an iCloud Calendar will be created an event will be added to this calendar. This tutorial is made with Xcode 10 and built for iOS 12.

Open Xcode and create a new Single View App.

For product name, use iOSAddEventTutorial and then fill out the Organization Name and Organization Identifier with your customary values. Enter Swift as Language and choose Next

First a new iCloud calendar need to be created. This can be done on an iOS Device or in OS X. Name the calendar ioscreator.

An event will be saved in this calendar so the user needs to grant access to the Calendar. Go to the Info.plist and add the following entry

Go to the ViewController.swift file and import the EventKit framework

import EventKit

Change the viewDidLoad method to

override func viewDidLoad() {
    super.viewDidLoad()
        
    // 1
    let eventStore = EKEventStore()
        
    // 2
    switch EKEventStore.authorizationStatus(for: .event) {
    case .authorized:
        insertEvent(store: eventStore)
        case .denied:
            print("Access denied")
        case .notDetermined:
        // 3
            eventStore.requestAccess(to: .event, completion:
              {[weak self] (granted: Bool, error: Error?) -> Void in
                  if granted {
                    self!.insertEvent(store: eventStore)
                  } else {
                        print("Access denied")
                  }
            })
            default:
                print("Case default")
    }
}
  1. An EKEventStore object is created. This represents the Calendar's database

  2. The authorizationStatus(for:) method returns the authorisation status

  3. If the status is not yet determined the user is prompted to deny or grant access using the requestAccess(to:completion:) method.

If the access is granted by the user the insertEvent(store:) method is called.

func insertEvent(store: EKEventStore) {
    // 1
    let calendars = store.calendars(for: .event)
        
    for calendar in calendars {
        // 2
        if calendar.title == "ioscreator" {
            // 3
            let startDate = Date()
            // 2 hours
            let endDate = startDate.addingTimeInterval(2 * 60 * 60)
                
            // 4
            let event = EKEvent(eventStore: store)
            event.calendar = calendar
                
            event.title = "New Meeting"
            event.startDate = startDate
            event.endDate = endDate
                
            // 5
            do {
                try store.save(event, span: .thisEvent)
            }
            catch {
               print("Error saving event in calendar")             }
            }
    }
}
  1. The calendars(for:) method returns all calendars that supports events

  2. Check if the previously created calendar "ioscreator" exists

  3. The event has a start date of the current time and an end date of 2 hours from the current time. (2 hours x 60 minutes x 60 seconds)

  4. An event is created with a title of "New meeting"

  5. The event is saved into the current calendar.

This tutorial can only run on an iOS Device, since the iOS Simulator ha no calendars. Build and Run the Project. The application needs to be authorised to access the calendar.

Next, ppen the Calendar app on an iOS Device or in Mac OS X. The New Meeting is added to the calendar.

You can download the source code of the IOS8SwiftAddEventTutorial at the ioscreator repository on Github.