核心Spotlight用户信息始终为空 [英] Core Spotlight Userinfo is always empty
问题描述
我正在使用CoreSpotlight api和NSUserActivity
api的组合来索引应用程序内容.一切正常,直到我点击搜索结果.在continueUserActivity方法中与userActivity一起传递的userInfo仅包含一项,即kCSSearchableItemActivityIdentifier
.我的其他自定义键是nil.
I am using a combination of CoreSpotlight api and NSUserActivity
api to index app content. Everything goes well until I tap a search result. The userInfo passed with userActivity in continueUserActivity method contains only one item i.e kCSSearchableItemActivityIdentifier
. My other custom keys are nil.
这是我的索引项目代码.
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!")
}
}
}
}
每当我尝试访问userInfo中的indexItemType或地址键时,它始终为零.
Whenever I try to access indexItemType or address keys in userInfo its always nil.
我已经尝试了以下线程的所有解决方案:
I have tried all the solutions from these threads:
http://helpprogramming. xyz/question/31328032/ios-9-nsuseractivity-userinfo-property-showing-null
以上均未解决我的问题. 我目前在iOS 9上使用Xcode 7.
None of the above solved my problem. I am currently using Xcode 7 with iOS 9.
推荐答案
我在这里遇到了同样的问题,而且我搜索了很多对我不起作用的答案... 幸运的是,我在 ios-9-tutorial-series-中找到了解决方案search-api
I had the same issue here, and i searched so many answers not working for me... fortunately i got my solution in ios-9-tutorial-series-search-api
我建议您阅读以上所有内容.我将为您提供两种替代解决方案:
I recommend you read all the content above. I will give you two alternative solutions:
-
使用NSUserActivity而不是CSSearchableItem:完成数据设置后,调用
userActivity.becomeCurrent
,则可以在聚光灯下找到您的数据(还有两件事:我没有iOS 9设备,因此仅在iOS 10设备上进行了测试,我上面提到的文章说NSUserActivity is limited to one activity per navigation point
,但我没有这类问题...)
use NSUserActivity instead of CSSearchableItem: when you done your data setting, call
userActivity.becomeCurrent
, then your can found your data in spotlight(just two more things: I don't have a iOS 9 device, so only tested in iOS 10 Device, and the article I mentioned above saidNSUserActivity is limited to one activity per navigation point
, but I don't have such kind issue...)
使用CSSearchableItem,将uniqueIdentifier
设置为包含您的自定义userInfo的JSON字符串.
use CSSearchableItem, set the uniqueIdentifier
to a JSON string that contains your custom userInfo.
注意:请勿为CSSearchableItem编制索引并同时调用userActivity.becomeCurrent
,否则您将获得两个关注的结果.其中一个是com.apple.corespotlightitem
,另一个是您的自定义activityType
NOTE: Do not index a CSSearchableItem and call userActivity.becomeCurrent
both, otherwise you will got two spotlight results. One of them is com.apple.corespotlightitem
, other one is your custom activityType
这篇关于核心Spotlight用户信息始终为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!