【发布时间】:2016-06-14 05:09:49
【问题描述】:
我阅读了一些教程,发现NSUserActivity 用于用户进行活动时的索引信息,Core Spotlight 用于索引应用程序中的一组内容数据。
使用 NSUserActivity
var activity = NSUserActivity(activityType: "com.example.demo.searchapi")
activity.title = contactEntity.name
activity.userInfo = ["id": contactEntity.uid]
activity.eligibleForSearch = true
activity.keywords = NSSet(array: [contactEntity.name,contactEntity.phoneNumber, contactEntity.email]) as! Set
activity.becomeCurrent()
使用核心聚焦
let attributeSet = CSSearchableItemAttributeSet(itemContentType: kUTTypeContact as String)
attributeSet.title = contactEntity.name
attributeSet.relatedUniqueIdentifier = contactEntity.uid
let searchableItem = CSSearchableItem(uniqueIdentifier: contactEntity.uid, domainIdentifier: "com.example.demo.searchapi", attributeSet: attributeSet)
CSSearchableIndex.defaultSearchableIndex().indexSearchableItems(searchableItems) { error in
if let error = error {
print("Error indexing: \(error)")
} else {
print("Indexed.")
}
但我认为两个解决方案结果之间没有区别。如果用户进行活动,我可以使用Core Spotlight,而不是NSUserActivity。它给了我同样的结果。
那么,为什么 Apple 必须提供两种结果相同的不同解决方案?
【问题讨论】:
-
NSUserActivityin iOS 9 有contentAttributeSet,它的类型是CSSearchableItemAttributeSet -
我知道。但是 CSSearchableItem 也使用 CSSearchableItemAttributeSet 参数初始化。那么为什么 Apple 提供两个结果相同的 API(NSUserActivity 和 Core Spotlight)。我没有发现任何差异
-
Core Spotlight 不支持公共索引,而 NSUserActivity 支持。 NSUserActivity 可用于设备上和公共索引。
标签: ios corespotlight