iosswiftuiapplicationcorespotlight

How to continue spotlight search and show content ViewController


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


Solution

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