---
title: "EventKit bites - Little Bites of Cocoa"
description: "Little Bites of Cocoa about EventKit: short tips and techniques for iOS and Mac development, each a brief explanation of one concept, technique or tool."
url: "https://littlebitesofcocoa.com/topics/51-eventkit"
author: "Jake Marsh"
---
# Bites about: [EventKit](https://littlebitesofcocoa.com/topics/51-eventkit)

Topics

- [EventKit](https://littlebitesofcocoa.com/topics/51-eventkit)

## [#97: EventKit Alarms 📅⏰](https://littlebitesofcocoa.com/97-eventkit-alarms)

 Tuesday October 6th, 2015

Topics

- [EventKit](https://littlebitesofcocoa.com/topics/51-eventkit)

*EventKit Alarms* are how we configure **notifications** and **alerts** that will be triggered to remind the user about calendar **events** or **reminders**.

Like in **Bite #96**, we'll use [Timepiece](http://git.io/timepiece) ([Bite #3](https://littlebitesofcocoa.com/3-timepiece)) to help us compose **dates**. We'll create an **event** for when our cookies will be done baking. The **event** will start in 12 minutes, and we'll add a new *EKAlarm* to it that triggers 5 seconds before the **event**. **Alarms** can be created with at **absolute** or **relative** times.

```swift
let e = EKEvent(eventStore: eventStore)

e.startDate = NSDate() + 12.minutes
e.endDate = e.startDate + 30.seconds
e.calendar = eventStore.defaultCalendarForNewEvents
e.title = "Cookies are Done! 🍪"

e.addAlarm(EKAlarm(relativeOffset: -5.0))

try eventStore.saveEvent(e, span: .ThisEvent)
```

Interestingly, **OS X** is actually ahead of **iOS** here. *EKAlarm* on **OS X **has more **properties** for configuring a sound to play, an email address to notify and more.

Now when we get to Disneyland, the **alarm** will remind us to head over to . (Just an example, in real life we'd never need the reminder).

Things get more fun when adding **alarms** to **reminders**. Let's finish by adding an **alarm** to a new **reminder** that triggers when the user arrives somewhere:

```swift
let reminder = EKReminder(eventStore: eventStore)

reminder.title = "Ride Star Tours“
reminder.calendar = eventStore.defaultCalendarForNewReminders()

let location = EKStructuredLocation(title: "Disneyland")
location.geoLocation = CLLocation(
  latitude: 33.8120918, longitude: -117.9189742
)

let alarm = EKAlarm()

alarm.structuredLocation = location
alarm.proximity = .Enter

reminder.addAlarm(alarm)

try eventStore.saveReminder(reminder, commit: true)
```

Topics

- [EventKit](https://littlebitesofcocoa.com/topics/51-eventkit)

## [#96: EventKit Basics 📅](https://littlebitesofcocoa.com/96-eventkit-basics)

 Monday October 5th, 2015

Topics

- [EventKit](https://littlebitesofcocoa.com/topics/51-eventkit)

*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-timepiece)) 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)
```

 

 
