Skip to content

iOS 9 Experiments - New API Components - Internal Search APIs with CoreSpotlight.

Notifications You must be signed in to change notification settings

Sweefties/iOS9-NewAPI-CoreSpotlight-Example

Repository files navigation

iOS 9 - New API - CoreSpotlight - Example

iOS 9~ Experiments - New API Components - Internal Search APIs with CoreSpotlight.

Example

Requirements

  • = XCode 8.0.

  • = Swift 3.

  • = iOS 9.0.

Tested on iOS 9.1 Simulators, iPhone 6, iPhone 7.

Important

this is the Xcode 8 / Swift updated project.

App Transport Security Settings

To support HTTP hosted : add the Boolean type Value to YES for NSAllowsArbitraryLoads in app's info.plist file.

Usage

To run the example project, download or clone the repo.

Example Code!

  • Set at least the following attributes: title, contentDescription, thumbnailData, rating, and keywords.
  • 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!

About

iOS 9 Experiments - New API Components - Internal Search APIs with CoreSpotlight.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages