1

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.

DanubePM
  • 1,526
  • 1
  • 10
  • 24
  • Instead of calling `userActivity.addUserInfoEntries`, what if you just call `userActivity.userInfo =`? – matt Mar 01 '20 at 02:27
  • I tried that. Same thing. – DanubePM Mar 01 '20 at 22:12
  • Found a solution for iOS 14: There seems to be an issue with URL types in the userInfo dictionary. Not sure if the value itself is evaluated but replacing the file URL by a string solved the issue for me. My dictionary only had two entries, a String and a file URL. Both values were cleared. Now the two Strings are passed to the new scene. There is some indication about the analysis of URLs in the userInfo dictionary in the documentation from Apple: "The system may translate file scheme URLs that refer to iCloud documents to valid file URLs on a continuing device." – Dr. F. Jul 22 '21 at 11:29

0 Answers0