Introduction to CoreSpotlight and corespotlight

Source: Internet
Author: User

Introduction to CoreSpotlight and corespotlight

IOS9 exited the CoreSpotlight framework, which can provide some internal App data for iOS search, so that we can pull up and down the iPhone in the search box, search for the content of the App we use (of course, the App must be adapted for us to search ).

The following is a description of the relationship with WWDC Session 709 keynote:



Compared with NSUserDefault, CoreSpotlight is a global storage space. The difference is that CoreSpotlight is the storage space of the system, which can be accessed by every App (this access may be restricted and there is no time for research), but NSUserDefault is private to every App. In addition, CoreSpotlight stores items, that is, CSSearchableItem, and each CSSearchableItem has many attributes. These attributes are set through CSSearchableItemAttributeSet. All of them have the Shenma attribute. Let's check the header file.

Below are some simple steps:

1 introduce CoreSpotlight. framework

2. Create CSSearchableItemAttributeSet and CSSearchableItem

3. Call CSSearchableIndex. defaultSearchableIndex () to operate items.

Due to my limited level, I only found the operation to add or delete itme, and did not find the method to update itme. If anyone knows, please let me know.

The following code is a simple example of my testing:

Import UIKit @ UIApplicationMainclass AppDelegate: UIResponder, UIApplicationDelegate {var window: UIWindow? // This method func application (application: UIApplication, continueUserActivity userActivity: NSUserActivity, restorationHandler: ([AnyObject]?) will be called when you click from the search result. -> Void)-> Bool {// here, the identifier of the click search result can be obtained, but it is unclear whether it is necessary to let identifier = userActivity. userInfo? ["KCSSearchableItemActivityIdentifier"] print ("continueUserActivity \ (identifier !) ") Return true }}

/// ViewController. swift // SpotlightTest /// Created by mxy on 15/6/21. // Copyright©2015 mxy. all rights reserved. // import UIKitimport CoreSpotlightclass ViewController: UIViewController {let identifier = "com. mxy. test. identifier "var index = 1 // identifies the added itme override func viewDidLoad () {super. viewDidLoad () CSSearchableIndex. defasearchsearchableindex (). deleteAllSearchableItemsWithCompletionHandler {(error)-> Void in }}@ IBAction func insertItem () {let attributeSet = CSSearchab LeItemAttributeSet (itemContentType: "com. mxy. test ") attributeSet. title = "Meng xiangyue mxy \ (index)" attributeSet. contentDescription = "this: what should I do here? mxy \ (index)" // set the thumbnail of the search result. I don't know why it doesn't take effect. After I add an icon to the application, icon attributeSet is displayed in the search result. thumbnailURL = NSURL (fileURLWithPath: NSBundle. mainBundle (). pathForResource ("aa", ofType: "png ")!) AttributeSet. thumbnailData = UIImagePNGRepresentation (UIImage (named: "aa ")!) Let item = CSSearchableItem (uniqueIdentifier: "\ (identifier) \ (index)", domainIdentifier: "mxy", attributeSet: attributeSet) let tmpItmes: [CSSearchableItem] = [item] CSSearchableIndex. defasearchsearchableindex (). indexSearchableItems (tmpItmes) {(error)-> Void in} index ++} // it seems that there is no update operation. Therefore, you have to delete it based on identifier and add it after modification. @ IBAction func updateItem () {if index> 0 {let tmpIdentifier = "\ (identifier) \ (index-1)" CSSearchableIndex. defasearchsearchableindex (). deleteSearchableItemsWithIdentifiers ([tmpIdentifier], completionHandler: {(error)-> Void in}) let attributeSet = CSSearchableItemAttributeSet (itemContentType: "com. mxy. test ") attributeSet. title = "Meng xiangyue mxy \ (index-1)" attributeSet. contentDescription = "this is written here after update mxy \ (index-1)" let item = CSSearchableItem (uniqueIdentifier: tmpIdentifier, domainIdentifier: "mxy", attributeSet: attributeSet) let tmpItmes: [CSSearchableItem] = [item] CSSearchableIndex. defasearchsearchableindex (). indexSearchableItems (tmpItmes) {(error)-> Void in }}@ IBAction func deleteItem () {let identifiers = ["\ (identifier) \ (index) "] index -- if index <= 0 {return} CSSearchableIndex. defasearchsearchableindex (). deleteSearchableItemsWithIdentifiers (identifiers) {(error)-> Void in }}}

Only three buttons are added to the storyboard to associate the corresponding operation. The following is a demo. When you click Update, the content of the last item will be updated:


For example: http://download.csdn.net/detail/mengxiangyue/8827141

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.