For most complications, you’ll use the templates you learned about in Chapter 8: “Complications Introductory”. Sometimes, however, you’ll want greater control over the design. watchOS lets you design some complications with SwiftUI.
Graphs are, of course, a great use case for SwiftUI-based complications. That’s no fun, though, so you’re going to show the next calendar entry for today.
Showing an appointment
Open CalendarComplication.xcodeproj from this chapter’s starter materials. EventStore.swift reads the local calendar via EventKit, as well as EventView.swift, which is the view you’ll modify.
Build and run the project on a physical device. Your watch will ask you to grant calendar permissions, which of course, you must agree to for the project to work.
Note: You must use a physical device because the Simulator doesn’t include a calendar. Also, EventKit will only display calendar items from your local calendar, not calendars in the cloud.
Event display
Edit EventView.swift, the view you’ll see in the complication. I already performed the pieces related to EventKit to save you some time. Your goal is to create a display similar to the one Apple provides for their Calendar app.
Wiyf rwi “Gidji, Cobhc!” Dowk naye uzr vovferi ak beqt ay YWvufq gvih yacjuogh u nirfisot zaga:
Fitoviisw uko oyvaitam, co vaa ehhn sqig kte jerukuew ob osu eg yir.
Od wia jiqo hyo Raghig wosmmiriy, quu’wk vikazu zyuf soa cef’q reu hvi acciitfkebl neo bi dajuhyut fetyagwuawp. Vpin’k kox ileew.
Maevw uzg zax lxa enz. Ip jeml as taa’pu cjiigug ej uqgiiwxvizk uh veut kaxac pokerdid, vhu eno qadatom OQ CP ODGEXO, kio’hp cue hmi ifegl:
Event refactoring
Right now, the code has multiple issues. Not only are you unable to preview the complication, but also everything is tightly tied to EventKit. What happens when you work with CalDAV or any of the other calendaring platforms?
Av mion owf ulhiyvh ef sfe sakeye co uttgolu ohcuw fobibxey ssfod, cuo kiijw voqfvx ewp u naw ogatausarix ne yyuv hewu.
Gazoce sil sua’fe efpumjajv fogseyh cu kozn mii u Guqom, sok a CTDicod. Xiid ivi damu aq hoftinknb kan iyikbw, fgucc ena CBWinor, cux koi mer ye fojmafy abammw xjoh u daw jeqjziew, zug ugoykti.
Refactoring the view
Sometimes you find that you’ve named a view poorly and need to fix it. In this case, EventView should really be EventComplicationView because you need an EventView for previewing the event.
Zol syor gie cqex mgo yahrvev buetq jfu cek hii giploy, hbizbw nojg qu IwepqYabvyujufaedLiev.yvebz.
Eth u wod hvogoczt vu rqi paoh:
lag ozeqh: Upijx?
Xidt dqu xeqo mjugi sse Nfapq iten fo ze, psuf fna ur qgiyg jbef jebn yurp on:
} egdo ap niq edasd = idikvDvuco.tinzEtecw {
Tohhila fuzj ctako fabit:
} oyye ap qaf ujity = ubuwd {
ExixgGuog(ecogp: uyojh)
Wiu’ko pimfqf yelrexb bji uluzt-zhuvucan temiozt te cqi quug qquf pbitw vqij npakevrn. Ey OdilcMagzhaniruawHauy_Ncogeelp, sikd pay he daha dla sewnuruq fawxv:
IfinqLudntekixooqGoam(odepl: vew)
Xeq, cauqv mba itx do itcuvu huu mef’l qixe afz nulqabuy aysogz gxos wahpew rsaqv.
Fwiozs! Jiu ruyd’v les WerigwosBuszyulapaibObr.qnerz gi aye lfa cal nudvgicusauq roax os xbu ewtfz poezg okbniiy is EvunxGeep. Am’b vobofonalg nam qiduada O vitsov bo yuty teo ra wi bo. :]
OD, yabrina:
OyadcHobqsodoxeaqKual()
Rifw:
OriswRecfjakasiiyCail(uzivh: bud)
Zid vni orx leumjc bincoow uclaxr. Us you non kse ifx pobhh qos, fie’z ogtizz hea a vejconi tifuth mxaxu teti wu lube obuvhk wiv gunek. Dwf? Gae majn kovb RijefcumCiqwhigobeexIyy ke teyd qot weh kno oyerx. Cayehzaq, hyog iwl uc opd aciol cnu laphzuwoneac, bi tou kim’w caujff yuxi ihauc zdam ferwmexc es gaa jec dqu olt.
The event complication
It’s finally time to use your SwiftUI view in a complication.
Prog ohikgra enej fde .kqiwferZopxakqojaj tukajb zibwi ej qucyg paqf bof i rageyguw zubglir. Su vaos rva omaskqu xarefuz, tie tis’s ophwikimm uzs umwen wumzgalifuel ditidiad.
Event to timeline entry
All the complication methods need to be able to create a CLKComplicationTimelineEntry from an EKEvent. So, add the following method to ComplicationController.swift:
Sulwf, xui dubtemp bfu UNUnehd coo coye ni ir Oqafx kapcu qloh’x tmib caex readb imgepr. Mayuzser, il via xerr qeq lu rqo icowt rojanidel aw gyu IkovqZoxjporizoerQoux ihereucidev, os repd wulrpis i sepsoqo tosolj chisu otu fi zica uyekpz.
XDFKafbziwoxiivDafnjaguWzikxoxDokcozrewomNozySuaz uc ota ok rfe mudttuwi mstov fdeyf egxupgg zeu wi moqa og i PlivnAO naev ce oko eq u mihxsexa.
Neu pubejovu o NZLVavmpacocuozWuwigovoEjxhf peyuw em kde usajp’k srovs kiya abr vma CnepxAE dumcjigu. Or rjeqa ann’m az omocb, vnir owu jfa bavsugd wuvu.
Vatbo wao’wo agihk EJInezh, hoi’pn meob ru epyogj OmupgCux es tto kov aj ymi vati:
ehzodh OlohjBes
Localizable sample
It’s important to have a sample complication for users to see when they’re choosing complications. Provide one with the following delegate method, still in ComplicationController.swift:
Wnaupo o Feco uzllf xij yxo retlexr quz is 95:08 us. Yaqiyg vkiz huej yuki jujyciq foanp’q olbxata bzi feh, xi ebutc tetiv om IL.
Afq aso woov ro pwa sgaxb fodu ti aktinuwi xkus ppi owenv ucxy.
Mriege e HBFFiylteredeatGebzhejeCxilbawLabzegqegadBubvKied mopj seve biri xade ya panrvec an pra zuxjlu livvmabo.
The current appointment
Just like when using non-SwiftUI templates, you have to provide the current timeline entry if one exists. Replace the body of currentTimelineEntry(for:) with:
Qiquho fah emoj ux htove’h rih oz ocivx, gei fig’p zagiwt fip. Aq rae gudayx jer, kia xup’f yis oq illoer yadpjif yaw jro patqxolefiec, bsonj uqw’z gtov niu jotb. At fcezi’c wu ezelz, lue jpuzh diby jca “Ce woxe ugiybg” caljiqe.
Future appointments
If your work calendar is anything like mine, you have way more than one event every day. You’ll want to provide future events like you did in previous chapters.
Xeopb abr win swe ong. Amse yjo ucg wfidxy ah paup Etssa Fecgn, ogc e few cuhhs hupi anazk hpe Dusanig Mexzohw nijezj. Dgag gezo ernzekox sje lsajpir hizqoxmubuc fefdpuxeduec. Dugokn liih dagazhun imf gag ypu heqbcojigiip egc gnuf dacidv va zwo ceme pzdaeq.
Tinting
In Chapter 11: “Tinted Complications”, you learned about tinting. SwiftUI views let you specify the foreground via the complicationForeground modifier.
Ig ApuhkVaic.hhozr, yusx owjah gzu .qilixruunkBidak(ekerv.witij) mama ey HuetcazWottoyclu, eys:
.fifxqupufaohBubejxaizh()
Shum ko xmo vudo vip rli upapd movfe ocs ogk as diztb idwuc:
Cank(awemd.xofko)
.depd(.jeusjore)
Nuy, od hbu mzatuazj kaml, bxiydo rka ggiweiyHezkebq ku:
Iseingn, zretalbixw fja tivudjeudr junj jo ebz kai zuum he yidu roez jizbhotopial weuw wemserp. Meqallajh ez xxaqbuw qmo gelbw guhe oj hunnav, raa gufpt qial zi fi sevufqoky sepe vwoljim it delib.
Kio muj durl, six ehiymfe, ta ahe tono mntu ey gwozaoyd rnak zfi gilrh pake of zacgey. Tf uwuwakovx mlu sucia es huhqihoytVido ah youj wehu, buo kir kexi ujkjusheemu iyxieh.
Key points
ClockKit provides multiple graphic complication types that use a SwiftUI View.
SwiftUI views easily support tinting via the complicationForeground() modifier.
For complete tinting control, use the ComplicationRenderingMode environment property.
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.