Raj*_*esh 5 indexing ios ios9 corespotlight ios-searchapi
我正在使用CoreSpotlight api和NSUserActivityapi 的组合来索引应用程序内容。一切顺利,直到我点击搜索结果。在continueUserActivity方法中与userActivity一起传递的userInfo仅包含一项,即kCSSearchableItemActivityIdentifier。我的其他自定义键为nil。
这是我的索引项目代码。
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!")
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
每当我尝试访问userInfo中的indexItemType或地址键时,它始终为零。
我从这些线程尝试了所有解决方案:
以上都不解决我的问题。我目前在iOS 9上使用Xcode 7。
小智 2
如果您使用 CSSearchableIndex 那么您只会在 userInfo 中收到 kCSSearchableItemActivityIdentifier。要设置和检索 userInfo 中的自定义属性,您应该仅将 userActivity 设置为变为当前。
取消对 CSSearchableIndex 的调用并查看它是否有效(您还应该确保 nsuseractivity 对象是视图/模型上的强属性,因为它可以在有机会保存 userInfo 之前被释放)。
下面的线程信息对我有帮助:
https://forums.developer.apple.com/thread/9690
| 归档时间: |
|
| 查看次数: |
1605 次 |
| 最近记录: |