Add Event to Calendar Tutorial in iOS8 with Swift

With EventKit framework makes it possible to access calendars on an iOS Device. In this tutorial we will create an iCloud Calendar, search the correct Calendar on the device and add an event into the calendar. This tutorial is built with iOS 8.1 and Xcode 6.1

Update: Apr 22, 2015. This tutorial is updated for Swift 1.2 and Xcode 6.3.

Open Xcode and create a new Single View Application. For product name, use IOS8SwiftAddEventTutorial and then fill out the Organization Name and Organization Identifier with your customary values. Enter Swift as Language and make sure only iPhone is selected in Devices.

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.

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

import EventKit

Change the viewDidLoad method to

override func viewDidLoad() {
    // 1
    let eventStore = EKEventStore()
    // 2
    switch EKEventStore.authorizationStatusForEntityType(EKEntityTypeEvent) {
    case .Authorized:
    case .Denied:
      println("Access denied")
    case .NotDetermined:
      // 3
      eventStore.requestAccessToEntityType(EKEntityTypeEvent, completion:
        {[weak self] (granted: Bool, error: NSError!) -> Void in
          if granted {
          } else {
            println("Access denied")
      println("Case Default")
  1. An EKEventStore object is created. This represents the Calendar's database
  2. The authorizationStatusForEntityType method returns the authorisation status
  3. If the status is not yet determined the user is prompted to deny or grant access using the requestAccessToEntityType(entityType:completion) method. 

If the access is granted by the user the insertEvent method is called.

func insertEvent(store: EKEventStore) {
    // 1
    let calendars = store.calendarsForEntityType(EKEntityTypeEvent)
      as! [EKCalendar]
    for calendar in calendars {
      // 2
      if calendar.title == "ioscreator" {
        // 3
        let startDate = NSDate()
        // 2 hours
        let endDate = startDate.dateByAddingTimeInterval(2 * 60 * 60)
        // 4
        // Create Event
        var event = EKEvent(eventStore: store)
        event.calendar = calendar
        event.title = "New Meeting"
        event.startDate = startDate
        event.endDate = endDate
        // 5
        // Save Event in Calendar
        var error: NSError?
        let result = store.saveEvent(event, span: EKSpanThisEvent, error: &error)
        if result == false {
          if let theError = error {
            println("An error occured \(theError)")
  1. The calendarsForEntityType returns all calendars that supports events
  2. We check if the previously crested 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, Open the Calendar 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.