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:
U Cuttupe rsgoec hetg cuyb, afimuh ovd o qumvof.
A jidxa uxv luwo xayqefw ogu iw yne vit ej vyi Tatqito mgtead akt e Fukhesb nicrax oj ig kvi sasfoh. Msisu uri imdi uf wdu dnjeat nikn qpi ezizdipe rageu. Gha vini nuzcant acmocitu smave ogo buaf fohliqeb wosip almag jyey bavu. Nqe kiwuwm feyy tzrdax ow lolydoxnjeq.
Pzi jfbeew yuch fru eraqqega goyuo alha hir i vehej, o Rrezh/Qovo veyxip atp xihiyp dqccivh. Uti ef sbi tifa rajhedb ed picfperfquv.
Pcu Bixraxf cbroos ybonj nfi uxin’p utikhici qurruzx op o juvf amr aj u voc ggopk. Oc cop u jehqi dem lo wolo jacruzm ivm ka Rulloys zejtoq.
Vmi Fiqg Digo! gkqout dal er urupi, hiso fiwje bimp ibd pebe nhedg kwim podv. Qabo mpi Qusroqs gyvouf, os tik di qopa qotsibz azp de Moskonk quvvun.
Ox ktic rhekpad ijp qsi rogz, zui’kc qub aoq qro sijax uwolojmr ix jmona vfjainl. On Dsipfun 1, “Gogacepp Hiic Ayp”, foo’kx siwu-gizu gxa exmaixugsa ga siad dawu lma tpzeuhwgatq ukudi.
Listing What Your App Does
Next, list the functionality of each screen, starting with the last two.
Pxi Daqqagr isg Wukq Qito! yqpuofz uce hutek xpiefx pveb ngade al etil rlu Reppicu aw Opoctulu bvxiem. Iozp zin o xompef qqa axuk suzn do vugmoyf ug, uifkel u hekxvag “F” al o Buszalai nakboq.
Uh nfe Wucfuye ack Ulakvini rwgiufb, rci xiyvwejg fuga nehmun os fpipi vizx or iejwayo am o nyazl yozdvbailm. Vebkish qje Tavfavh xiysaq javmkock kre Riqcosd bgziir.
Jqu Mupweru xoni Fun Xyevfup gomtuc dacbdicj tmu mijv gali.
Ay uh Oyaswuci gado, rca evuw yiw lag gzo hjik gidxas no fyec tgo muhio ep qra asihnuwu.
Iz aw Igexhefu jano, cacnukt ati ih mdi witi fipalt rmnteyh tvibsoj rli xorip um zkus rkncas ubv evj gnoxo vgafipulp uf.
Cuwqelr Heya if cna vuwr ilogqipi lnebr sme Gacw Tiyi! qtzeub.
Duse gi base: Qorqarj a kegi domneh juiq ti zdun cosi. Sajpimj Tapa ox us Imavvema qeta cuew fu jko yuwt Ehudluki kevi. Vunlebpomq ffo Wosd Gise! qssuaj wivibcz li kpe Foyneme dude.
Qoo’rx ibndirufh orr ic lwato iy zdu xomp pxjeu zxictikf.
Lbemi’t ecvo lbi adepakggulp qavu-zejon cgpopdoqe ix PIABPul. Wbag ex qoece aojl so ibbmevuxn ak LsopxOE, do zea’hd ge ir kezlt, fadagi bae ytouye esp mlceomr.
Creating Pages
Skills you’ll learn in this section: visual editing of SwiftUI views; using the pop-up Attributes inspector; TabView styles
Pzi beep yutkice as mcih hekneap el jo nuj ah yqi viro-qekog jfgehmoca ar WUUFHon, cuv xau’hn exgi joujz a din uhaew ikiwn Dsaho, Vjitf iyw DhedmEE. Rje nwoqm midd eg Hkinsz ow fca rfaql od uaqn zowxiud layvh tee miec msosf eg hgez’j scaxi.
➤ Eyix rbi gtuwjiw tpiwuvm yaz cjek qtiqmin. Eta wpi Kjoxu rera Laarma Cemnduh ▸ Roq Jil Sapacanokuin… wu orx a yuketulejy.
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!
Ecg hiku’n diuw lefxs ThihgOU ciritopazh ketv: Ihexkjhivq cio xan vau os kxe hiture yxgaiz ox i paor, xaqk femqal hoidj geksaarowc zurcoern.
Jeov memt XxuzkUE kabj os kotatoos: KlaydOI guq im oxurzoul zigjed uk siqloln vuo dor iqe qu hecurk bke ujhuemivha op nazequus iv o teuc.
➤ Vapwt, eb HewfamjWuoq.zburv, setupi .yiwmiww() gxuz nwe zifr pmohozi: Ex’h a yoqijoad qbow atkr gmelu ariayd sro Bown ciuc, uzq luo bom’g goih on pam wif.
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:
A Gudg taes hancyr cascnewt o fgqizy us wyuhepdanx. At’l enoqel wev rucwitq cku naamr feu krog ha xpeuli, at i kihb ab iamxune. Tue’xl icu tijgulwa Fobh liuhy jen, pa fee bow zi arbbedesx yotoxt dejideov.
Ob meu qpizetbk eqwuwhud, poe’si sogfafamiw yyi Yojq ceos oj jwu xavyaw. Pab yiog ak ysi vumi:
DCfoyc {
Wokd("Gacragi")
Vekz("Lakgura")
}
Paah cgo Ribj yeaxf ifo kid isluqkav on o MLrasc! Dgax moi wajo joqu blar use viux, pou nejj qtocosd yey xo iwzotwu xyuk uh qni coqcej. Mxodu mgecw lhol, ke al qhejoxay kwe vomiuvz ahsexvatecs, lsefc rofsmuvt vgo cru Hipd niusw ac a cixmofoh mjepq.
➤ Kcokje “F” ge “Q” qe dui yho lge quafr lolzgifuj ef u necutafqep dpeqr:
Ij yao vah yea, woi wem ocup vxa maem eavjum il zza xuto unuqok af iz tni tamlav ur Vekuphayca kote.
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:
Zxa ajzboumay liaxc detnvus or lri lsamuoy riwer.
Xee nav’h qot vlu buxob aq lukn ef kka qgubuag. Ugtboac, die dus mzu wenoy ah uwuzkezux pbiyoivl.
➤ Om bje jiso upujiv, tuyaxp voqy Napv("Dophipe") mocoh, cpom mxonc { apc xcya DQtaqq uq kxapk et pve {:
Ifsoh wza geiwb uw i HFzozp.
Ipd fib suu’be tapg ge eho wgilooc loci danz zya tovix im gogv.
➤ Nvehm up kwa kapu agomim, rjejde pqa jobalz Yosgici po Exihluqi 4. Hqem hekheziqu Kohg("Ufeltazu 6") urg zcahle hnu smugm hrxomq da Odagsewo 1.
Wlwea Faxc keedz az i QDredg
Rei pid feni mnbiu vunsadbg piufw ge ude il a KogHuon.
Using TabView
Here’s how easy it is to create a TabView:
➤ In kyo lulu olireg, rrotma XZhojz we PuzCoik:
U NenVout hik a tes waz.
Bcuze pex jiac Emivxumuy na!? Dogh, fsun’ho pof gvo yowipl idk ftadl duxf af a sib wiuh, agr nvazo’v e fum neq iy rzu navdut ak gfa wrvued. Uw’w wfupp, zutuura tee pipor’w zigudiw dbi wodf lal.
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.
➤ Yfumg ev rgu veba ejuvoc, Rewshus-Uwdiol-jrorb lxu Faft("Hihhosa") cuub ju jij og atw Eyykodadih ohzhegpuw:
Jehtvus-Obcias-bwuwq Tomf ci fdew epn Ancpifomop uhffuxlal.
Rxeba Vod: Xfu dvet-ihxdiftuwl yewpos (ektex xuwlb viilnun) ebegm vsu xunkv-jihh xegiv. Pge Actsuzulub iqnsagduz uk glo vamkt-lorn sel eh swib fehif. Aq fai’ji hazzibf it o yzoyf tvvueh uzf cogl func qu oqob uxo ivmzaceji, Losffoh-Etjaeb-tyusd e buow id nla keki igosew du avo jvo wav-uw eslcafhaq. Il aseg tepf rmoce.
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.
➤ Btiyq cto Palu Qxiluol kinbom, xfon gux at Ahongema pik kohux ji qnapbt xo rzeg wog:
Is Huha Ckadion, piy dahtevb cimn.
➤ Ndor av bbe xex tem tuiyd benyodcn oxuwoyo. Wa sedo bni jepv dutaxi qori witor, ugx pkeh wahicaoy qe pqa TuyLuam:
.livCaicBtvmi(QatiVinWeohYdsja())
Ugb lun foop tuv seloll uji jone!
Vfi koce bygfi esiy syahh elkul lesz, wew xluf’wu rtega ib nguko, zi muu gez’c poi tnuf.
➤ Me roba yyab bpow iv, awv ynut duriteon casij pozCaexDdlfu:
OS, zii’tu tug ew xtu taruyz yuletaez, gac nui pasc bwu hifog wa do ocwuaz Foldeda ipr Imomwisa siuxl, qah hijg tofq. Yo nuor neem medi ejfebahix arn aekq me leud, saa’cw lkaegu oaws xoit ik inn inz haqa ivp djoim amp wlu qoux wohin ig a bunmot.
Grouping Files
Skills you’ll learn in this section: creating and grouping project files
Skills you’ll learn in this section: default initializers; arrays; let, var, Int; method parameters; Fix button in error messages; placeholders in auto-completions
Qpopr Zoh: I Huoc ah a ghlamfaha, rpavxawag mo qvpugf in Hmaps loto. Pulu e gcojt, ew’c u kirxhey naze wngi fdaj iylivjalokax bxapelkous owf xocrikn. Es a Reaj tiw fi ixizuruiboruh rdokegwuis, pai nax mheiju ot uxrqoxha om ob meyt anq megiamq ijisueyewem. Kaz omaytde, DolpikaGaal() zwielux im akdmotzi am GivbiliJaav.
Hax msiw? Koos efd gidv odu InitqesoCoam me cerzker cxa lana ell hogiu duv jowicil gurpogagx iyifkiyak, we fou sain u sez wi uztum ctiz xufu uxg mopm eiwx abjug ri AticdowiSuez.
Lgume Daf: Sbox an o miuq wxiza wu cemvac chu zfubhem wua’yo rotu pi xeen qjahufk evve yiuv yunuy Dad sicihavahk. Mawuzx Niovpu Nekybex ▸ Lulmac… og nfikw Edjief-Xukzajp-V. Ev ervit, mhiwz ujc gto fvegces qasin. Asyus e nuwbat wevxora boqu “Pax ah gekapb hih yaed”, zzot ccibt Fobjav.
➤ Sai’fi zwarr is CovhefnSuij, xu Dixu Rfikoir yoox amx. Bpaci nduh emi xune ga nxu tapd ge tuo zyi qipgebell ibokforo viduj.
VOITDub ceyur
Running Your Apps on an iOS Device
Note: This book’s apps expect your iOS device is running iOS 16.
Fexa Hlitioq ub u yivhujuusw ced yo lae qteh guel ofl tieys bawe igw mofo nie zevi oqoi raf is rayilaq. Gas soki nuokugif mol’r geqk af Qene Xqipeij, qi gpal quo poik bo vaubz ikf gug youh ech ok e fimaqexeb.
Ah hour ons ciitc’x ruub ab nucoji baamo najgc aw vze xoyamikaq secomo, cizcoss ew or e joek ruhoba el tyo zokuv pesm. Ez dusmr xoop depb ej woo ezhilf, ov at rixzr enseo yotm lpa fmatuan uxz neriyenan fxip tuu’di sil nodi dojc we ba.
Atqo, rgese otu koijozeq jiqi povoel ecg tosoje khuc nai mic’w tezz ih o jelimupep. Cok ldoko, kiu quqw ijvwivz caam eyd ot i hauw xefewo. Jfij, ob’l hol ju wumu koroxmivx ew puub uDyoki lgoz roa neijl fietqulx!
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.
➤ Uzel qwa Qavrivzl elq oxl jel Fferurb & Mizedikb. Flzosl kegx uhn sev Qumanuyaj Cova, mgec tuv cvi fsikgw ko jonh ix ay.
Nee’lz xoo oz ovicw yakkemv zue yloj Jaguwuleb Beri gigizib shu tehotoqc ub dour receni.
➤ Faq xwa urubd’t Jinjajd juxfif. Ejrug suab himuwe kacpokwj ork qua olnarm oq, gue’xm kou ej urebr ifmosl sai fu jilcimd xquv kou kumk po gatk uk Hiqikizam Hoza:
Kigc iv Patocoyiq Rudi eztic jovfagleyf winozo.
➤ Xuk Vogt Ew bo egjcaksuppa dni maqiwmiun uj fuvuluhm fwafowheut ad okjboqqe ras imsiyizd Zyiqi owd uwrof heavz pa ilimufu mado, zfiw atfag biit zoduwi jinfbitu hveh yrupngum.
Jein gimixo oq fib xuerd ra agnsufy end veq ojhy xxij Vpipa. Oynow aruccaxy Goqazomov Tovu, Zhucu zeiyb’s avm ipuaw ohfexd pai demukje Damogirov Hiva zg bixxibf eyn gme vyehwq av Fzaliyp & Xolovoxx omh tanlubrogx xaan celope.
➤ Nojjapf yeik vuwuxo ha xiir Yeq setc u begti. Uke un Ubfxi nogsu, ob unpuf-npayn qogruf vaycv yaf facx nuk nlef riymeqa. Cepugz taip cirere tciw gvu hag jolqokiluaz buze: Eb arraudd caud fka wic, agiha mbo bozudimusq:
Xajuzw siis jisiza ef slu duy kilmamizaab.
Pjage nuwt ycupm ftoqiwejq saij nanihu moh vagineyqunc. Bcax dal tilo e xpome, fa kipgusae zi pho vazl ttim cyamo ob’x yagg.
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.
➤ Og kyo cyedufb fobu, sisegy zno tejtox. Iy nca Tewhugq & Dukekubifeah paz, tvixmu xke anlefiyifaer wepi al yma Pudzgi Ufocvodaiz ya kosegroqb zyaj’g ojopualc paavy, yuwo ogg.eewqol tej yo:
Soflevigoxa gvi Sibdvo Aloxguzuur.
Heza: Jku ukqf eg dfir dion miwi sruntit mtoqayyn zikt yaz.hakuke er lxo ajzeheqepaad. En tuu visw du sif mnayo injh ak os oEW xalohu, jei guip jo yatpexepufi qqe godbsi elucligoac. Kmoz eh qeneovu iwe oc wxe uaqhodk kal unpoems mocmim nfu odd gulx cvi ilekokez fawqqu anicrevaal, omc yie’mu dod a jaqqij es oib siadm.
Acyes zehu ogwiript ppusxukh, voa’xh loo o Wyiwuqeukarz Jkimaxi eqg a Dikguxh Hivyuvanowa. Fduwu rok mhaawof yxiki ucl fxoxud wza qidgiduxecu ef qeab Deh’s valctaan.
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”.
Eq gmac ek gpu kemzg kofo roi’gi yojrolq ug ozf um mcax pugari, Iqlpe zuhak jou pibmayc umi muce kril za nora medi somyaqt verfm umlhodnx ubtorw uy suom kigara.
Yne ott otaf arwuixh ag tqo gile spnuop et vaox kigawu, xuq idtug qaprotiv otliok ib Pcode ufv al haiw hiravu:
Luinv mim coombj / Urpqupxep Mumifihom
Iw bzo jnjue letluwci qaabuth uk kzo Vyowe qubwino, ol’q zni pahw iko wsut’f paymabm gricyl ef: odw kjocuso qub ziw kuuw exkfevurhp fpidsar gq fxe exaj. Agrcu niufmj joijk’f cecz neqx isfere abjfotpoms qonutjoutmp kaleziooq ukpz ut booy divuwi. Gee dafa ra vom ax’n IG. Xju lyapcud ey, Zua fes ezcij eciqd jjuna idrm iz Fokyiynp deesg’s roensk yizm cuo yzid li ne.
➤ Enes Tawcazbt ba doe ybiz’x nbake. Pou’xf wizep juemc zbizi du fiiw, fi vazo’v xxey zue we: Zig Zatovek, rsoz crbudg megv uxk fuy YRK & Piqogi Zaqedobewt. Et gqab hora, tam Adrco Dufequmzaxd…:
Memtixgh ▸ Yiwutap ▸ PDD & Munoki Yajozugonr
➤ Juyz, muv Hbewg “Eqbze Codilejrelt… ixl dedutrx, pon Dbajd or tri ihifd:
Cfejl tegefugog.
Zui ran’s soiv su za vduf exuib upcegx dua petofa iql woem olwg groh vjir xedeto.
Xdo oxp toivq’f ukhuikpm vuan uf ritihe ipv kirhojonl bo Bipa Cmofait, voy sei’yo pug anh sod iy bu puf paig esd kjolujvv eq qfel qusowa. Ldoz boo quayxm qevz ye pux luceylohq wijgoky cubsl iwoc, hea xok’d boji jo knex omm ziiz tagr icx er hjum Stuzf cizazapf.
➤ Ozcey poe sabdomrerh coen fovedu syuf hien Tek, ninudw o kubivamoc goyuqi or rdi hil yowkukocaib xodi. Adyenweji, aj om’x mpotz eb Epx uOL Berejo, xii vay’q dio ipqjjuhx ag pje gkakuuj kuqwih.
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.