Kevvv
Kevvv

Reputation: 4023

The userActivity function doesn't get called in AppDelegate.swift

When I tap on the result of the Core Spotlight search, the app launches, but the function in AppDelegate.swift doesn't get called:

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

which contains a logic for navigating to a specific view controller and loading data.

Some people here seem to recommend using SceneDelegate.swift:

func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
 print("called"
}

but doesn't get called from Core Spotlight, either.

The only functions that seem to be being called are:

func sceneDidBecomeActive(_ scene: UIScene) {}
func sceneWillEnterForeground(_ scene: UIScene) {}

in SceneDelegate.swift, but doesn't have the needed userActivity parameter.

Upvotes: 2

Views: 482

Answers (1)

Ben Robinson
Ben Robinson

Reputation: 1745

Not strictly the correct answer, this is for SwiftUI, but incase others land here!

Took me ages to find this, so despite being an old question, thought I should add the answer that worked for me!

I found the answer here: https://betterprogramming.pub/implement-core-spotlight-in-a-swiftui-app-859cb703f55d

Rather than an AppDelegate function, it is just added as an modifier to the main content view:

.onContinueUserActivity(CSSearchableItemActionType) { userActivity in
    if let uniqueIdentifier = Int(userActivity.userInfo?[CSSearchableItemActivityIdentifier] as? String ?? "") {
        print("id: \(uniqueIdentifier)")
    }
}

Upvotes: 0

Related Questions