1

在我的应用程序中,我有一个表格视图,其中每个单元格代表一个文件 URL。用户可以点击一个单元格,然后UIScene将打开一个代表该文件的新单元格...

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

正如您在上面的代码片段中看到的,我将 URL 存储在NSUserActivity传递给UIScene.

我的问题是userInfo在新场景中总是空的。

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

调试器命令...

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

我通过设置用户活动的标题进行了实验(我不在我的应用程序的任何地方使用活动标题)。标题确实进入了新场景,确认我正在查看的用户活动是正确的。

4

0 回答 0