EventKit reminders manager : How to retrieve, create, and edit reminders from within your app in Swift

Event Kit framework is designed to grant developers access to data generated by both Calendar and Reminders system apps. The framework provides a set of API to manipulate the Calendar database, that same database which store all Calendar/Reminders informations of our iOS devices.

The power side of Event Kit is that it allows, not only to read your reminders and events data from within your app, but also it grants you access to manage such data, whether by deleting, adding, or editing Calendar and Reminders app informations.

In this tutorial, you gonna build a pretty neat app to manage your reminders data. Along the way, you will learn how to access the Calendar database to manipulate this set of informations in read and write.

Without further ado, as usual, let’s do this!

Before we dive in, download the starter project here.

Open up the project and take a look at the interfaces in the storyboard file. It mainly has 3 screens. The main screen will show the reminders in a table view. The two remaining screens will allow to add a new reminder to the Calendar database and edit an existing one.

As I said earlier, both reminders and events are stored and managed inside the Calendar database. Basically, you gonna query it to retrieve all your reminders.

Calendar Database stores the events and reminders informations

Let’s start and extract all the reminders from your device to show them in the app.

Select the ViewController.swift file from the Project navigator view, and add the following properties declaration after the class name:

The eventStore property represents a reference to the Calendar database, while the reminders property is an array that will store all the queried reminders before populating into the table view.

Also, don’t forget to import the EventKit framework at the top of the class:

Next, you will override the viewWillAppear method and implement the required code to fetch all the reminders saved in the Calendar database. To do so, locate the viewWillAppear method as change it as below:

Let’s explain the above code:

// 1: Before you access the reminders in the event store, you need to get the user permission. The requestAccessToEntityType API will run asynchronously and call the completion block when the request is completed. The block will pass two arguments, the first to check whether the user has given permission to the app to access the reminders, and the second will store an error object for a backtrace if something went wrong during the operation.

// 2: Once permission is granted to the app, it will fetch all reminders asynchronously and return the results in a completion block as well. At that time, you assign the reminders objects to the array property and reload the table view to populate the data.

Run the app, for the first time you should get the permission alert popping up, make sure to allow the app and grant it permission.

grant permission to the app to access reminders

Cool, now let’s implement some UITableViewDataSource protocol methods in order to reload the table view with data.

Always in the ViewController.swift file, place the following code before the closing bracket of the class:

The cellForRowAtIndexPath protocol method will iterate through the reminders array and display each reminder’s title along with the formatted due date inside the cell.

Also, it’s important to make the class conform to the table view data source protocol, as well as the table view delegate protocol (you will implement its methods later). To do this, change the class declaration to the following:

Before you go, set the delegate and datasource of the table view to the view controller, copy the following statements inside the viewDidLoad method:

So far so good, run the app and make sure your reminders are loaded. If the table view is empty, make sure to create some reminders in the Reminders app in your device and run the app again.

Here is my reminders retrieved inside the app, I got three in the queue πŸ™‚

Reminders app in iOS

Remove a reminder:

Removing a reminder is even simpler than fetching reminders. Basically, you will call the removeReminder method on the event store object to synchronise the delete action with the Calendar database.

Go ahead and place the following data source protocol method implementation inside the ViewController.swift file:

The data source method above will run once you edit one of the table view rows. It will first get the EKReminder object in the selected row, then try to remove it from the event store (and hence from the Calendar database). The remove operation may throw errors, that’s why you run the code inside a do-catch block. If the reminder is removed successfully and the data are synchronized with the Calendar database, you then remove the EKReminder object from the table view and its data source array.

With the method above, you can swipe left on a cell to reveal the Delete button. Let’s also toggle the edit mode programmatically with a button click. Locate the editTable action method and implement the following code inside:

The action method is attached to the edit bar button item on the left of the navigation bar.

Run the app and switch the table view to the edit mode, either by swiping left on a cell or by selecting the edit button on the navigation bar. Delete a reminder and switch back to the Reminders app in your device to make sure the removed reminder is also synchronized with the Calendar database πŸ™‚

Create a new reminder:

Let’s extend our app to add a new reminder to the event store. If you run the app and try to click on the “+” button at the right of the navigation bar, another screen will show up, I already set up the segue in storyboard.

