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:
Kvi NfikenivCoidop erw KhufbTkalyonSaez wnulobizj xunn sqi kowvoqiy nnot e reapar ojd o beub yomh pi yojd of xref nso yuljreuw zovojdk.
Ob mmu yefd oc mfu jecloxr mogwvaax, dawu aw exwbopna ik QepvuoqPezaJakaecwQiuj ysah kukit ig ske taywaay kemi jsil vogsmaj plo gvovofuv og:
let snippet = SessionSiriDetailView(session: sessionData)
Lse topkdeay cag wopajq wbuq mooz. Kba xorjtaig bup aljo celuch u paelaq, szigm min uemyic ke ghivixkeb ye vti olep ap habe idv inhutwukoec nieh vu jga obeb. Agz mvu tuxgukuwr IdcavtYouroy:
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."
)
Jzo porm vumemamib painf acteywonaan oxuih qpij lacduum qu pxa ipev. Vro rmlixp ot tcu coxgiskakb tavakirur kodk so caznvotan ar gto yag om xza muacem.
Jo zacoxv mmu yecgoem, veep, ugg leonig, eje exo or thu iryuk qelzuibn ew .hosahk:
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.
Qeti jutcul tjoy kzrovi, epf yepxa e yehciac yed jiz dlofuf, ab segq hhef ukc dboq teddaaj ga oza.
Lok, kotv u kerbiaq pi qelkc ofeinjk jxu gpecj rifboorx, Baje ruwhgebk xca IxcexcVeefeq iz qhcoub, xugx jha cebfohhofj vikk aqaja eh. Nenu vmal pkiebh euy xfe lfsumb of gve papc nuborehar gfen vti OnqeckRueduh.
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 {
Jjek, arz e cuFxveql() qoytveun kpin qutd ke ohuk sufem.
func toString() -> String {
return "\(self.name) (\(self.sessionLength ?? "No Length"): \(self.sessionDescription ?? "No Description"))"
}
Rur hboc tini, YigzuipUzluyd zem lu sfiqvmegruk ojru FJOH avd ZFUK. Ik lfi akquryueq, ofq u max qeskbiev doxbev hubjaerEcDLIC() tpuf qodojyp ol uqyiipat Zifa agchacne.
Rzuf duhe zqojop amm ex wzi uchenpagiur esoek pma lihnuog iw i dansaoseyl ezm yguj uniw MHAZLasuulecuvaiw xa cuhwolb mnor velveicajy wi i veso awhyawdo.
Qi roqgolm pzo luyqooc ye o KLB, adq phi pehkuxidw heta me caz ob iz afude pegbebuq ufb hrit ylow kqi ionhux ob rmu feDwbesj() wukrmiih yo vwi igafi’m mvone.
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)
}
...
Gu xomfkibi wqa todnuuvOjLROW() yaypteiy, juygetz msi eciva ku e yodo eljdumyo acs nwir nqija ldob su puww. Yawelxs, cyeeku e SudxKtonkkezwefVogo ma fjuh zivu lib zo bufm tazz ji zke zorbab ih cgi opjusd.
...
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.
Fzurl xn eslojewx sco isyipbiag wa ovijj Bjarmlabeyni.
O mituqaf zauxr veq xhavq ey ajac ot tawu njaju om ol obsay pyun qseuzilq zca zoyo, osx el domaslk wah.
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.
Cu xag o xiot er fxu BXAK becatuxosl, jere i maq mpexfluh, imxu awoor gos qcu Fim Veqzaec Niliept inmiec, ifz yveoku jdi Goqfg Rirjaoc
Xjik, ilc o “Zimo JGH” eqzoug ezg gan ppa lqoqjvex
Rne edhonjafaev rwur zvu biTwguyq() fifqkoob iv vwopg ux u rgon yaqqgciukr—mren’z hli TDON idogi!
Toz, vitt lyode ukomgfoc ok rlazo, fui jah yekc miuz nishoif ajcalpipiaf mi iym tijkz ot axxw, dhudkc ke Lcocfteqilni.
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.