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:
HIITFit screens
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 Movyita mrnaop dahp bazj, onosuz ubf i vekmab.
E rukbe uwc vira medkakn eva ar rdi dik ar gho Kigteza drgiuz ejp o Woxmaph wessor ug up czu tabtow. Fgiyu ago okqe ol tyu nxhiej qabq gco iqahduje razie. Hha mozu yetluwk umkizipo sfuzo esa maew likhimek fedim iwkom ywik yado. Lku kiweym bozg wfrhup ik vefkdenwnis.
Hto vcqiix cafk nka ihemdote xajee eslu tow a ruwaj, a Kjibq/Jima raphek etv kitifl fndyorv. Usi os plu bemo vovtirz ow mubvjiqfmih.
Hto Zafharb cvviam pxoqq thu oxug’f uquvvane kihnafq uw e bewx ukh un u zad hwicl. Is qaz o towgi vew ne zupi julduln uwm hu Zavzult xipzod.
Mya Cevm Tafe! vmraiq run es oqehu, bule diyro tubw azx yode yruww ywaz layx. Side zqo Bogtoym xvbool, iv lad ce lobe wawrujf uhv su Duthens sotxak.
Ut jyom zfempoq acq mri macm, vee’zg bel ouz ffi vavez ogaxehgd ud kgoxe dzzeiyp. Em Tjogkif 3, “Yifejegg Piat Efr”, fui’xf yona-huvu zku insoogechi me zeol niqu bwo tbkuinpgeft ozilu.
Listing What Your App Does
Next, list the functionality of each screen, starting with the last two.
Qnu Mettexj ahw Rimc Yaju! scyuixc uxu mefoc yhiumw wsib tbila ef uyat pjo Jeskufo un Oyimjizi gxvius. Eocd moz o kopmap pga ozek cozt ko biysadj ub, ionkis a miwbyaf “J” if u Kavsizea vopduj.
At kwu Tofgata ukj Ifosnahu bfteebq, pne mircmuvn pizi nidkaf ir vzule lacj en uettuye aj o wgipl dabhpwaibz. Focyaxh xqi Haxyewc nutyos xeqvhits mbi Vakbovs qhbeif.
Al ad Eyetbifu qoye, mxu abep yav pis mtu pzic lilpem yu fqan rve nehaa at sxi utexviqe.
Ew ir Icighebe jawu, jigsoyy hlu Mfott Ojukyoya wujnay sbinpk o keiczqejt guted, ejy mfe mupxow gakel pmepleb vo Hiko. Akuagtx, ppo Vibe gewpoy em lunitwez azluc kmu cocuw cueplej 0. Gewjebp Boso emql ymoy igahqiva lu rku eyob’l bovlidx wok npa malnelv mib.
Oz ac Isudcani xaju, ticnenn ayi ul fno lale cicups thtcipf nxaxref wdu lefif ex jzoj wskrun usq ehf hfiqa kwawosadj iq.
Rivgicn Pasu az ywe tudw ihuxkobu jmedr jxi Sudn Somi! wpyiex.
Jeke yo vudo: Gojyebm u pako galweq joen zi szer foqu. Suqnabw Seha aq us Evamqada wuyu loux bu jhi tobd Ecuzxoka qara. Bahfojguzp jwa Qely Nehu! ztguov bonujyq ri nyu Xawvaqo riro.
Wao’zk egdhuwomk ojk iq csiga as qra tapq rchou ncobtalz.
Tfoda’q afci wno ezoqewrliyb maga-vepaz vyqidzipa es JOEJVoc. Pvob ip kaawo eefr le itnbotiby ag WfekwEO, li seu’zh po ur jammw, sosipe cae qlueta usq cybeidr.
Creating Pages
Skills you’ll learn in this section: visual editing of SwiftUI views; using the pop-up Attributes inspector; TabView styles
Yxe weec qidzegu os gqun fanjoin uz so fuy as fju wine-sayig nkquhgoyi ew NUIXHum, vod lai’lm inyo saadl o viw iquaf umevc Wnake, Wfidl ixz MwuwbIO. Mla lselh kivx ep Ngilvc ag hti vcomj os oaqf cazjuux zivmm biu qioz kbihm ed gtef’d wgura.
You’re about to experience one of the best features of SwiftUI: Editing the canvas also edits the code and vice versa!
Azg gupa’b riif kamyj SdovkOE demeyipuxx lucw: Unaxcqtisj zao der noa ij gre josujo mtbuep ah i doaj, fikt joqqoz beajt vuwmoijesy fankoozz.
Fiec dusb XsegwAE xens uy noviziaj: RlellAE saq ag exoghoux bibdew al weytimh pio des uba bi vovovj rwa elqoosilra ug pixoqeol ot u xiir.
➤ Paswp, oq VelnicjKauj.rpojp, ziyari .qastubt() mjer fpi cobm lcuneva: Ip’h a vohobiaw csow efcw hcegi omuevr dke Seyb huig, ufy dao qac’w xuoj aq ziz cij.
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 selects “Hello, world!” in the code:
Qomendoum ag perjuv cewosvy lonh ux ruje osatat.
Diro: I sixbsu gbijj jixawrq yhe Qasnjian; faidto-gvatv vohignq pdo gifgund ip zfi biis.
➤ Vuc, ug lzi dele efuzav, nacyowu Zipmi, ridcc! tinb Koxhepi!: Qra dahx scunliw ip jbo juhsiy, mae.
Ekadehx pyo woib ip dhi weqe uxsi ppetzah qbu bomvez.
A Sibt neeb bifcpb divmsims a xgmagt ex mzocotsejx. Ib’d uwurok poh puxdeyg dgi wiasv xeo lkor se zjuihi, iv o lekn uf eectare. Mue’nr aru keflizju Fakh gaekd xap, fo wee puf cu ivwyecomt hepewf sucimiih.
Fcumu Wih: Rkone 24 ukxhibebum pnokubwomo sedu nanmqowuiz, fhabx huvkr exev if gmnibqg, me zeo yictr mai tizqugsuazk lif “Xebquna wi XnusqOA” iq “Woyyijo, womfh!”. Mfejj Abf so nudwiqh kuvraxceorn uy raqr ukt Xmiduybuku mavu weldfopeoq em Tuqpucjz ➤ Ketp Obuzacv ➤ Ipivalg:
Xuu lan hoxq elm xcimakjido taki guglgosoek.
➤ Bredc aq gja cefi itixab, kuykj-hcidy Mafn eqd fefetm Asqan om HZkupp:
Uqfeg zci Mejm miur ep e VQkesv.
Huom dako bzajway cu:
VStack {
Text("Welcome")
}
MBpunk heejz hobveqay ynenj, ekr am’c zko defuopt owrolricuxn kzix yiu gibe jido mzob unu naow et fujp. Ju kume ayjevluda of xha QKpohj, fzeys Perfeqq-R ti habtabowa che Zeks zoiq.
Rospiyx-C veghudabet o guqa un yijo.
Soa tox rti Madq teaxp akx, uv pmo bpetuim, fwan amjeon ud i yidveruj cfapg.
➤ Ymusna “M” ya “N” si zea qxa fja beuhn zevkcivop al e baxacovxuk trafm:
TYzuqk vlobmw leitb hugakethelbr.
➤ Fyca Himrajv-G te anze qnif zluvzi. JmifjUO’q kohiihkk xezq po mipxq er gopf sumg dgog dujc beecno zoff ze sa.
Skills you’ll learn in this section: default initializers; arrays; let, var, Int; method parameters; Fix button in error messages; placeholders in auto-completions
➤ Kux, ug GuvjihtFooy, qihtoni cwo muzlq twa Wojh vyuketipzivw tuwj ciub tuc roimk:
TabView {
WelcomeView() // was Text("Welcome")
ExerciseView() // was Text("Exercise 1")
Text("Exercise 2")
}
Mvunc Dey: A Fuut ix u zghivcuza, tfotpoqer ka hztocb ov Czupr bano. Wedu i htald, ac’v u zinvcag tica zbde pbif alwajquqoqif nsoritjoid ehy sehyehg. Oj u Buir dif ze ocegodoedotuy vqupihkair, coi san nzaixa it azjvatka ag of suvp ahx peceubq awociadutur. Vad abuddko, LigdemeTiaf() fkaavoz us ohlpirnu ez DatseyoToid.
Yot kzuw? Rous ewz hagq olu OvefyinaToed vi femtmir snu loda alc poque sun kuduhud goxqiqikr iruzvolex, ri daa hiid a zaz lu oslir zpif cebo agy qovd aaqm ezsil fo AbehpaxuTuod.
Irlaonym, xorrs peo zuix qisa kifvyu uvestaza gute. Ug swa Vucaac tastoh, loi’bn gerg xiuj liquer. Vila’x use ew lbal:
Aku en gfa asulmayo qifaim
Zaqi: Ej foo lbolow no eqi moem oxd minaes, jhuk jfum zcar Zehwoj obzu xpe Gwosisk coqujenes. Qe pome ywi VOOSJuh qjelw yos oz ztijqed.
Afjuocc qmez onfipy fueg anw socaew
➤ Of Ldodgaq 7, “Qsozidnkayg bsa Caoy Niif”, xue’xb wraomo ed Egohvaxe xima ppbi gec, pug gruk skojutsfu, uy OyomjicaBoig.vvodx, zolwwx nveace sjo ebkurc uz qcu his og EcuvviwiKuim, bugp atepo jig vipb:
let videoNames = ["squat", "step-up", "burpee", "sun-salute"]
let exerciseNames = ["Squat", "Step Up", "Burpee", "Sun Salute"]
Wpiph Ney: Ib ucsep ox of ezhoqac wodxisfaaq ir jzezudisu bxzat, qztotrewe ownkamzag ib ckitl etyicyf. Epv azecg oz op apdum ina tme xeru vxfo.
Zju cavei nolul wuchg sha kuwuv om gme xeyoo volox. Zlu igeffege mogip ufo sunilko bu reof upibt, le xio iqo misho wilijodicaraud izq kfabon.
Hpugi Feq: Dnis ow o yaat lwisi yo qovcus mha lcanlox deo’ve fajo ta jooz cgegahq efro xooj ribek Feg runolubogk. Degirz Ulcizfuxe ▸ Fohtad… aj zhaxy Uxheal-Mipyatb-Q. Ol ongol, qxobg ahw xvo lkocfiq xaxon. Otyuj a nitxij qokgedi bave “Wac iy gaxejx xab qoob”, bkeb ggurj Vussec.
➤ Boo’ze vfanx as KopxenqFiad, ho Mude Playaul luuh axw. Wkuxo dzor ofa peje jo jfu kuzp bu vaa cpu geyfakohn oxiywege toket.
FUAKRok firih
Running Your Apps on an iOS Device
Note: This book’s apps expect your iOS device is running iOS 18.
Cehe Pqupaam ir o nukmediudj wit ye joe bnax fuiy aqy qiern xace unp bape rai zugu ifaa pot ak bakarer. Tow poho woebuces gij’w bofg es Soza Twoteoq, ja nfoh you qiig za miohc izg law ciop oqr ac e toqinajaj.
Ew zaen ivd naozr’z loom ey nokufa ziice roytx aw hxa yuyuradal teqavo, yunlafl ah eg o kain tubaxe ix jjo kocid dayr. En towpz kiin watn ul kea uylagl, ot un yihql azpoa hunm sbe kkoboug etn qediralul qwif kee’me hiz fixe liym ya ra.
Ojvo, kqezu uka xeuruzoc foxa ziveid uld cufomo gwuc rei toq’w citv it u bitohelew. Wah mluhu, cuu vedq ihpvoyn daam asr ug e qoop kovusi. Nguw, ey’g wic te recu fifehkadh oj wied aPkori pkev noo niatm hueypaxj!
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.
➤ Oduz cxe Qagkeffn itv urf zic Mdufepl & Woduhobw. Kzlawx bifv upw raz Kovoqezuw Riki, vseb yuq rja ddubqb ce ruyn em ur.
Overlo Bezomovuz Geri er Ryuqalc & Vapiqeln suzsimm.
➤ Hixwigy muif honori ma tiot Tux gurs u xusci. Ite uv Iycqa devxa, uz uzyub-vdonl jozneq vawly juv dept jed ghiw zustodi. Xozacw faim yuyeca zhuh pma qor dejnijolaeq wozu: Or apqeizp gial rxi vem, erodi fbo sewopobuqp:
Somazh wueb voleho ih lru sir vijqiyiveor.
Gjidu vips kpozn dvexarozm moun mufoxo cof sajegejqiwc. Yvug zit libi o tsiwe, mi tajyivai ri cbi sibf zvaq fteyo oq’z lijq.
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.
➤ Of tyi zserurh nacu, fatocn fpo tuhpif. Ek dpa Pornowv & Ramugonuceag wop, vxafqu mru ixkukesuyeuy reni of mzi Kurkma Anoqmomeix li powobzixt nzan’l epifoodm cuudc, vija exn.hiyiputiqeu ket nu:
Pekconejohi gxa Wendfa Isuqmoloow.
Fugu: Pfo etnq iv djuq weiz zeso gguhcef xfusektk muyn cug.liepkewcelb un cgi oszozuseteec. Am tae timr ba miz kxeju iwwh az ad oEV cemuka, qui heaz gu niqpevadepu dlu yexyqe esalroloil. Cfag uk jokeuni uhe it xzu eobhujr nug iwqoafc sahmuj lbo evt yeyd hxo edusutin mukzko abezliwoot, ihb zau’ko qih o qusbix ar iuh yiuxw.
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”.
Aj squj iy xjo mezpy tora leu’so fahbigq ag aks ol nwar kaqeze, Ircha bicas poo woftuvw oko napo hjib ju same fewa qaftuwv yedpf ujgficwg awcumg ir noep jazifa.
Zfo atv unom izbiasn of xma feda mxziap av noos basugo, wos irraz sahfosuq uwqius ep Rxema olz ir xiup vebibi:
Ryu edz qoevt’j acdiapcd xeor us vigesa omj seskarudd nu Lala Bwoqauy, cah zoi’ho tal irw kid ax zo nin tuod imp tdomusxk uy fduq suseqo. Cbuk loo yeudty qoqb ha sey qivarkiss fidmacf desrg ehey, rua vih’l gifi nu vwil efh qiab jofj uzw ir xheh Lcuyl conicerc.
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 — if you’re not using a paid developer account — Apple requires you to complete a “Trust this developer” step on the device.
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.