NSUserActivity for a newly requested scene has no user info

258 Views Asked by At

In my app I have a table view where each cell represents a file URL. The user can tap a cell and a new UIScene will open representing that file...

// In some view controller
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    let url: URL = dataSource.itemIdentifier(for: indexPath)!

    let userActivity = NSUserActivity(activityType: ViewController.activityType)

    userActivity.addUserInfoEntries(from: [
        ViewController.ActivityKey.url : url
    ])

    UIApplication.shared.requestSceneSessionActivation(nil,
                                                       userActivity: userActivity,
                                                       options: nil)

    tableView.deselectRow(at: indexPath, animated: true)
}

As you can see in the snippet above, I'm storing the URL in an NSUserActivity which gets passed to the UIScene.

My problem is that the userInfo is always empty in the new scene.

// In SceneDelegate.swift

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

    let navController = window!.rootViewController as! UINavigationController
    viewController = navController.viewControllers.last as? ViewController

    let userActivity =
        connectionOptions.userActivities.first ??
            session.stateRestorationActivity

    if let someActivity = userActivity {
        // ✳️ BREAKPOINT HERE ✳️
        viewController.restoreUserActivityState(someActivity)
    }

    viewController.setupThenOpenDocument()
}

Debugger commands...

(lldb) p someActivity.userInfo
([AnyHashable : Any]?) $R2 = 0 key/value pairs

I did experiment by setting the title of the user activity (I don't use activity titles anywhere in my app). The title does make it to the new scene, confirming that the user activity I'm looking at is the correct one.

0

There are 0 best solutions below