我在 iOS9Sampler 中看到下面的代码在Search API
iOS 9 中使用。它同时使用NSUserActivity
和CSSearchableIndex
. 所以我想问一个问题:
- 什么时候用
NSUserActivity
,什么时候用CSSearchableIndex
?在 Spotlight 中搜索时,我看到它得到相同的结果。 - 下面的代码调用每个
viewDidLoad
视图控制器。这是正确的吗?还是应该只调用一次?我怎样才能检查一次通话?
NSUserActivity
let activityType = String(format: "%@.%@", uniqueIdentifier, domainIdentifier)
activity = NSUserActivity(activityType: activityType)
activity.title = "iOS-9-Sampler_NSUserActivity"
activity.keywords = Set<String>(arrayLiteral: "dog", "cat", "pig", "sheep")
activity.eligibleForSearch = true
activity.becomeCurrent()
核心聚焦
let attributeSet = CSSearchableItemAttributeSet(itemContentType: kUTTypeImage as String)
attributeSet.title = "iOS-9-Sampler_CoreSpotlight"
attributeSet.contentDescription = "iOS-9-Sampler is a code example collection for new features of iOS 9."
attributeSet.keywords = ["dog", "cat", "bird", "fish"]
let image = UIImage(named: "m7")!
let data = UIImagePNGRepresentation(image)
attributeSet.thumbnailData = data
let searchableItem = CSSearchableItem(
uniqueIdentifier: uniqueIdentifier,
domainIdentifier: domainIdentifier,
attributeSet: attributeSet)
CSSearchableIndex.defaultSearchableIndex().indexSearchableItems([searchableItem]) { (error) -> Void in
if error != nil {
print("failed with error:\(error)\n")
}
else {
print("Indexed!\n")
}
}