问题
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!")
}
}
}
}
Whenever I try to access indexItemType or address keys in userInfo its always nil.
I have tried all the solutions from these threads:
iOS 9 - NSUserActivity userinfo property showing null
http://helpprogramming.xyz/question/31328032/ios-9-nsuseractivity-userinfo-property-showing-null
None of the above solved my problem. I am currently using Xcode 7 with iOS 9.
回答1:
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:
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...)use CSSearchableItem, set the
uniqueIdentifier
to a JSON string that contains your custom userInfo.
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
回答2:
If you use CSSearchableIndex then you will only receive the kCSSearchableItemActivityIdentifier in the userInfo. To set and retrieve custom properties in the userInfo you should only set the userActivity to becomeCurrent.
Stub out your call for the CSSearchableIndex and see if it works (you should also make sure your nsuseractivity object is a strong property on your view/model, as it can get deallocated before it has a chance to save the userInfo).
The info on the thread below helped me:
https://forums.developer.apple.com/thread/9690
来源:https://stackoverflow.com/questions/32776758/core-spotlight-userinfo-is-always-empty