【问题标题】:Core Spotlight Userinfo is always emptyCore Spotlight 用户信息始终为空
【发布时间】:2015-12-22 23:11:14
【问题描述】:

我正在使用 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 或地址键时,它总是为零。

我已经尝试了这些线程的所有解决方案:

  1. iOS 9 - NSUserActivity userinfo property showing null

  2. http://helpprogramming.xyz/question/31328032/ios-9-nsuseractivity-userinfo-property-showing-null

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

【问题讨论】:

标签: ios indexing ios9 corespotlight ios-searchapi


【解决方案1】:

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

停止调用 CSSearchableIndex 并查看它是否有效(您还应该确保 nsuseractivity 对象是视图/模型上的强属性,因为它可以在有机会保存 userInfo 之前被释放)。

以下线程上的信息对我有帮助:

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

【讨论】:

    【解决方案2】:

    我在这里遇到了同样的问题,我搜索了很多对我不起作用的答案... 幸运的是我在ios-9-tutorial-series-search-api得到了我的解决方案

    我建议您阅读以上所有内容。我会给你两种替代方案:

    1. 使用 NSUserActivity 而不是 CSSearchableItem:完成数据设置后,调用userActivity.becomeCurrent,然后您可以在聚光灯下找到您的数据(还有两件事:我没有 iOS 9 设备,所以只有在 iOS 10 设备中测试,我上面提到的文章说NSUserActivity is limited to one activity per navigation point,但我没有这样的问题...)

    2. 使用 CSSearchableItem,将uniqueIdentifier 设置为包含您的自定义用户信息的 JSON 字符串。

    注意:不要为 CSSearchableItem 编制索引并同时调用userActivity.becomeCurrent,否则您将得到两个聚光灯结果。其中一个是com.apple.corespotlightitem,另一个是你的自定义activityType

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-09-09
      • 1970-01-01
      • 2016-11-22
      • 2017-06-07
      • 1970-01-01
      • 2020-04-21
      相关资源
      最近更新 更多