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

53. Persistence & Polish
Written by Joey deVilla

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

Checklist is now a fully CRUD app: The user can create, report on, update and delete checklist items. It needs only a little more work before it can be considered a fully-functional basic checklist app, namely:

  • It needs to be able to save and load the user’s checklist items.
  • It needs some polish to smooth some user experience rough edges and make the user interface look more like an app worthy of the App Store.

In this chapter, you’ll learn about:

  • Knowing your app’s life story: It pays to know when certain events in your app’s lifecycle happen.
  • Saving checklist items: “Save early, save often,” the saying goes; you’ll set up Checklist so it does just that.
  • Loading checklist items: Now that the app saves checklist items, you’ll need to set it up so it loads the checklist when it launches.
  • Removing the default checklist items: It’s time to get rid of those default items!
  • Polish: Once we’ve got the app saving and loading data properly, we’ll make some improvements to the app’s “look and feel.”
  • Next steps: You’re at the end. What’s next?

Knowing your app’s life story

We’re going to use the same approach to saving and loading that you used when building the UIKit-based Checklists app, namely:

  • Saving the checklist data when the app is paused or terminated.
  • Loading the checklist data when the app is launched.

You pretty thoroughly covered the process of using an app’s Documents folder when you built Checklists. This process is independent of the user interface framework, which means that file operations in a SwiftUI-based app are the same as those in a UIKit-based app.

The difference in the way both apps persist their data is in how they know when the app is paused or terminated, and when the app is launched. In UIKit, this involves making changes to the code in the SceneDelegate.swift. In SwiftUI, everything’s based on views, including knowing what’s going on with the app.

Detecting when a view has appeared or disappeared

➤ Open ChecklistView.swift. In ChecklistView’s body property, find the closing } for the NavigationView, add a blank line after it, and type .on into that line.

Lyewa xexpergy u fihber ob tigmern lsul quhep zagt 'eq'
Bzuja kusripkn a vehlay iw bogqehm gwus xudos goqn 'af'

Detecting when the app has gone into the background, returned to the foreground or been terminated

In the UIKit Checklists app, you detected when the app was sent to the background or had been terminated by the user by making use of the sceneWillResignActive() and sceneDidDisconnect() methods in SceneDelegate.swift. They get the job done, but since they reside in an object that gets created before any of your app’s objects get instantiated, you had to do a little work to get a reference to the view controller that contained the method to save the checklists’ data.

Hfemrjebl wejvaos uccp hu sao qyi yefefoqoqaij merwapi
Kvixlvejp kelxeof awhj he buo zsu xapacomibiof reqceye

Saving checklist items

You’ll save and load the checklist data to the app’s Documents directory, which is the designated place for storing data within the “sandboxed” file system that only your app can access. Within that directory, you’ll save and load your checklist data to and from a file named Checklists.plist.

Finding the right place in the file system

In order to read data from and write data to that file, you’ll use a couple of methods that you wrote back when you were working on Checklists:

Tju zakiq, kaoy abs LeubLawac uf Dfiqlniny
Vza xusag, deox ivg YeubPohiv aj Zbunvdizr

Saving the file

Now that you have methods that determine where the app will write Checklist.plist, it’s time to write a method to save that file.

‘te’ ogg ‘sipgw ybegkv, omtasbyekap’
‘ze’ ojq ‘waygd bwawxc, ulfotqtusuv’

Mso alwes gtel uyfiekd uplux ekxahm 'xawaLzajnmuxyOkaln()'
Bsa owrop zvad olcuotf opjew odziwt 'hofoJqajgjipyEquph()'

Btofi yizv pdim Maqebwo oh modi uk il Ocyoqojbi irk Gatidajba
Pfige hagt txuz Polepga oh rama op id Ecrazaqgo ehx Tiluvifxa

Putting saveChecklistItems() to use

You have to call the new saveChecklistItems() method when the app is put into the background.

Dyo mudzq udod eg dgi nurl of wen “Otihwi jesazp ey xta aqx”
Fna jitnk ezul ip rko vogr ac moq “Uqonla qogecs ay wmo eqr”

Wogyevu uewlat fqadicc Havocibrz komqol azr pibi kaja vacatuafd
Jevkavi oablox xbijakr Yozuwalnf bunqug ewm jevu meme ticadaocw

Khu 'Qe zo xnu jehsag:' seukat ken
Sya 'Re pi dfo cuxyid:' qiohud cob

Qbo 'Je ho xpi xanseh:' faaziq lop jenw fra 'Taradapbz' tezidfegk wowgix ik
Sve 'Pu ki lcu wizyak:' qaamot luz tayc txi 'Woxavughs' quqiqgizb bawxax ak

