How can I open my SwiftUI app and perform a function using NSUseractivity from within Siri Intent Extension?

前端 未结 1 627
一整个雨季
一整个雨季 2021-02-04 16:17

My Problem

My app has its own Shortcuts actions created using Intents Extensions. They perform background actions perfectly.

For some actions, I\'m trying to m

相关标签:
1条回答
  • 2021-02-04 16:56

    Like you already commented you can use func scene(_ scene: UIScene, continue userActivity: NSUserActivity) to continue the NSUserActivity when the app is still in the background.

    However when the app is closed you can implement let userActvity = connectionOptions.userActivities.first inside the scene(_:willConnectTo:options:) method inside the SceneDelegate to access the activity the user wants to continue.

    Just adding this piece of code to the standard implementation of this method would look like this:

    func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
        // Use this method to optionally configure and attach the UIWindow `window` to the provided UIWindowScene `scene`.
        // If using a storyboard, the `window` property will automatically be initialized and attached to the scene.
        // This delegate does not imply the connecting scene or session are new (see `application:configurationForConnectingSceneSession` instead).
    
        // Use a UIHostingController as window root view controller
        if let windowScene = scene as? UIWindowScene {
            let window = UIWindow(windowScene: windowScene)
            window.rootViewController = UIHostingController(rootView: ContentView())
            self.window = window
            window.makeKeyAndVisible()
    
            //Standard implementation until here
            if let userActvity = connectionOptions.userActivities.first {
                if let intent = userActivity.interaction?.intent as? YourIntent {
                    print(“do something”)
                }
            }
        }
    }
    

    This works for Siri Intents with Swift 5 Xcode 11.

    For Handoff though the .userActivities should not be used as the documentation says, instead the associated delegate methods will be called (scene(_:continue:)):

    This property does not contain user activity objects related to Handoff. At connection time, UIKit delivers only the type of a Handoff interaction in the handoffUserActivityType property. Later, it calls additional methods of the delegate to deliver the NSUserActivity object itself.

    0 讨论(0)
提交回复
热议问题