swiftios8appdelegatecorespotlight

Using spotlight in iOS app with deployment target iOS 8


I have iOS app with minimal deployment target set to iOS 8.0 and I want to enable spotlight search in it. I understand that spotlight search can be used only in iOS 9 and higher. That is why in my AppDelegate I am using available

@available(iOS 9.0, *)
func application(_ application: UIApplication, continue userActivity: NSUserActivity, restorationHandler: @escaping ([Any]?) -> Void) -> Bool {
    if userActivity.activityType == CSSearchableItemActionType {
        if let uniqIdentifier = userActivity.userInfo?[CSSearchableItemActivityIdentifier] as? String {
            print(uniqIdentifier)
        }
    }
    return true
}

And Xcode gives me this error

Protocol 'UIApplicationDelegate' requires 'application(_:continue:restorationHandler:)' to be available on iOS 8.0 and newer

What can I do with it


Solution

  • I found solution so I decide to post it as it may be useful to other developers. After import of CoreSpotlight

    import CoreSpotlight
    

    You should add application(_:continue:restorationHandler:) method if you are targeting iOS 8.0 and newer. But as 'CSSearchableItemActionType' is only available on iOS 9.0 or newer you should add version check inside method.

    func application(_ application: UIApplication, continue userActivity: NSUserActivity, restorationHandler: @escaping ([Any]?) -> Void) -> Bool {
    
        if #available(iOS 9.0, *) {
            if userActivity.activityType == CSSearchableItemActionType {
                if let uniqIdentifier = userActivity.userInfo?[CSSearchableItemActivityIdentifier] as? String {
                    print(uniqIdentifier)
                }
            }
        }
        return true
    }