Shaideru

All things iOS & Swift

Working with EventKit Framework [Swift]

Download the project file here

There are classes from EK Framework designed specifically to access the calendar database to organize the events, reminders and alarms. These classes are EKCalendar, EKEventStore, EKReminder and EKAlarm.

Creating a project file with Xcode
Create a new project, name it whatever you want.

Product Name: reminderTest
Organization Name: yourName
Organization Identifier: com.yourName
Bundle Identifier: automatically filled
Language: Swift
Devices: iPhone
Use Core Data: Unchecked

click next and save it!

Design your UI like below
Screenshot 2015-02-21 12.19.44
textfield
button: “Set Reminder”
date picker

Ctrl-click and drag all your objects to the assistant editor and release it.
@IBOutlet weak var reminderTitle: UITextField!
@IBOutlet weak var reminderDatePicker: UIDatePicker!
@IBAction func setReminder(sender: AnyObject){}

Next of, create a new swift file called EventKitFunctions, import EvenKit.
type in all of the code below inside your swift file

In order to access calendar database, create an instance of EKEventStore class.
var calendarDatabase = EKEventStore()

Here’s a code to access reminders in the calendar database and reports error if the access is denied.
func eventStoreAccessReminders() {

calendarDatabase.requestAccessToEntityType(EKEntityTypeReminder, completion: {(granted: Bool, error:NSError!) -> Void in
if !granted {
println(“Access to store not granted”)
}
})
}

once the application has been accepted or denied, go to settings->Prvacy, you’ll see the name of your reminder app toggled on or off, depends if you accepted or denied.

if you want to change the message when system request access message pops up, go to info.plist file and add a new row, key “Privacy – Reminders Usage Description” and value or the custom message you want.

Accessing Calendars in the Database

enter the following inside your swift file

func accessCalendarInTheDatabase() {
var calendars = calendarDatabase.calendarsForEntityType(EKEntityTypeReminder)

for calendar in calendars as [EKCalendar] {
println(“Calendar = (calendar.title)”)
}
}

Creating Reminders

Code below creates a reminder and store it to the default calendar for new reminder entries.

func createReminder(reminderTitle: String, reminderDate: NSDate) {
let reminder = EKReminder(eventStore: calendarDatabase)

reminder.title = reminderTitle
let date = reminderDate
let alarm = EKAlarm(absoluteDate: date)

reminder.addAlarm(alarm)

reminder.calendar = calendarDatabase.defaultCalendarForNewReminders()

var error: NSError?

if error != nil{
println(“errors: (error?.localizedDescription)”)
}

calendarDatabase.saveReminder(reminder, commit: true, error: &error)
}

You created an instance of EKReminder object named reminder, after that, we configured the reminder to store it in the user defaults calendar before saving.

reminders can be triggered at a specific time and date, or when the user embarks into a certain location or departs from a geographical location, concept is known as geofencing.

go to your viewController file, and enter the code below inside your actions button

@IBAction func setReminder(sender: AnyObject) {

calendarDatabase.requestAccessToEntityType(EKEntityTypeReminder, completion: { (granted, error) -> Void in
if !granted {
println(“Access to store not granted”)
println(error.localizedDescription)
} else {
println(“Reminder entered: (self.reminderTitle.text), (self.reminderDatePicker.date)”)
}

})

createReminder(reminderTitle.text, reminderDatePicker.date)

}

extra: to get rid of the keyboard when you’re done using textfield

override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
reminderTitle.endEditing(true)
}

build and run your project and add a reminder, after adding, go to your reminder application and you’ll see the reminder you added.

2 Discussions on
“Working with EventKit Framework [Swift]”
  • Hello there… have you done an alarm one yet?? one that uses EkAlarm – I’m finding that the reminders aren’t powerful enough to get the user’s attention. thanks.

Leave A Comment

Your email address will not be published.