ios – Core Spotlight Userinfo始终为空

我使用CoreSpotlight api和NSUserActivity api的组合来索引应用程序内容.一切顺利,直到我点击搜索结果.在continueUserActivity方法中使用userActivity传递的userInfo只包含一个项目,即kCSSearchableItemActivityIdentifier.我的其他自定义键是零.

这是我的索引项目代码..

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!")
        }
    }

   }
}

每当我尝试访问userInfo中的indexItemType或地址键时,它总是为零.

我尝试过这些线程的所有解决方案:

> iOS 9 – NSUserActivity userinfo property showing null
> http://helpprogramming.xyz/question/31328032/ios-9-nsuseractivity-userinfo-property-showing-null

以上都没有解决我的问题.
我目前正在使用iOS 9的Xcode 7.

最佳答案 如果您使用CSSearchableIndex,那么您将只在userInfo中收到kCSSearchableItemActivityIdentifier.要在userInfo中设置和检索自定义属性,只应将userActivity设置为becomeCurrent.

保留对CSSearchableIndex的调用并查看它是否有效(您还应该确保您的nsuseractivity对象是您的视图/模型上的强属性,因为它可以在它有机会保存userInfo之前取消分配).

下面的帖子中的信息帮助我:

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

点赞