如何继续聚光灯搜索和显示内容ViewController

时间:2022-09-10 19:13:39

I had get Spotlight search all sorted out, the problem I'm facing now is how to show the content view based on the item which has been press in spotlight.

我已经整理了Spotlight搜索,我现在面临的问题是如何根据聚光灯下的项目显示内容视图。

My app's structure is UITabVC>UINavigationVC>UICollectionVC>UIVC

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


spotlight and code is shown below

聚光灯和代码如下所示

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

如何继续聚光灯搜索和显示内容ViewController

2 个解决方案

#1


1  

even I have same problem in my one of the app. Then I was surfing on internet for solution, here the best sample example and code along with it's explanation.

即使我在我的应用程序中遇到同样的问题。然后我在网上冲浪寻找解决方案,这里有最好的示例和代码以及它的解释。

  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
  }

#2


5  

In your ApplicationDelegate class implement this function:

在ApplicationDelegate类中实现此函数:

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

func应用程序(_应用程序:UIApplication,继续userActivity:NSUserActivity,restorationHandler:@escaping([Any]?) - > Void) - > Bool {

Then you can access the clicked CSSearchableItem's uniqueIdentifier by:

然后,您可以通过以下方式访问单击的CSSearchableItem的uniqueIdentifier:

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

如果让searchActivityIdentifier = userActivity.userInfo [CSSearchableItemActivityIdentifier]为?字符串{}

Then all you need to do is to redirect user to related view controller based on searchActivityIdentifiervalue.(push, present etc.)

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

#1


1  

even I have same problem in my one of the app. Then I was surfing on internet for solution, here the best sample example and code along with it's explanation.

即使我在我的应用程序中遇到同样的问题。然后我在网上冲浪寻找解决方案,这里有最好的示例和代码以及它的解释。

  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
  }

#2


5  

In your ApplicationDelegate class implement this function:

在ApplicationDelegate类中实现此函数:

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

func应用程序(_应用程序:UIApplication,继续userActivity:NSUserActivity,restorationHandler:@escaping([Any]?) - > Void) - > Bool {

Then you can access the clicked CSSearchableItem's uniqueIdentifier by:

然后,您可以通过以下方式访问单击的CSSearchableItem的uniqueIdentifier:

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

如果让searchActivityIdentifier = userActivity.userInfo [CSSearchableItemActivityIdentifier]为?字符串{}

Then all you need to do is to redirect user to related view controller based on searchActivityIdentifiervalue.(push, present etc.)

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