iOS 9~ Experiments - New API Components - Internal Search APIs with CoreSpotlight.
-
= XCode 8.0.
-
= Swift 3.
-
= iOS 9.0.
Tested on iOS 9.1 Simulators, iPhone 6, iPhone 7.
this is the Xcode 8 / Swift updated project.
To support HTTP hosted : add the Boolean type Value to YES
for NSAllowsArbitraryLoads
in app's info.plist
file.
To run the example project, download or clone the repo.
- Set at least the following attributes:
title
,contentDescription
,thumbnailData
,rating
, andkeywords
. - Add
CoreSpotlight
+MobileCoreServices
Frameworks
import CoreSpotlight
import MobileCoreServices
- CoreSpotlight example
// defines an object that represents an item that can be indexed..
var searchableItems: [CSSearchableItem] = []
// define Item Attribute Set
let attributeSet = CSSearchableItemAttributeSet(itemContentType: kUTTypeItem as String)
attributeSet.title = item.title
// define search keywords
var keywords = item.title.componentsSeparatedByString(" ")
keywords.append(item.category)
attributeSet.keywords = keywords
// create searchable item
let searchItem = CSSearchableItem(uniqueIdentifier: item.title, domainIdentifier: "domain-id", attributeSet: attributeSet)
searchableItems.append(searchItem)
- Defines an object that represents the on-device index
// On-device Index
CSSearchableIndex.defaultSearchableIndex().indexSearchableItems(searchableItems) { (error) -> Void in
if error != nil {
print(error?.localizedDescription)
}else{
print("items indexed witch success!")
}
}
Build and Run! Switch to Search View in your simulator or devices and test by typing one keyword!