I’m attempting to setup Shortcuts and AppIntents to permit a person to summon Siri and open a dialog to create a brand new document with two or extra SwiftData mannequin attributes. I’ve efficiently coded for Siri to open the app and for Siri to open the app and navigate to the web page so as to add a document however have been unable to seize a person response from a Siri immediate and save that to a brand new information. If that is lined within the Apple docs and examples, I’ve not been capable of finding it.
Here is what I’ve to this point:
@Mannequin
class Factor {
var identify: String = ""
var remark: String = "no remark"
var recordType: String = "0"
var depend: Int = 0
init(identify: String, remark: String, recordType: String, depend: Int) {
self.identify = identify
self.remark = remark
self.recordType = recordType
self.depend = depend
}
}//class
struct CreateNewThing: AppIntent {
static var title: LocalizedStringResource = "Create New Factor"
static var description = IntentDescription("Opens the app and strikes to the Add Factor display screen.")
static var openAppWhenRun: Bool = true
@AppStorage("navigateToThingAddView") personal var navigateToThingAddView: Bool = false
@MainActor
func carry out() async throws -> some IntentResult {
navigateToThingAddView = true
return .end result()
}
}//create new factor
struct ThingAddDialog: AppIntent {
static var title: LocalizedStringResource = "Add Factor Dialog"
static var description = IntentDescription("Immediate the person for a reputation and remark for a brand new Factor.")
static var openAppWhenRun: Bool = true
@Parameter(title: "Identify", default: "")
var identify: String
@Parameter(title: "Remark", default: "No remark")
var remark: String
@MainActor
func carry out() async throws -> some ProvidesDialog & ShowsSnippetView {
// Logic to navigate to the Add Factor dialog view
//navigateToThingDialog = true
let dialog = IntentDialog(
full: "What's the identify of the brand new Factor?",
supporting: "I've opened the Add Factor Dialog.")
let reply = $identify.needsValueError("Present a reputation")
identify = reply.description
saveThing(identify: identify, remark: remark)
return .end result(dialog: dialog)
}
@MainActor
personal func saveThing(identify: String, remark: String) {
let newThing = Factor(identify: identify, remark: remark, recordType: "0", depend: 0)
// Add newThing to SwiftData
let container = attempt! ModelContainer(for: Factor.self)
container.mainContext.insert(newThing)
attempt? container.mainContext.save()
}
}
struct ThingShortcuts: AppShortcutsProvider {
static var shortcutTileColor = ShortcutTileColor.navy
static var appShortcuts: [AppShortcut] {
AppShortcut(
intent: CreateNewThing(),
phrases: [
"Create a new Thing in (.applicationName)"
],
shortTitle: "Create a Factor",
systemImageName: "doc.badge.plus"
)
AppShortcut(
intent: ThingAddDialog(),
phrases: ["Add Dialog in (.applicationName)"],
shortTitle: "Add Factor Dialog",
systemImageName: "plus.sq."
)
}//var
}//factor shortcuts
Any steering could be appreciated. Xcode 16.1, iOS 18.1