本文介绍了如何从Siri Intent Extension中使用NSUseractivity打开SwiftUI应用并执行功能?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序使用Intents Extensions创建了自己的快捷方式操作.他们完美地执行了后台操作.

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

对于某些操作,我正在尝试使意图扩展在快捷方式"中运行并执行功能时打开主(容器)应用程序.

For some actions, I'm trying to make the intent extension open the main (container) app when run in Shortcuts and perform a function.

我在使用NSUserActivity时遇到了麻烦,我不确定这是一个SwiftUI项目,还是我实施它的方式(或两者兼而有之).

I'm having trouble with NSUserActivity and I'm not sure if it's the fact it's a SwiftUI project or the way I'm implementing it (or both).

我已经在我的info.plist("com.me.project.activityName")中将NSUserActivity名称注册为NSUserActivityType.

I have registered my NSUserActivity name as an NSUserActivityType in my info.plist ("com.me.project.activityName").

我已将以下代码添加到我的AppDelegate中.

I've added the code below to my AppDelegate.

我在意图扩展内初始化了一个新的NSUserActivity,其类型与在info.plist中声明的类型相同.

I initialise a new NSUserActivity inside my intent extension with the same type as the one declared in info.plist.

我也尝试过在应用程序中声明活动(我认为我不需要这样做吗?)

I've also tried declaring the activity within the app (I don't think I need to do this?)

我正在跑步:iOS(iPhone XS,Beta 6)macOS 10.15 Catalina(测试版5)Xcode 11.0(Beta 5)

I'm running:iOS (iPhone XS, Beta 6)macOS 10.15 Catalina (Beta 5)Xcode 11.0 (Beta 5)

我的AppDelegate中有这个

I have this in my AppDelegate:

func application(_ application: UIApplication, continue userActivity: NSUserActivity, restorationHandler: @escaping ([UIUserActivityRestoring]?) -> Void) -> Bool {

          if userActivity.activityType == "com.me.project.activityName" {
              if let url = URL(string: "https://www.google.com") {
                  UIApplication.shared.open(url)
              }
              return true
          }
          return false
      }

这是我的意图扩展:

let openApp = intent.launchApp?.boolValue ?? false
        if openApp {

            let UA = NSUserActivity(activityType: "com.me.project.activityName")
            UA.title = "Dummy title"

            completion(UserActivityTestIntentResponse(code: .continueInApp, userActivity: UA))

        } else {
            completion(UserActivityTestIntentResponse.success(result: "You chose not to open the app with a user activity."))
        }

在info.plist

In info.plist

<key>NSUserActivityTypes</key>
    <array>
        <string>com.me.project.activityName</string>
    </array>

我在项目的一个快速文件中声明了这个(虽然我认为我不需要它):

I have this declared in a swift file in my project (though I don't think I need it):

let openURLActivityType = "com.me.project.activityName"

let viewPageActivity: NSUserActivity = {
    let userActivity = NSUserActivity(activityType: openURLActivityType)
    userActivity.title = "Dummy Title"
    userActivity.suggestedInvocationPhrase = "Dummy phrase"
    userActivity.isEligibleForSearch = false
    userActivity.isEligibleForPrediction = false
    return userActivity
}()

意想不到的结果

我希望当在快捷方式"中运行该操作时,我的应用程序会打开,并且网站" https://www. google.com ".

当前,在快捷方式"中运行操作后,我的应用程序启动到主屏幕,没有其他反应.在我的appDelegate中似乎没有断点.

Currently, after running the action in Shortcuts, my app launches to the home screen and nothing else happens. No breakpoints appear to be hit in my appDelegate.

如果是因为我使用NSUserActivity错误而导致无法解决,无论是因为它是SwiftUI,还是因为它无法在意图内正常工作.

I can't work out if it's because I'm using NSUserActivity wrong, whether it's because it's SwiftUI or whether it's something that just won't work from inside an intent.

非常感谢您的帮助!

推荐答案

就像您已经评论过的那样,当应用程序仍在后台时,可以使用func scene(_ scene: UIScene, continue userActivity: NSUserActivity)继续NSUserActivity.

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.

但是,当应用程序关闭时,您可以在SceneDelegate内的scene(_:willConnectTo:options:)方法内实现let userActvity = connectionOptions.userActivities.first,以访问用户想要继续的活动.

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")
            }
        }
    }
}

这适用于带有Swift 5 Xcode 11的Siri Intent.

This works for Siri Intents with Swift 5 Xcode 11.

对于越区切换,尽管不应按文档所述使用.userActivities,而是将调用关联的委托方法(scene(_:continue:)):

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

这篇关于如何从Siri Intent Extension中使用NSUseractivity打开SwiftUI应用并执行功能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-29 19:55