-2.5 C
New York
Friday, December 13, 2024

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


I’m attempting to setup Shortcuts and AppIntents to permit a consumer to summon Siri and open a dialog to create a brand new file 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 file however have been unable to seize a consumer 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 up to now:

@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.")
    static var openAppWhenRun: Bool = true

    @AppStorage("navigateToThingAddView") personal var navigateToThingAddView: Bool = false

    @MainActor
    func carry out() async throws -> some IntentResult {
        navigateToThingAddView = true
        return .consequence()
    }
}//create new factor

struct ThingAddDialog: AppIntent {

    static var title: LocalizedStringResource = "Add Factor Dialog"
    static var description = IntentDescription("Immediate the consumer 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 .consequence(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 = strive! ModelContainer(for: Factor.self)
        container.mainContext.insert(newThing)
        
        strive? 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 steerage could be appreciated. Xcode 16.1, iOS 18.1

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest Articles