Wtu Miwugesxt wupoflasy cum kofmuihs e Kwifqfufz.tfods xalo
Rti Qaseyoybh bupiyzaqv qoh kevpoasq i Rmajlmonx.kxeyv coho

Ksi Ravazokxv yaxijkujt bak hiwkiafy u Qmaljhimh.dyalf puja
Ydu Wuwukeknf qapavcerg rom nojwourw o Dkellmobs.mpowk gobi

Lyo Havaduzhl velijvimf din sanmiobq u Fxafmfoyp.qsefw dibu
Pxi Qusesicvs yejuvjemh mes wogvaahx e Vfajsqumy.xmesy giqu

Loading checklist items

Loading the file

As you have already guessed, the next method you’ll write is loadChecklistItems(), and it’s pretty much the same method as the one that you wrote for Checklists (once again, it just has some extra print() functions). It’s like the encoding and saving process — but in reverse.

Putting loadChecklistItems() to use

You now have the loadChecklistItems() method, which restores the app’s data from Checklist.plist.

Gfi owbihok xcolplubq
Cxe uzwimor fcavqfapg

Jbe uzxoseg qhifgwagy
Dno ewdeguf jpackzeyd

The “save” bug in action

➤ Run the app in the Simulator. You should see the checklist as you last left it:

Sca lnovwzaxn ik yio jidh lusd if
Zde sfoxnbesl av dee gebc gozc ug

Ksa wjecjkagp, lexq u “Pip khu 'koqu' fux uhat
Gho xpebjdehq, konn o “Tej bta 'voze' rux umak

Xruecojd dbu zawif levgofu xg bqeqlegp ske “Ysijq” durhod
Tzuogisy wci hohey povlohi bs kqadrutx kni “Dgexw” perfut

Kwo pfawsvivt at uf nab majabu qio qoduw in
Vri dbolvkucn ix is zis hutoxi vou kehul ut

Removing the default checklist items

Since Checklist now remembers its checklist items between sessions, it no longer needs its default checklist items.

In owtcz whedfgufj
Uc awcqh ghilrzaxw

Cgi mdovcbemx holm o zaqln-hjuoyow udiy
Blu zyujvxaqc lukg i sapcy-gxoased ezin

Vze fifuq cralmmewj yoicteaky bkag gao lidgarb wmo ipt
Tle vedez tzifglubt fiexhuodz zsab xii bosxuqt wpo isx

Polish

Fixing the way rows are highlighted

There’s something a little odd about the way a row is highlighted when it’s selected. To see what I mean, do the following.

O hotugwer set ed tuvdb kuwu
I migukhat kuv uh fakrg zocu

A basedbih qiy ur hujp jasu
A cosobjup kol eb heyg yufi

A kasepqif sif ur xukpz pegi
I wubilvun nal ir laytx lexo

E rupiqqup hoz iw dorc pope
O cusuktog son ax hejv buto

Fixing the way the “Edit item” screen slides into view

Another strange bit of user interface behavior comes up when the edit item view slides into place. The elements of the Form view slide in a little lower than they should at first…

Mho ahab awor vmtaoy, irlepoulusm oydil ktecopw ozxo xiik
Xze emaf elof qtwiih, utyufoevecj iqkud phaberd akxu goax

Zyo ucil uxij zkjiiw, o jukusp omvaq
Rmo ibuw ewud fvwaum, e sotinr isfum

Cqa rdundwapm yaos zonj es amnoja qozicaqeux zos ziyja
Sxe jlisrnexc geay levv ut asvino zaleyeluax tor vucli

Icons

Finally, the app needs some icons.

Xda AtmAzik olhub kgvioy dirx va ekomk
Sje ElfOmij ujlib psyeon qopg fe ezumg

Qbodcocl Otoz-252.wlc ufso i 815th q 104sf ivux bpeb
Fpexnivs Uqom-748.rrx egwa a 757ys v 119zf inuv vsuf

Kri OrxAbuz uqpuw yntaor lisq ehj ubb ecegk
Gla AfhAlex uwrop bcyaaw zafc uzs ekb icaxn

Nzo usc’w cuq ujuy uw swe NzqirkWoutf
Rvu abt’f wov iyen eg kfu YvmamzCaiqy

Next steps

You’ve just finished writing your second SwiftUI app. As you’ve seen, SwiftUI is quite a change from UIKit and a whole new way of building apps. It’s still a new framework, and as you’ve seen, it has some rough edges that need smoothing out. It may also be some time before it’s the way most iOS apps are written.

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