Core Spotlight Userinfo is always empty

I am using a combination of CoreSpotlight api and NSUserActivityapi to index the contents of an application. Everything is going well until I touch the search result. User information passed with userActivity in the continueUserActivity method contains only one ie element kCSSearchableItemActivityIdentifier. My other custom keys are null.

Here is my code for indexing items.

class MyTestViewController:UIViewController{
     viewDidLoad(){
        searchHandler = SearchHandler()
        searchHandler.index(items)
     }
 }

 class  SearchHandler{
         var activity: NSUserActivity!

         func index(items:[Item]){
            for item in items{
         let attributeSet = getSearchItemAttribute(item)
                if let attributeSet =  attributeSet{
                    let searchableItem = CSSearchableItem(uniqueIdentifier: item.uniqueId, domainIdentifier:itemType.groupId(), attributeSet: attributeSet)
                    searchableItem.expirationDate = item.expirationDate
                    addToSpotlight([searchableItem])
                }

            activity = NSUserActivity(activityType: searchPrivacy.activity())
            activity.delegate = delegate

            //meta data
            activity.title = item.title

            var userInfoDic = [NSObject:AnyObject]()
            userInfoDic["indexItemType"] = itemType.rawValue
            userInfoDic["address"] = item.title
            activity.userInfo = userInfoDic

            if item.expirationDate != nil { activity.expirationDate = item.expirationDate! }
            if item.keywords != nil { activity.keywords = item.keywords! }
            activity.contentAttributeSet = attributeSet

            //eligibility
            activity.eligibleForHandoff = false
            activity.eligibleForSearch = true
            activity.eligibleForPublicIndexing = true
            activity.requiredUserInfoKeys = Set(["indexItemType","address"])

            activity.becomeCurrent()
            }
 }

 private  func getSearchItemAttribute(item:Item) ->CSSearchableItemAttributeSet?{
    if item.contentType != nil { // add an entry to core spot light
        let attributeSet = CSSearchableItemAttributeSet(itemContentType: item.contentType!)
        attributeSet.relatedUniqueIdentifier = item.uniqueId
        HALog.i("item.uniqueId= \(item.uniqueId)")
        attributeSet.title = item.title
        attributeSet.thumbnailData = item.thumbnailData
        attributeSet.thumbnailURL = item.thumbnailUrl
        attributeSet.rating = item.ratings
        attributeSet.ratingDescription = item.ratingDescription
        attributeSet.contentDescription = item.contentDescription
        return attributeSet
    }
    return nil
}

private  func addToSpotlight(searchableItems:[CSSearchableItem]) {

    CSSearchableIndex.defaultSearchableIndex().indexSearchableItems(searchableItems) { (error) -> Void in
        if let error = error {
            HALog.e("Deindexing error: \(error.localizedDescription)")
        } else {
            HALog.i("Search item successfully indexed!")
        }
    }

   }
}

Whenever I try to access indexItemType or address keys in userInfo, it is always zero.

I tried all the solutions from these threads:

. Xcode 7 iOS 9.

+4
2

CSSearchableIndex, kCSSearchableItemActivityIdentifier userInfo. userInfo, userActivity currentCurrent.

CSSearchableIndex , ( , nsuseractivity /, , userInfo).

:

https://forums.developer.apple.com/thread/9690

+1

, , ... , ios-9-tutorial-series-search-api

. :

  • NSUserActivity CSSearchableItem: , userActivity.becomeCurrent, ( : iOS 9, IOS 10, , , NSUserActivity is limited to one activity per navigation point, ...)

  • CSSearchableItem, uniqueIdentifier JSON, .

. CSSearchableItem userActivity.becomeCurrent , . - com.apple.corespotlightitem, - activityType

0

Source: https://habr.com/ru/post/1608940/


All Articles