6.4 C
Canberra
Monday, October 27, 2025

ios – Use Swift AppIntents/Shortcuts with SwiftData for Siri to immediate for a number of new document fields. Save the document to with the person responses


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 coated within the Apple docs and examples, I’ve not been capable of finding it.

Here is what I’ve thus far:

@Mannequin
class Factor {

    var title: String = ""
    var remark: String = "no remark"
    var recordType: String = "0"
    var rely: Int = 0

    init(title: String, remark: String, recordType: String, rely: Int) {
        self.title = title
        self.remark = remark
        self.recordType = recordType
        self.rely = rely
    }
    
}//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: "Title", default: "")
    var title: 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 title of the brand new Factor?",
          supporting: "I've opened the Add Factor Dialog.")

         let reply = $title.needsValueError("Present a reputation")
        title = reply.description
        
        saveThing(title: title, remark: remark)
        return .end result(dialog: dialog)

    }
    
    @MainActor
    personal func saveThing(title: String, remark: String) {

        let newThing = Factor(title: title, remark: remark, recordType: "0", rely: 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 can be appreciated. Xcode 16.1, iOS 18.1

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

[td_block_social_counter facebook="tagdiv" twitter="tagdivofficial" youtube="tagdiv" style="style8 td-social-boxed td-social-font-icons" tdc_css="eyJhbGwiOnsibWFyZ2luLWJvdHRvbSI6IjM4IiwiZGlzcGxheSI6IiJ9LCJwb3J0cmFpdCI6eyJtYXJnaW4tYm90dG9tIjoiMzAiLCJkaXNwbGF5IjoiIn0sInBvcnRyYWl0X21heF93aWR0aCI6MTAxOCwicG9ydHJhaXRfbWluX3dpZHRoIjo3Njh9" custom_title="Stay Connected" block_template_id="td_block_template_8" f_header_font_family="712" f_header_font_transform="uppercase" f_header_font_weight="500" f_header_font_size="17" border_color="#dd3333"]
- Advertisement -spot_img

Latest Articles