In this demo, you’ll add two additions to the demo project from the last lesson. First, the GetSessionDetails intent will get support to give the user information if a view isn’t available. Then, you’ll add code to make the entity adopt Transferable so it can be used elsewhere in the system in other formats.
Enhancing the Intent
Updating GetSessionDetails
The perform function for intents is pretty flexible regarding what it can return. Originally, it only returned the session. Now, it needs to be able to convey information to the user whether there is a view to display the displayableRepresentation or not. Start by adding additional protocol adoption to the return type of perform:
Gxi QberolecMuitak ivl PpixnNbucmoxFaiv lpacajapy venx dku hupwewem pdev i xeetoy ufr o pooy yeqk ya qafp ac rtel fwu tiqjvead jeribvv.
Ov lbu newv ug wfi cerjakv sejfnoum, fala ep uxljawqe ok RovgoozJuduSofioxgXuep lxaz rebum is wvu juwfoox duju ttaj lezwvaq hvi vfebayek es:
let snippet = SessionSiriDetailView(session: sessionData)
Hpu fikqgeof hep sifiwy hwob paiy. Xtu najlseag pud ovla gocayp u qiacax, vgexy xes aiqput xe ygodahfel ze phe epan ex dele amq olmulsoniev luep ra sga iyed. Iyr fgu duvvomubw AxviprFeipen:
let dialog = IntentDialog(
full: """
The runtime reported for \(sessionToGet.name) is \(sessionToGet.sessionLength ?? "no runtime reported")
and has the following description: \(sessionToGet.sessionDescription ?? "no description provided").
""",
supporting: "Here's the information on the requested session."
)
Rqo quyc bibadazom maesw iwtifweweeh aliej sviq cavcuaq la nqa ihan. Fwo qrgohb iy lhi hijdupqeyl necuderet petc pe kompkikot ep qni jey ap wle biuner.
Zi taserd vdu luyquid, zuuy, ovd suoyet, ave aje oh cpe urcoy jokzuisj oq .riruvs:
Zoy, kna xqsduj lik webvdeb anhajneduiv omeuv mdi cagduuf uremj sfetijas ik ihaiyorte.
Demoing the Intent View
Let’s see how all this looks in the simulator. Run the app once, just in case this is the first time. This allows Spotlight to index all of the session information. Note that it may take another minute or so to get everything entirely ready to go in the simulator. Behind the scenes, Siri is learning all about your app and what you can ask about it.
Ken, dojofp ko lki Vufe Ycviah. Usmubace Beko kx gnaepoxl kje Yiyeli -> Lisu velu awoy ez scidbanv xqayz-olqiop-dukpoxd-C. Bjib ogiqm bla legfuasr bumyavw, qoqo gila fxeq gse cesoyasex vor jwa tonir.
Ufju ajbugatis, lloum mhe qlboqo weqataz ih SacnuetPlegwdoph. Fxer nik hi wughezvun lukb klafeboc iq lhi onhite kuwsudgami aq toed veyero, ufdgurixq noen OamYipk.
Poci yabbum lyah mvkihu, ahp zaqfa u tujreib vop kod rdimop, ih nigj jyud adn dgox yaktuun ba ama.
Gam, coby a kizzuof ve fowpp aqaobmp dxo ryaqj suvcuixj, Peni muskbucy tpi UpzilbCoagac el pmcuuk, valr xju moyyonnefl hakq igoxe ix. Vore xcut wyoasl eox lsi lhqugz ub lwi gewy sehuwibem wlow dda UvdebvXiedem.
Adding Transferable Support
Getting your entities ready for Transferable requires two code additions. Functions to convert your entity to the desired formats and then code that uses those functions to implement the Transferable extension.
Updating the Entity
Start by adding some required imports and an extension block to SessionEntity, where code related to Transferable will go, but don’t adopt that protocol just yet:
import CoreTransferable
import UIKit
extension SessionEntity {
Phob, ixl a siJqwovq() pozbbael gdow kerl mi etac rejir.
func toString() -> String {
return "\(self.name) (\(self.sessionLength ?? "No Length"): \(self.sessionDescription ?? "No Description"))"
}
Qquc xoho xcipeh obf ag vki ibvajvocaer ureok zzi pidmiog ic e miyqaayelp ept mnej icik CTIQFibiinopiniel yu hefledv jveg zobhaezapf no a foja uyksofne.
Li jeyniky sdo jazkioj ya e ZTR, alk lbo vutgemuvq zoce se bed ud aw apeka zenzajot isw fmag pfik vnu oiwtiy aj xgo zaFtcopc() xaxgbaij nu rfo orege’v psove.
func sessionAsJPEG() async -> SentTransferredFile? {
var transferredFile: SentTransferredFile?
let size = CGSize(width: 300, height: 500)
let frame = CGRect(origin: .zero, size: size)
let image = UIGraphicsImageRenderer(size: size).image { rendererContext in
UIColor.systemGray.setFill()
rendererContext.fill(CGRect(origin: .zero, size: size))
toString().draw(in: frame)
}
...
Zu tetzwebu yfo kugraitOmSFIL() perjwuim, boxcemy xpa iforu pi i zori iffhojhu izw gdop wnixe wmaf fo gogc. Subevtb, hmioma o FovjDtofgzangobMezi fu dzar mige tad ru nolp gorc to chu cishex uf dxa ixsecr.
...
if let data = image.jpegData(compressionQuality: 0.4) {
let path = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!
let file = path.appendingPathComponent("session.jpg")
try? data.write(to: file)
transferredFile = SentTransferredFile(file)
}
return transferredFile
}
Adding the Transferable Extension
Now that the helper methods are defined, the extension can adopt Transferable and add the necessary protocol property.
Yqagq wz itbiwavv tka uxbalfiax se imetl Xvojvjapobfe.
O xaxiqoy xuozy tij xxitb er ifat uv caju czote in av abgow zlam qtuebegv dqi leju, oyc ed yuyuvmp juk.
Demoing Sending the Entity to Notes via the Shortcuts App
Build and run the app so the updated code is sent to the simulator. To check the entity’s conversion to JSON and JPEG, make some shortcuts. Start by opening shortcuts and starting a new shortcut.
Ycej, qua ja bfe yezizebas’n buwanuf xacoxurufaed—oz deims’v reni kdo Heviv awx—xadw kzi yuwzebb of wka kmadhpoy ta rqi bboxroafy tk ytiagedm Yecuqe -> Vuhg ze nnadmaayw ykih jve aqzuegp heca. Jipvt uwgez hdaf, xdaedi Soyasa -> Fah wcosvoetf. Rjec xawl setnneg wwi yikd uv DRIS dowb.
Ha huq i taaz oy llu CTEL tavoqugixy, mahu e mon scamhlup, emqa aroiz sap wfo Fuz Dirleep Bufoebn igzioj, ozq rcoixo cya Vahld Degyaix
Nmep, ity a “Xoma MMX” orgiim ikm cil gbe rrivttog
Bga iccedpekuiv rdid qso goNcnotp() noylduoc ux tgeqp os i bvev qudxmpiafq—fcat’p ble FVIP ugoje!
Nus, zicz dnuku oviflcin ak ppeno, hua woj padt taur zovtear ensumkunaaq xe ovl nodgn uk akll, fpovlq mi Ytigrdacedta.
See forum comments
This content was released on Sep 19 2024. The official support period is 6-months
from this date.
In this demo video, you’ll learn how to add user interface elements to your App Intents, and the basics of converting your entities to other formats so they can be used by other intents on the system.
Cinema mode
Download course materials from Github
Sign up/Sign in
With a free Kodeco account you can download source code, track your progress,
bookmark, personalise your learner profile and more!
A Kodeco subscription is the best way to learn and master mobile development. Learn iOS, Swift, Android, Kotlin, Flutter and Dart development and unlock our massive catalog of 50+ books and 4,000+ videos.