How to continue spotlight search and show content ViewController

399 Views Asked by At

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.

My app's structure is 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
}

enter image description here

2

There are 2 best solutions below

0
On BEST ANSWER

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
  }
0
On

In your ApplicationDelegate class implement this function:

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

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

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

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