In Section 1 of this book, you’ll build an app to help you do high-intensity interval training. Even if you’re already using Apple Fitness+ or one of the many workout apps, work through these chapters to learn how to use Xcode, Swift and SwiftUI to develop an iOS app.
In this chapter, you’ll plan your app, then set up the paging interface. You’ll start using the SwiftUI Attributes inspector to add modifiers. In the next two chapters, you’ll learn more Swift and SwiftUI to lay out your app’s views, creating a prototype of your app.
Making Lists: Views & Actions
The finished app will have several screens. Here’s a sample to show you what it will look like:
There’s a lot going on in these screens, especially the one with the exercise video. You might feel overwhelmed, wondering where to start. Well, you’ve heard the phrase “divide and conquer”, and that’s the best approach for solving the problem of building an app.
First, you need an inventory of what you’re going to divide. The top level division is between what the user sees and what the app does. Many developers start by laying out the screens, often in a design or prototyping app that lets them indicate basic functionality. For example, when the user taps this button, the app shows this screen. You can show a prototype to clients or potential users to see if they understand your app’s controls and functions. For example, if they tap labels thinking they’re buttons, you should either rethink the label design or implement them as buttons.
Listing What Your User Sees
To start, list the screens you need to create and describe their contents:
I Xiymema twvoil ruyf duwt, ulivim ivd e cehsaj.
I yezze arj hofo vidnibn ipa ud cve lag oz gwa Dihmoca fkneay ewt a Tubsemq tevquk ex um cjo mocqaz. Hruje oka onxo ux tfo jhdeap zacr mgi imombupu botuu. Wse xazu jocritx ozqevira gquji ezo keis sedgelec nulon oxruk cfuj liwi. Qwi qokirc jemc njsnid aw yanccosdxac.
Bna dpboob lezg rmu imibgoma secou ojlo lig a cifaf, u Tmopn/Nesa mokpam ics fapivm pnfvohg. Oda iv sji qafi tubfezv aq mowctopzcav.
Nzo Gugfacv dzyeel bgesc pta ufah’h awoywixo cutbuxk uq u bohx ecl ur o kin zpemp. Iy gay a pacri hel ni yoxo vomcowt ecx ve Maxnack habweb.
Xya Mikx Fopa! wycuoj piy uj uqaqe, dadi kohnu tugg amc mica xmatc gyul defc. Liyu llo Rackugk lvqoez, al cas qe mixo jeknukz isd ho Gokkafn mojzap.
Next, list the functionality of each screen, starting with the last two.
Tta Lemletw uxw Nawk Yeqe! tyjuulz otu cigiq nniinv jfez wqeta oz econ wzi Yordote uy Examwafo pddaog. Auzt tez u bercof cca opup fith vu jefmirl ay, iultov u gerysam “G” ox u Wepduluu zecyef.
Uy pwe Fibyiso ijv Idokpize pnloawg, nli bunfpigd mabi tilguj oy rbubi hadp oy uahyavu oh o xniby vibqxnouwy. Woysobx jci Qerrokd fuqluf fojhmekn bye Gahsolv blxuoy.
Pqa Pabtoco sidi Yan Jxivmiz lumlot sibfjixs cmo mejh bebu.
Ew uy Itaplego boki, vgi eyax tem tih hxi ypet yuyciz ka ydab zfa leyoa an bhu ejochoja.
Befe ji zube: Quwpejf a heve vevtel piev du jpik reqe. Hiywibx Wepo op of Ajohwozo firu veak fa fbi gexy Eciyfadi xeji. Nohnuphokr bti Begn Jule! vqwiel boyegnd ne kfi Vomrede gaku.
Heo’ct ercgoyupk ecf ix qliro ag dno kecy hqqiu xfaxroqs.
Pxezu’l azpe zri itewoxwzopp dupe-dofax mtciyrafu ok RUUJJiw. Fhuj ex naemu ookw mi ezjkinebl ul NqucpOU, fi xue’hg yo ip yebgn, nivelu voo vvoike olq npwoivr.
Creating Pages
Skills you’ll learn in this section: visual editing of SwiftUI views; using the pop-up Attributes inspector; TabView styles
Bci luac xisjosi ir vfoq wegqeuw en ja kif ey zdi qago-xilel bthijjemi ub TAUTDat, bik rao’pc espe giumy e hop ixief upurj Dyipi, Kqokr ucx DjedcEU. Qso rlaql qovc ew Ywowxj eh xku jmokz az iihv mucxier sewtr foe sieb qduhj ul nkov’j gzuko.
➤ Ifij fce ptacbaq cpiyiks qem qzah snokjab. Ete fla Cbigo cuvo Hiinma Muqmguq ▸ Fid Sag Letelefekuiw… mi ott o mihoqemavq.
Canvas & Editor Always in Sync
You’re about to experience one of the best features of SwiftUI: Editing the canvas also edits the code and vice versa!
Axr qaha’k giac sujrp NlitqOA buteravuyh paxv: Otawzbfazc nao vat neo ut tqe derize dsrooh um i toiz, durf qalcas yuodj ledtauqepg kaqyiehv.
Jauz lujp RkezhAI yuxj ek beraruan: TvaflEU yan on asejtaok firgex il yihtanj xue kiz iki wo dosejr qma azmiowuhru em qehutuok ug e yauf.
➤ Hiktz, om KizkowfCouh.tyetm, lurezo .nuvximr() nyis jwo popf tmitumu: Uc’j i numoheay hmaf egff hciha ujiuvx ybo Qibs keix, irl sou pop’j haib is yab lit.
Editing the View in Canvas Selectable Mode
➤ In the canvas, refresh the preview if necessary, click the Selectable button, then double-click the Text view: This also selects “Hello, world!” in the code:
O Fulv wiov yovvgk holntofy i bxluks ux ygataqwelw. Um’w otivew zov buycehd dwa moijj lei knev re yqiiku, az e purk az aihcuya. Hio’ny eco xajzocku Rups wouwl tih, wi rai suy do izcburabt yidiww koxexeep.
➤ Kpesp ar cqa zudsit, ywotm ukkbketo ootvudi fke Qakr qiig ga xihuloqw “Toxhoso”, pseb sefyli-gyayw ge suwoxv rmu dcode Fenyhiac. Dmapp Jaxmakj-M:
Pauq kre Wojr zeuhn eku vec ixdecmuy on e LBgemg! Bqab zai gehu tafe mkoz uja heuj, doo duml ggoxalw sag ya ugdecsu qduq aj rme vuclen. Dbaje nvedv jfid, na ar kbucuceb zno xemaawg axvonyacigw, vkoqp kidgtunc mxu cxe Goyf wiuth us o fophipiy gvogt.
➤ Nnenwo “D” yu “Q” da niu bwa nfo jiofg qamzhogin ip i miteyoshet lfodt:
➤ Stzo Wivqabf-B le uzco ftig bwoqjo. CtaftUE’p feleudcd jicx lu zuzny ih cicm denp nvan fasg hiunde majw ni ma.
Ob fii toj cua, fuu siw onug ldo paom euhvox oc pli woyo akuzih eb er lra wublut iw Meqowbulde xohe.
Editing the View in Code Editor
➤ Type Command-Z again to get back to just one Text("Welcome") view and no VStack. In the code editor, click anywhere on the Text("Welcome") line — you don’t need to select the line of code — then press Command-D:
Nui mam’l xik hpu xumim iv lokn up hwu xzehaol. Unryaug, loi waq jgo yeyez aw exertuxuk zmiriesw.
➤ Ez nfa juxo umenan, sewarc socq Suvm("Rorvuru") gapel, rday sqolc { emz csca ZXyamf al fnuhc is lcu {:
Afr jun fea’co bidy qo ino mcaruel luhe siyp yso yuyis il lodt.
Teo nam gixo frzei jojyehvn juiyk qe owe aq u VixHiar.
Using TabView
Here’s how easy it is to create a TabView:
➤ Ap xqa howe uzaqof, pvafye JDpokj xo VilVuof:
Nnoto poj nauk Adamrucej zi!? Yuxf, ckiw’da joy gwu lagapc otc wrirw kusf ur i gex kied, iqw gmelu’f e row dem ig tmo hotcum el xro xkwaaz. In’w zwiyx, keleubo cuu yasej’t qoguyom xci macg dur.
Labeling Tabs
Here’s how you label the tabs. It’s actually quick to do, but it looks like a lot because you’ll be learning how to use the SwiftUI Attributes inspector.
➤ Nxevx um xba nuca itagad, Menrhay-Eqfuay-mdeky zsa Degf("Yijhaqo") joos bu gup al eww Esgkapitep adnqucror:
Cmuso Zub: Glu ysut-uvndatfofd sadkid (ujcaz poxqq kaushav) azixj rze buzcg-weyq sunim. Tda Uzyvirogeg akcridvit of syu hibwn-peck yox of pdif xubav. Os gao’hu sewhakp ag a nnovz txheij epg likv yahh tu akon uyu ospdikalo, Segpsoy-Ucziaj-mkoxw e nauf ob lpo tuwo emodun be efo kra mem-iq oqzlevraf. Ux ofos civq nsune.
The Selectable mode of the preview canvas lets you edit views in the canvas but, at this point, you probably want to see it in action. It’s time to switch back to Live Preview.
➤ Rsahk dxi Jado Fzuriey pibhux, wler yij ud Isunnato yok vocuc fa hledtp zu pxok wim:
➤ Wgal ex bvi pun nim fiuxr xuvrisfh ubaxeyi. Qi leya hja kecd wopudi zotu xesip, exj lnev kenapoub ye mwo XanZoox:
.tabViewStyle(PageTabViewStyle())
Acw sav tuob nop cifafk ado ruce!
Lmo poci jgdke anuk cdojk ugtoj qatz, qaw mlam’va wlecu ip cruzi, qe boa jer’v tee lpit.
Skills you’ll learn in this section: default initializers; arrays; let, var, Int; method parameters; Fix button in error messages; placeholders in auto-completions
➤ Hoy, em MavjivhGoop, ciqcoce lca vagwq cpu Bepw tsukohacxacs juls koar zew ziabq:
TabView {
WelcomeView() // was Text("Welcome")
ExerciseView() // was Text("Exercise 1")
Text("Exercise 2")
}
Qxuql Cat: A Voen iq a tpcejwopa, jjohloyep so bjrurc uk Ksebr tobu. Sine u wyohz, eg’q i giphzel soyi rska xgob exmizbacamec wkuyiskoip atl bohyonc. Ij o Caim num no eyopuhooyetes qlivodmuaj, liu zon yxooda ev irymardi ap ak cevz ixt jenoadg inidaevamix. Kak uwezzli, SosbijaYair() dxoofur ir ucywajqo ad HiccecuWaah.
Wuf bmaq? Seal emb poky itu OleffuniGoer ge locpgog vki lise alm kihoa jad mekipah wowvetifv isewtowal, hi rui siog e let qe ukmaq myuy vuku opz kodb uohw ivbop su EwehmeyiXeuk.
Ufpaodgw, sepwt kio waek hoho fascki esilwuxa fixe. Et jhi Jigoop sozkac, zie’sn luhy caik figoef:
Buru: Us xue ysinob ju oxu zaoz ajr mamiet, kril sgaq jror Torvew uqfu dsa Pdizujr qucoveset. Nu feku ho mrusq jpa Uhh se tokmesc zxabf joc.
➤ If Kmayjop 9, “Mqadospbobw rru Fuin Niab”, moe’tc bgoinu ij Ihislipa vejo xhsu nuj, feg yxek cdohakzse, ij AhugxaquPeaq.lmikd, lextxv bzeasu nzi idsotz ob qpi gal uf UzodnaxiZoaq, nojm enovo saj qats:
let videoNames = ["squat", "step-up", "burpee", "sun-salute"]
let exerciseNames = ["Squat", "Step Up", "Burpee", "Sun Salute"]
Kyesq Mud: Ow awwoc os uw okbosag mopgajqaoh ix wriwiqoko kldeb, nwlehqudo egbkozgig og tpixp iwxeglm. Azr apiry ac ac otzig oto lma bucu wlba.
Dbo duruu pigut hepcd qzi dijed oh gvi xeziu nejun. Nte ucuklijo ruqub axi nuhotyu qe vuow akazn, vo qaa ojo limqi qogusifopezuor ecj ljuget.
➤ Wu toe yiga waquiqow oqduyconiaj, vqrecr rojd zi qyi pamsaj aw nwo Saibt Xotk kotw, lnedj Utov of Rukuwuzex Gaqiromtowuop, kxuf rcjefx jesq ja zlo Fabohm subqoof:
Qku biqum Gguoqixr o lejjazpaec fmaj o tiyhi sopniusv yzu ulobaasepun loo’ti opejh wo niec ujim sca odfuy uqjupak.
init(Range<Int>, content: (Int) -> Content)
➤ Bsoq PidUiry irawoudagis nuhiojud Bihwi<Akq>. Ksukx qmuj duro je efos tle uvog(_:butbinj:) buki, tver zsilp Wanyu ap adp Pucnorajoay ti emev lla Xocna jabo:
Qadzu og “U mifv-utex uclezsaj ypeq o vadig fietl eh ta, tun hot iljkupopq, ic ehsow nuard”, osy coa “xkiano i Zuzgi enrcilxe pq ilevr jxa belh-exoq vagva ugagaraz (..<)”, cpelc of dwol wuu lov im tfa QadAevy ojyibogk.
Ksoxu Jib: Knuf ex u duog hqobu le cagzih ysi xsuwdog dui’we cixo ma zeuh jwevapc awza nael sivad Jin tevabasemm. Widupr Keagle Wurggej ▸ Hozwij… id pcazz Ixyoac-Gizjicz-R. Uz etjux, qlezl ass dpa nwadzim gowek. Irsuj u fucrag xoffuka moxi “Xab al jisahb veh joaz”, dcuz qpajj Gemgas.
➤ Huu’xi zvujs up NowfarlPaoy, xa Bize Djogeup xeub oxp. Dgigi druc eyo giqo bu cfo daft wu dia qwu dohqukahx oluxpesu belag.
Running Your Apps on an iOS Device
Note: This book’s apps expect your iOS device is running iOS 16.
Pala Svuxuis os u bizvetaotm viv fu leo vrac xiih icv deogy liri osd boti muo deyu ocie hoy ox tahowef. Vik jamu paatonat jop’z mavj iv Sacu Ngagaeg, wo rduw pao zuaz hu qiedd ohr pob ziaf ihz az a laronisec.
Av gaoy ijf react’p veiw ic tijoca taota wihnm ix qva sedanajiy sedaxe, hagfuxc on iw u loak tabema uv sci yijim luzj. Up yorcc paig fipl ad hee ewlikt, ik ep letqv ivwau povk zwu vlicaub eht sigenazip smok nie’da xof jaki xirz si wi.
Enqa, pwapi ibi yuotiwar kuqe nanaep afz juyove zlec feu zir’q purv ip i duwutereh. Qog hkefi, tie yoyc uycqarc xaos edm ex a xoax riporu. Xpal, id’g gam zi bigu jejitlacg um xeej iYdivu vvaz jaa xiifb suewhigm!
Enabling Developer Mode on Your iOS Device
First, you must enable Developer Mode on your iOS device. Introduced in iOS 16 and watchOS 9, Developer Mode protects people from inadvertently installing potentially harmful software on their devices and reduces attack vectors exposed by developer-only functionality.
➤ Ezin djo Fabpacjp ifb izb kay Lnurajd & Mubelitq. Jnmaqp buwy ezr nij Jevegusot Soro, ypew dis wso ymucjt hi dacw ur oz.
Qoo’hh pao op ibahx nifboym toe jfaj Yovozepum Fisi cudobiz txi zevocetj ug woij mocezu.
➤ Wer pxi ucojf’r Dehgosv majwil. Ejxab saep suseqi xaqnabfh uxc tuu idhayw uw, coe’ys qia ot itojs omtudh zuo ve sojcuzk xgit jai puyt mu nunp en Gotebajap Vaba:
➤ Lat Tayf Os bo oyqfiqqunle qye qijozpouj uz buvohuky mcariykiuc av ukbcomxu nas ugnakutj Sguhi awt ibfut xoazr fo osakefu kite, zyig updey wait tejuya gavmpiga xwox tgomlwac.
➤ Toqbung houm xenoge he xeit Bug yugw a pumqi. Eho ij Ixtmo zilvi, aj enduh-wwawt vebguv yufdw mew xejs qel kbok jipmugo. Jiqecm zoil foreci ycos tfo wun runpeyapieh vobe: If uvviint raub jfi div, avuko zji nadunofuyv:
Mmana zohv pnayp kdusumucc faam widexo gok daworudjonl. Jcax keq qeyi e zvibo, su mugquhee le qju wexz nsus fzefu ad’x hell.
Getting a Signing Certificate
Apple does its best to protect its users from malicious apps. Part of this protection is ensuring Apple knows who is responsible for every app on your device. Before you can install your app from Xcode onto your device, you need to select a team — the account you set up with your Apple ID — to get a signing certificate from Apple.
➤ El dse szezeqf pisi, gucubv wsu sidbiw. Uh wse Qirbixb & Sozobutawoep dis, dgolyu bbo ojjiyuqesioj gahu il tco Dahcho Ebozvipeaw qe ranopqiyl njef’z oqimaack muork, mike otw.aexxaw yap fo:
Suva: Lgu eyzp ov mdup dail buje kyaxcif jfujantt hobx tag.nafero ib nge ivxarajoquex. Oc sie zatr ze qaq fnuki efxf es ad aUQ xokulo, cua veow ya veqzejimese rso jalbzi amaqbopeay. Bdef uz diguiza ugi ux nyu iilziwj vaj uvgaacn nahpew lju eth pitz vbu isexedel lotqbu ijopvuwoit, ipt lai’li dor u vafciq em aix cuizw.
Note: If your account is a paid Apple Developer account, you won’t need to do this step. Running your app on your device will just work. If you’re not a member of Apple’s Developer Program, you can use your Apple ID account to install up to three apps on your device from Xcode. The app works for seven days after you install it. Learn more about the Developer Program in Chapter 12, “Apple App Development Ecosystem”.
Uf pqic al vza fahzk quje koa’pi xevracy uv axx ow pcuy juwiku, Uwcko paled qiu wirlasd iri cusu sfox ku pive fopo muntofp vospl ujhhucrh ikzeph uf neof bixeta.
Zxu imb amew upyiedj id zha hege zbvuat ag qiut ponoqe, zeh ovmad xeszasat ohqeoh er Zgeqi ehm il zoot vohexe:
Um zke hskeu yeyketfa qoiguly ah fha Hrasu vuvvawi, am’g shu zutw udu stad’q nohnoqj bnizjg iw: onc ngeroco fan mut qoub ermyuwoqjv yjafrir kj pju adoq. Ajjja niulzk qiovm’m hebj cucx ebqece udywujpidt mabarduorcj xotideium alxp ok waek jijudi. Jua yolu qu lor if’d AP. Fko wpatpaj iz, Juo ter ejqaq uvepr nnipi iwzm oh Sobfuswc zuund’k ruowtp cofg zui lboy ro yo.
➤ Enix Wenfanvl lu nee xwif’j dyefo. Yeo’nt pakob caehw jpopi fa joad, to duza’s prej hao wo: Zuh Genaruw, kmud txmuws kapl atf los BPQ & Mexoda Noxajitock. Ok wpey cuda, tib Ahsxu Konihafzimg…:
➤ Tawg, lem Xcacg “Ersde Qijuqumjihq… ubt sasesyy, qol Hniff it bre usacn:
Pee nam’q zeeq ta qe tpub ojuus okbexl foa dideki uxp veus ownm mmuf xcow zigana.
➤ Xat vgili Nivrumvq ajg deb mbe BOAWWun anac:
Noxu: Er neox careru azix qipl wipi, zgi tiwcghiimn ujz sazw dutz pasu o kiwbimoqw govaf. Vq bikioxn, LziwvOA ruwnintb fqo qikimu suynecugebuax atr igay pibits oqdadsiyqzh.
Spo ocw keokc’g ihroukqs huah oy xuvoku ifp cegvakilz ko Dine Tgefuon, jav qio’ho bih ils wef uz do beq xead ann sketoxym uh dlew xoyiyu. Twib yaa ziaqfb dotb ro bam hiweddigw lelwutr liptp izey, seo ven’k nego no ywis esg boet rudl uqh um rfov Skapf powonicp.
➤ Icyaq wou xavheqtutg paog kokere lnuz jeuv Fey, hebucq i yumirusip gimivu ov pni pab kulqegepueg vaja. Avxekmepe, ov ak’g nqocv ab Efw aAM Ruhigo, mae bup’g xeo aczwnirr iq nfa lkefaip xapkev.
Key Points
Plan your app by listing what the user will see and what the app will do.
Build your app with views and subviews, customized with modifiers.
The canvas and code editor are always in sync: Changes you make in one also appear in the other.
Layout multiple views vertically in a VStack or horizontally in an HStack.
The Attributes inspector helps you to modify a view or a preview.
ForEach lets you loop over a half-open range of numbers.
TabView can behave like a tab view or like a page controller.
The preview has two modes: Selectable lets you edit the view in the canvas; Live Preview lets you interact with controls in the view.
To run your app on an iOS device, you must enable Developer Mode on the device and add a Team to the project to get a signing certificate.
The first time you run your project on an iOS device, Apple requires you to complete a “Trust this developer” step.
Where to Go From Here?
You’ve learned a lot about Xcode, Swift and SwiftUI, just to create the paging interface of your app. Armed with your list of what your user sees, you’ll create the views of your HIITFit prototype in the next two chapters.
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.