---
title: "#96: EventKit Basics 📅"
url: https://littlebitesofcocoa.com/96-eventkit-basics
author: Jake Marsh
published: 2015-10-05
topics: ["EventKit"]
description: "EventKit is how we access and interact with a user's calendars and events. It has APIs to manage events, reminders, alarms and even participants."
---

# #96: EventKit Basics 📅

By Jake Marsh, October 5, 2015. HTML: https://littlebitesofcocoa.com/96-eventkit-basics

_EventKit_ is how we access and interact with a user's **calendars** and **events**. It has APIs to manage **events**, **reminders**, **alarms** and even **participants**. Today we'll take a look at the basics. Let's get started.

## Authorization

Before doing anything fun, we'll need to check the current **authorization status**, then request permission if needed.

``` swift
switch EKEventStore.authorizationStatusForEntityType(.Event) {
case .Authorized:
  fetchCalendars()
case .NotDetermined:
  requestAccessToCalendars()
case .Restricted, .Denied:
  showAccessDeniedAlert()
}
```

To do anything interesting, we'll need an **EKEventStore**. 

``` swift
let eventStore = EKEventStore()
```

Apple suggests we keep a long-lived instance around, so we'll keep ours in a **property**. Now we can write our  **function**:

``` swift
eventStore.requestAccessToEntityType(.Event) { (granted, error) in
  guard granted else { return }
  self.fetchCalendars()
}
```

## Fetching Calendars & Events

``` swift
let calendars = eventStore.calendarsForEntityType(.Event)
```

Lastly, we'll use the handy  **function** to put new **events** where the user expects:

We'll use [Timepiece](http:/git.io/timepiece) ([Bite #3](https://littlebitesofcocoa.com/3)) to help us compose **dates**. Then we'll create a **predicate** to look for **events** in the last week, across all the user's **calendars**.

## Adding an Event

``` swift
let event = EKEvent(eventStore: eventStore)

event.title = "Drop off Carbonite Shipment"
event.startDate = "3031-02-15".dateFromFormat("yyyy-MM-dd")
event.calendar = eventStore.defaultCalendarForNewEvents

try eventStore.saveEvent(event, span: .ThisEvent)
```
