iosswiftuiandroid-intentsirikitapp-shortcut

Can't load dynamically provided parameter values And handle method is not getting called in Intents in iOS


I am getting An unknown error occurred. when I tap on parameter which is dynamically provide in my shortcut inside Apples Shortcut app. And the handle(intent: TestIntent) async -> TestIntentResponse is not getting called.

Implemented the Handing protocol provided and generated by Xcode.

class SiriKitAppClosingIntentHandler: NSObject, LogAppClosingIntentHandling {
    func provideAppOptionsCollection(for intent: LogAppClosingIntent, searchTerm: String?) async throws -> INObjectCollection<SiriKitIntentAppName> {
        let appOptionsCollection = AppName.appOptionsCollection
        if let searchTerm = searchTerm, !searchTerm.isEmpty {
            return INObjectCollection(items: appOptionsCollection.filter({ $0.displayString.contains(searchTerm) }))
        } else {
            return INObjectCollection(items: appOptionsCollection)
        }
    }
    
    func handle(intent: LogAppClosingIntent) async -> LogAppClosingIntentResponse {
        print(" closing intent \(intent) \(#file) \(#line)")
        return LogAppClosingIntentResponse(code: .success, userActivity: nil)
    }
}

Solution

  • Main reason is not wiring up everything together correctly. Here are the brief intro and the check lists.

    SiriKit needs a handler that conforms to the corresponding intent handling protocol.

    Each intent object has an associated protocol based on the name of the intent. Ex: <IntentName>IntentHandling (Intent name: given in the SiriKit intent definition). And this protocol is created by Xcode (If you cant find it you need to build your app after you add one of your intent in the intent definition file)

    The protocol defines the methods that your handler implements to resolve any intent parameters and to let SiriKit know how your app handled the intent.

    There are two way that you can let Sirikit know your handler that you conform to <IntentName>IntentHandling protocol.

    A. Provide a Handler in Your Intents App Extension - lightweight , so that is quick.

    1. File -> New -> Target..

    Choose a template for your new target:

    1. Make availabe intent for the extension.

    enter image description here

    1. Implement <IntentName>IntentHandling protocol

    2. Return 3) implemented class in the handler method in the extension.

    3. Make sure your intent is included in the plist file of the extension under IntentsSupported . (otherwise add <Name Of The Intent>Intent)

    IntentsSupported


    B. Provide a Handler in Your App - need to wakeup your whole app ( with all the import statements. so that it is slow)

    1. Implement <IntentName>IntentHandling protocol

    2. In an iOS app or an app built with Mac Catalyst, implement application(_:handlerFor:) on your UIApplicationDelegate.

    If you’re using SwiftUI, use UIApplicationDelegateAdaptor

    1. Make sure your intent is included in the plist file of the App under Intents eligible for in-app handling . (otherwise add <Name Of The Intent>Intent)

    Intents eligible for in-app handling