Chris
Chris

Reputation: 1417

UserActivities in SwiftApp not showing up in Shortcuts

I have migrated an iOS App from UIKit/ViewController to SwiftApp/SwiftUI lifecycle.

I have put my UserActivity identifier into plist.info NSActivityTypes array:

<key>NSUserActivityTypes</key>
<array>
<string>OpenMyApp</string>
</array>

On my MainView I have put this:

.onContinueUserActivity("OpenMyApp", perform: { activity in
  print(#fileID, #function, "Executing: OpenMyApp")
})

However the UserActivity is not showing up in Shortcuts after I started my app.

What I am missing ?

Upvotes: 0

Views: 40

Answers (2)

JanApotheker
JanApotheker

Reputation: 1906

You need to set isEligibleForPrediction to true on the activity. Code example:

.userActivity("viewing-photo", { activity in
            activity.title = "View photo"
            activity.isEligibleForSearch = true
}

Upvotes: 0

Chris
Chris

Reputation: 1417

I found out that in a SwiftApp it is sufficient to provide:

  import Foundation
  import CoreSpotlight
  import CoreServices
  import AppIntents

  struct OpenAppIntent: AppIntent {
    static var title: LocalizedStringResource = "Open App"
    static var openAppWhenRun: Bool = true
    static var isDiscoverable: Bool = true
    
    @MainActor
    func perform() async throws -> some IntentResult {
      return .result()
    }
  }

and expose it via:

  struct AppShortCuts: AppShortcutsProvider {
      static var appShortcuts: [AppShortcut] {
        AppShortcut(intent: OpenAppIntent(), phrases: ["Open \(.applicationName)"])
      }
  }

The main 'trick' is to provide (.applicationName) to the phrase. Otherwise it will not become exposed to Shortcuts during install !

Upvotes: 0

Related Questions