What you will do now is to pass the event store object to the next controller so that it will be used to create the new reminder.

Place the following code before the closing bracket of the ViewController.swift file:

The method above will run right after a segue is being performed and right before the next controller is shown. The code you used inside will cast the destination controller to the NewReminder class, and assign the event store to its relevant EKEventStore property.

Select the NewReminder.swift file from the Project navigator view to open it in the editor. Now, add the following property declaration right after the class name:

Also, don’t forget to import the EventKit framework at the top of the file:

You are now in the new reminder screen, the event store is passed and can be used from within the NewReminder class.

In this screen, a UIDatePicker should be loaded once you click on the text field, so let’s set it up. Make the following changes to the NewReminder.swift file:

This code will create a UIDatePicker instance and set it to show up on the text field click, it will also call the addDate function each time the date is changed in the picker. Finally, you just set the reminderTextView as the first responder so you focus directly on typing in your new reminder details.

Let’s quickly add the triggered addDate function, place the following implementation inside the class:

Now, locate the action method called dismiss and place the following statement inside:

This will be attached to the Cancel button on the left of the navigation bar to dismiss the current screen and move back to the root controller.

Next, locate the method called saveNewReminder and implement the following code inside:

Let’s breakdown the code to better understand how it works:

// 1: This will create a new EKReminder instance with the event store property you previously passed to the view controller. Then it will set the title, dueDateComponents and calendar properties of the reminder with the relevant values. Note that you used a helper method called dateComponentFromNSDate to convert the UIDatePicker NSDate content to an NSDateComponents object and assign it to the dueDateComponents property.

// 2: Every Calendar database operation is done by the event store. Here, you just used the saveReminder API with the EKReminder object in argument. The API will try to save the reminder and if it succeeds, the screen will be dismissed to reveal the root view with all the current and new reminders in the table view.

Before you run the project, let’s quickly implement the helper method in the app delegate. Select AppDelegate.swift file from the Project navigator view, and place the following code inside:

Good, run the app and try to add a new reminder. After saving it, make sure it shows up in the table view as well as in the Reminders app in your device πŸ™‚

Edit a reminder:

Editing a reminder is as easy as creating a new one! Let’s do this and I will let you judge by yourself πŸ˜‰

Switch back to the ViewController.swift file and add the following property declaration to keep reference of the reminder you want to edit:

Next, implement the following table view delegate method to track the click on the accessory button of the cell and launch the transition to the details screen of the reminder:

The ShowReminderDetails is the segue identifier I set in storyboard to move from the root to the details screen.

Nothing fancy here except that there is now two segues that need to be managed. To distinguish between the two segues, locate the prepareForSegue method and update its implementation to the following:

This way, the method will know which segue is initiated based on the identifier set from the view controller’€™s storyboard file.

You are almost done, select the ReminderDetails.swift file from the Project navigator view, and make the following changes, this is almost the same work done for the NewReminder controller (I commented each code placement):

And finally, implement the addDate method:

That’s it!

Run the app on your device. Add, remove and edit your reminders, but most of all, enjoy your work πŸ˜‰

As usual, you can download the final project here.

Feel free to leave a comment below, I always enjoy to hear from you!

If you got any question about this tutorial, feel free to ask me in the official forum thread here. Don’t hesitate to open a new forum thread if you got any other questions to ask, I am here to help.

About Malek

Malek is a passionate iOS Engineer and Founder of Medigarage Studios, a small mobile games startup. I started my iOS adventures in 2011 and since then I fell in love with it. You can hire me for your project, get in touch by Email to discuss further details. Also, feel free to reach out on Twitter and Google+.

  • Pingback: Android & iOS Application Development | Just another My blog Sites site()

  • Aveia

    Thanks! Save me!

  • Malek_T

    You welcome!

  • Simranpreet Singh Saini

    return self.reminders.count app crashes at this point ! HELPP ! EXC_1386

  • Malek_T

    Hi, can you provide me with more code from your project so I can see what’s going wrong?

  • Markel

    Thanks for tutorial, how can i filter reminder between two days. Am using the code
    let pastPredicate = eventStore.predicateForIncompleteRemindersWithDueDateStarting(today, ending:tomorrow, calendars: []) am getting null. How can i filter it ?