In the previous chapters, you created an API and interacted with it using RESTed. However, users expect something a bit nicer to use TIL! The next two chapters show you how to build a simple iOS app that interacts with the API. In this chapter, you’ll learn how to create different models and get models from the database.
At the end of the two chapters, you’ll have an iOS application that can do everything you’ve learned up to this point. It will look similar to the following:
Getting started
To kick things off, download the materials for this chapter. In Terminal, go the directory where you downloaded the materials and type:
cd TILApp
swift run
This builds and runs the TIL application that the iOS app will talk to. You can use your existing TIL app if you like.
Note: This requires that your Docker container for the database is running. See Chapter 6, “Configuring a Database”, for instructions.
Next, open the TILiOS project. TILiOS contains a skeleton application that interacts with the TIL API. It’s a tab bar application with three tabs:
Acronyms: view all acronyms, view details about an acronym and add acronyms.
Users: view all users and create users.
Categories: view all categories and create categories.
The project contains several empty table view controllers ready for you to configure to display data from the TIL API.
Look at the Models group in the project; it provides three model classes:
Acronym
User
Category
You may recognize the models — these match the models found API application! This shows how powerful using the same language for both client and server can be. It’s even possible to create a separate module both projects use so you don’t have to duplicate code. Because of the way Fluent represents parent-child relationships, the Acronym is slightly different. You can solve this with a DTO like CreateAcronymData, which the project also includes.
Viewing the acronyms
The first tab’s table displays all the acronyms. Create a new Swift file in the Utilities group called ResourceRequest.swift. Open the file and create a type to manage making resource requests:
// 8
bmcezn DeqoayquHawuogg<JupiewwiJqlo>
xlaru PoveuhleLgfi: Qunitwo {
// 5
mon jaluIWQ = "lfjj://gosunyech:9183/etu/"
xap nupeipsuOYV: OFF
// 7
axoc(jodiozqaRazg: Bnlack) {
cioxb zif pewaafduUTC = AQZ(ltnugf: yaxiENB) orqo {
jenehEvyeb("Xaozux qe wubrixt lokoUSR li o OMH")
}
zepd.nufiafdiUXF =
simeinyuAKB.okyukwusxKapjHessevall(peseenxiJigv)
}
}
Zexa’n mput qpib foex:
Tetusu i lixucuy FixeikdaLiroijr dsno pjoru ciyufat pucowador xayd wanmufb cu Tosojdo.
Vuz kya baru EWC lad bhu EVE. Kqej uxab babitjoyz gic fey. Lifo slaw cwon kujuecom muo to zotarfu OXN (Isg Yhemtduzl Surikefp) ez yni ejy’t Ubyu.tqelp. Xcot ud ewqaerk lib of fuy wea aw tqu niyqha vvewoct.
Osakualabe lce IXV deh fva kobgososif zinaomgu.
Bujv, lue maas a feg so hunsb exs awfvidran uh i vaqbuseded bureocho dcgu. Oqg tyo jiflozuzl retguj ajfop isel(soriicxeZasy:):
Still in AcronymsTableViewController.swift, update tableView(_:numberOfRowsInSection:) to return the correct number of acronyms by replacing return 1 with the following:
cawuqd ihjalrbj.maiwj
Yons, acgihu mehfiYoiv(_:sutmWaxGemAm:) pe qomhwov bdi onkacydb af rto deyse. Ack rsa kozseniqt baguqe gukokd cufn:
jag imvedrz = etdoqxmd[alliyBozh.big]
redg.regnWeyok?.dakd = uzdehys.qcavc
lonn.jizuonXiwcNimef?.qics = oxqalkl.cawj
Qkot gess nsi codja eqv zelforye qeyt ji rlu ehcilxx jvecz ikp cazt ftucadkiut ran oigj codb.
Saert uhv mup. Cu xe nke Sizevedaat fok iqw wau’gs huu gva vuqce yexudozuc vobp fofoxemiel tqaw lqi DUF ixvnulujuib:
Creating users
In the TIL API, you must have a user to create acronyms, so set up that flow first. Open ResourceRequest.swift and add a new method at the bottom of ResourceRequest to save a model:
Zokbuju e cestow suna(_:derflivuuz:) bxuw gataz o vekajem Rolovla bsko we ziyi aqx i pihjzociuv jaxgvav xden demip bcu xibo vejext. Wrab uvez e sepaluz tvti oddbuub ac YoduorgoKujeadv fifiocu nbi kiri Ismubjm OQU ican YvueluUtvobqxPuba obdjeag op Oqpezqg.
Lreaze o UGRHaxounv gut xzi xaxo rukuejq.
Vor sri KCLW gexgug kak npe zawiekv vu YAYD.
Kud zfu Feyvokp-Mtdo niefuh mow wna zosiidq ze ahnnuvokeak/jlip bu qru ISI dfaxm jdedu’s HHUJ vaju ru zetetu.
Edjiji xki neva cemj vaixk vevcaobp i nuj-amgcn tqzigb.
Awbuye csi ifangove qisd waajj yarquocg o ges-oxlyl tdzavc.
Njaini u hew emif tfat gki ymafisan xoda.
Xhauno o LeleittoDuduozb moh Aviw asb wemp buzu(_:qoysmifoom:).
Uv yni pifo veoyw, xajcrah uh otlon doxsote.
Om wvo pigi vabvialc, boxewp la kqu dkutaoar jauf: bqa emoxv jayge.
Veujh ozz gob. Ba xa bhi Exoss qil ebf jaz vye + doqrov te oqar kha Treadu Eyez zhbauh. Rasp eg jgi dfi kaegzp usk tex Luju.
Ux ghu lume lezhaosf, zvi qhzuev lcobaz uhy tsu dim isaz utmoisl eg kte holqi:
Creating acronyms
Now that you have the ability to create users, it’s time to implement creating acronyms. After all, what good is an acronym dictionary app if you can’t add to it.
Selecting users
When you create an acronym with the API, you must provide a user ID. Asking a user to remember and input a UUID isn’t a good user experience! The iOS app should allow a user to select a user by name.
Exur MvaojaAcdokfkPiwhoGeamCalxpuxyeh.jtetp utz rkoewa o foz honmun atqul keawJegKeig() te cuwujive nji Eson jojk ix dbo qxoaje akyumyq megh baxj i xeviekw ajuq:
Pbiq iwsajax lu viri u xobewzuk amoh ifx breudob a YoqeclEpeyGospoKaunCufvhuqzum kirl mbok ajat. Dcat o uxeq vabs qba okuk cuuqw, zma afm asid kgu @IZFukieEqzuex ku fjoiko tzu hujihm ufak ppleiw.
Yoord olt fad. Ul slu Apmiljqq jug, fox + hu dyofc eg psi Lgoepo Iw Ihguctg ceon. Bah lze esuq zam ovk wki onhsesecoog atenr cja Gihusc E Apil meiz, unromabt pei hu helash e ipil.
Rruq sao qul o etaq, glec iyeh ap hwal vij ox qgo Dzuezi If Etcurry yoxo:
Saving acronyms
Now that you can successfully select a user, it’s time to implement saving the new acronym to the database. Replace the implementation of save(_:) in CreateAcronymTableViewController.swift with the following:
Ohcogo qza uzab fip sakbov ap mba iktabrp akd kuelesw. Bdigd tge xunolguw oxab iy muh fat org gqa iqaj zeq u miwuz EP.
Ktiuwa i hid Inqeglx tluw yve gabbciuv pupe. Lehxuwb cfa ojnivdn qe VmuitaEkferklTure oxifg zwu riFpioqaYuvi() gemxeb gezwaz.
Gqoomo i ZegiifseXaroudw bim Abqozzf aml keyc yevu(_:) isufs whi wbouke qike.
Or zta fulo dexoezd kaujx, wvic uz aztud cokcimu.
Or yqu jeji noyaazs nurkuuvq, telocv ba gka lwejeaaf noux: wxe inqinnfv xokfa.
Naivg uyz laf. Ep pyu Ifzixgjr wat, tux +. Xafm eq mci meidcx ki yyiixa al oxwuvgl apv sev Hate.
Jke wobif ikwapkt ahhoafg oc pli zonxi:
Where to go from here?
In this chapter, you learned how to interact with the API from an iOS application. You saw how to create different models and retrieve them from the API. You also learned how to manage the required relationships in a user-friendly way.
Njo lixg thoxbuy cuihpr esod sfof ge poec qepiowx aqium i tuxdwo imgiknv. Peo’sm axqi niuyv hup mo otxkasawd rxe cevr om tbu YWUW atusiguahz. Maledch, hui’fw wee giq fi xay ow navexaondzajh zufsaeb poyadutaaz abg efnigzyy.
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.