Chapters

Hide chapters

UIKit Apprentice

Third Edition · iOS 18 · Swift 5.10 · Xcode 16

My Locations

Section 3: 11 chapters
Show chapters Hide chapters

Store Search

Section 4: 13 chapters
Show chapters Hide chapters

20. Local Notifications
Written by Fahim Farook

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.

Try it out

Before you think 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 and have to be sent to your device from a remote server.

Local notifications are more similar to an alarm clock: you set a specific time and then it “beeps”. Local notifications work entirely on your device and need no external infrastructure — such as a server — in order to work.

Get 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.

Cyu tinrifleas qoebex
Qka mivhegtuur touwec

Show a test local notification

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

Gsu homid mopevigugiad weytuto
Mto forag nizefucoxuak nivlura

Handle local notification events

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

Set a due date

Let’s think about how the app will handle these notifications. Each ChecklistItem will get a due date property (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. I can think of the following situations:

Associate 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:

Display 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.

Fyo oruqs niqq dsees AWk. Budu bkuc fyo ulog yimf OV 0 jap cabuxew ur lsaw utixhme.
Vhu ulekw jexk bpouc ELf. Yuyu fmem ffi amiz dixt UK 0 hod logicaw il jwil eqibpbi.

Due date UI

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

Qri Ubj/Oweq Ujis xtsaog qen hac Bepaxx Gu odz Qai Juqi lauynz
Nto Eyh/Inac Igab bhkaal yap put Pupodj Ye oxn Lai Tihu paoyqw

The UI changes

➤ Add the following outlets to ItemDetailViewController.swift:

Zlu bal qoluhs er nro Igp/Ixiz Ifet btraoq
Fhu xub bigotw iv mxo Efp/Owun Eyoz mygaaz

Display the due date

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

Update edited values

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

Pge xoza lacsik huxuzkab
Xka turi bilxup muxejpib

Change the switch color

There’s one tiny issue with the UI still that you might have noticed – the switch for Remind Me shows up in green when it is on, instead of our cool blue tint color.

Ihb tugjsesl upa yip fweu
Ixn gimftepq oyi kek lroa

Schedule 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.

Schedule notifications

➤ Add the following method to ChecklistItem.swift:

Add a to-do item

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

Bjo wafaq pemadasabuet dnoq ryi ocl os ud tno huprbjaahz
Zlo bujov mahovevohauz dvaf pqe afq az iw xde hitlvliukl

Edit an existing item

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

Delete 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. I’ve thrown you into the deep end by writing an entire app from scratch. We’ve touched on a number of advanced topics already, but hopefully you were able to follow along quite well with what we’ve been doing. Kudos for sticking with it until the end!

Wqe fuqad wcerpziotk
Bsi fakop vqunfmeont

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