Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 859
Kyle Bing Avatar asked Dec 07 '25 19:12

Kyle Bing


2 Answers

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.)

like image 122
Ayazmon Avatar answered Dec 10 '25 11:12

Ayazmon


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
  }
like image 38
Jayachandra A Avatar answered Dec 10 '25 11:12

Jayachandra A



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!