Saturday 7 September 2019

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

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 make the intent extension open the main (container) app when run in Shortcuts and perform a function.

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).

What I've tried

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

I've added the code below to my AppDelegate.

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?)

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

My Code Thus Far

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
      }

This is within my intent extension:

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


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

Unexpected Results

I'm expecting that when the action is run in Shortcuts, my app opens and the website "https://www.google.com".

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.

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.

Thank you so much in advance for any help!



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

No comments:

Post a Comment