如何继续 Spotlight 搜索和显示内容 ViewController

How to continue spotlight search and show content ViewController

我已经把 Spotlight 搜索搞定了,我现在面临的问题是如何根据在 spotlight 中按下的项目显示内容视图。

我的应用程序的结构是 UITabVC>UINavigationVC>UICollectionVC>UIVC


spotlight和代码如下所示

// Continue Spotlight Search
func application(_ application: UIApplication, continue userActivity: NSUserActivity, restorationHandler: @escaping ([Any]?) -> Void) -> Bool {
    if userActivity.activityType == CSSearchableItemActionType {
        let uniqueIdentifier = userActivity.userInfo?[CSSearchableItemActivityIdentifier] as! String
        let id = uniqueIdentifier.components(separatedBy: "_")
        let rootTabVC = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "RootTabVC") as! RootTabVC

        print(id[0], id[1], separator: " - ", terminator: "\n")
        // printed "craft - Shovel"

        switch id[0] {
        case "craft" :
            let craftVC = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "CraftDetailVC") as! CraftItemDetailVC
            let craftRootCollectionVC = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "CraftRootCollectionVC") as! CraftCollectionVC
            let craftItemsCollectionVC = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "CraftItemsCollectionVC") as! CraftItemsCollectionVC
            // MARK: - TODO show vc

        case "character" : break
        case "mob" : break
        case "plant" : break
        case "recipe" : break
        case "thing" : break
        case "material" : break
        default: break
        }
    }

    return true
}

在你的ApplicationDelegateclass中实现这个函数:

func application(_ application: UIApplication, continue userActivity: NSUserActivity, restorationHandler: @escaping ([Any]?) -> Void) -> Bool {

然后您可以通过以下方式访问被点击的 CSSearchableItemuniqueIdentifier

if let searchActivityIdentifier = userActivity.userInfo [CSSearchableItemActivityIdentifier] as? String { }

然后您需要做的就是根据searchActivityIdentifier值将用户重定向到相关的视图控制器。(推送、呈现等)

即使我的其中一个应用程序也有同样的问题。然后我在网上冲浪寻找解决方案,here 最好的示例和代码及其解释。

  func application(_ application: UIApplication,
    continue userActivity: NSUserActivity,
    restorationHandler: @escaping ([Any]?) -> Void) -> Bool {

      guard userActivity.activityType == Employee.domainIdentifier,
        let objectId = userActivity.userInfo?["id"] as? String else {
          return false
      }

      if let nav = window?.rootViewController as? UINavigationController,
        let listVC = nav.viewControllers.first as? EmployeeListViewController,
        let employee = EmployeeService().employeeWithObjectId(objectId) {
          nav.popToRootViewController(animated: false)

          let employeeViewController = listVC
            .storyboard?
            .instantiateViewController(withIdentifier: "EmployeeView") as!
          EmployeeViewController

          employeeViewController.employee = employee
          nav.pushViewController(employeeViewController, animated: false)
          return true
      }

      return false
  }