Chapters

Hide chapters

iOS Apprentice

Eighth Edition · iOS 13 · Swift 5.2 · Xcode 11

Before You Begin

Section 0: 3 chapters
Show chapters Hide chapters

Checklists

Section 2: 12 chapters
Show chapters Hide chapters

My Locations

Section 3: 11 chapters
Show chapters Hide chapters

Store Search

Section 4: 12 chapters
Show chapters Hide chapters

20. Local Notifications
Written by Eli Ganim

Heads up... You’re accessing parts of this content for free, with some sections shown as scrambled text.

Heads up... You’re accessing parts of this content for free, with some sections shown as scrambled text.

Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.

Unlock now

I hope you’re still with me! We have discussed view controllers, navigation controllers, storyboards, segues, table views and cells, and the data model in great detail. These are all essential topics to master if you want to build iOS apps because almost every app uses these building blocks.

In this chapter you’re going to expand the app to add a new feature: local notifications, using the iOS User Notifications framework. A local notification allows the app to schedule a reminder to the user that will be displayed even when the app is not running.

You will add a “due date” field to the ChecklistItem object and then remind the user about this deadline with a local notification.

If this sounds like fun, then keep reading!

The steps for this chapter are as follows:

  • Try it out: Try out a local notification just to see how it works.
  • Set a due date: Allow the user to pick a due date for to-do items.
  • Due date UI: Create a date picker control.
  • Schedule local notifications: Schedule local notifications for the to-do items, and update them when the user changes the due date.

Trying it out

Before you wonder about how to integrate local notifications with Checklists, let’s just schedule a local notification and see what happens.

By the way, local notifications are different from push notifications (also known as remote notifications). Push notifications allow your app to receive messages about external events, such as your favorite team winning the World Series.

Local notifications are more similar to an alarm clock: you set a specific time and then it “beeps.”

Getting permission to display local notifications

An app is only allowed to show local notifications after it has asked the user for permission. If the user denies permission, then any local notifications for your app simply won’t appear. You only need to ask for permission once, so let’s do that first.

Things that start with a dot

Throughout the app you’ve seen things like .none, .checkmark, and .subtitle — and now .alert and .sound. These are enumeration symbols.

Wmo rozzepciav ziekiy
Sda bupgewquot duivek

Showing a test local notification

➤ Stop the app and add the following code to the end of didFinishLaunchingWithOptions (but before the return):

Vzi keluc xijacawuxoiq cetboja
Xni taxiv nolidofabief bekvale

Handling local notification events

➤ Add the notification delegate to AppDelegate’s class declaration:

Setting a due date

Let’s think about how the app will handle these notifications. Each ChecklistItem will get a due date field (a Date object, which stores a date and time) and a Bool that says whether the user wants to be reminded of this item or not.

When do you schedule a notification?

First, let’s figure out how and when to schedule the notifications. Here are some situations:

Associating to-do items with notifications

We need some way to associate ChecklistItem objects with their local notifications. This requires some changes to our data model.

Class methods vs. instance methods

If you are wondering why you wrote,

Displaying the new IDs

For a quick test to see if assigning these IDs works, you can add them to the text that’s shown in the ChecklistItem cell label — this is just a temporary thing for testing purposes, as users couldn’t care less about the internal identifier of these objects.

Spe ijorn wiqy mriok ASx. Navu fcac nco adoh qest AT 4 sam soqifun ac hruv ahehnpo.
Xni ebokx jexz swoiq ITl. Duvo tniv rci eted wevj EZ 9 xep dutokew ah fpun imuwrne.

Due date UI

You will add settings for the two new fields to the Add/Edit Item screen and make it look like this:

Nza Opg/Umob Ulim tlpoev koh cox Vekuxd De end Nio Voko moigpn
Hru Ecf/Udin Ewuj hjgeal how com Kipusp Ha ekw Cii Xiya qoazth

The UI changes

➤ Add the following outlets to ItemDetailViewController.swift:

Mco pof warusb oh kho Omm/Uzas Iluc rrcoub
Qnu fup siforx ez lku Iqp/Anim Ovun vsmoup

Displaying the due date

Let’s write the code for dispalying the due date.

Updating edited values

➤ The last thing to change in this file is the done() action. Replace the current code with:

The date picker

You will not create a new view controller for the date picker. Instead, tapping the Due Date row will insert a new UIDatePicker component directly into the table view, just like what happens in the built-in Calendar app.

Wci vije vuzwex uf xje Upg Icey yvzeow
Lbi tuni kozrus uf rsu Und Axoc fdmiit

Gwagmibb i xumxu xaem tagl acko shi ybiwu kopm
Fnanqewm a jiblu faap pilw onva mro psilo duvp

Czu caq megko dauc razf vobq ib apq ins omia
Mju mag cewfo fiup dosf veyr ul ugg oyj ohei

Fga gerotdik cupi sigluf pabf
Mfo wunetkiy kuwi cotgux kodd

Subvtet-gnah juhfaov zsi iyibr iz zmi qmowe farp
Tovjzef-clov vondouq wga opopl ex gda pgewi xijr

Displaying the date picker

Great! Now that you have outlets for the cell and the date picker inside it, you can write the code to add them to the table view.

Making the Due Date row tappable

At this point you have most of the pieces in place, but the Due Date row isn’t actually tappable yet. That’s because ItemDetailViewController.swift already has a willSelectRowAt method that always returns nil, causing taps on all rows to be ignored.

Cju wuqu voznoj uchaigh um a zar hurl
Kgo lomu yakhom onsoams ew u boj tayt

Listening for date picker events

Interacting with the date picker should change the date in the Due Date row, but currently this has no effect whatsover on the Due Date row (try it out: spin the wheels).

Changing the date label color when the date picker is active

Speaking of the label, it would be nice if this becomes highlighted when the date picker is active. You can use the tint color for this (that’s also what the Calendar app does).

Wqe yogu jesiz ejleidj ud vni hics woyiw vlewa pyu cota jindet az biqesli
Sge roni jewuv ulbeikq ig tfi kaqy nuyab kziro glu hehi sewmeh ij kakonqe

Hiding the date picker

When the user taps the Due Date row again, the date picker should disappear. If you try that right now the app will crash — what did you expect? This won’t win it many favorable reviews.

Scheduling local notifications

One of the principles of object-oriented programming is that objects should do as much as possible themselves. Therefore, it makes sense that the ChecklistItem object should schedule its own notifications.

Scheduling notifications

➤ Add the following method to ChecklistItem.swift:

Adding a to-do item

➤ In ChecklistItem.swift, change scheduleNotification() to:

Cpa fuyij cepeqexiyoex cfur zki ald op ek slo dazgxxuumt
Yqo tepik vurodubuwiux bfeb yco iql ut uc pdu korzjxioqj

Editing an existing item

When the user edits an item, the following situations can occur with the Remind Me switch:

Deleting a to-do item

There is one last case to handle: deletion of a ChecklistItem. This can happen in two ways:

That’s a wrap!

Things should be starting to make sense by now.

Gde tonip dsegcvaijn
Pmi segat rhufbriokb

Have a technical question? Want to report a bug? You can ask questions and report bugs to the book authors in our official book forum here.
© 2026 Kodeco Inc.

You’re accessing parts of this content for free, with some sections shown as scrambled text. Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.

Unlock now