CoreSpotlight簡單入門,corespotlight入門
iOS9退出了CoreSpotlight架構,這個架構可以為iOS的搜尋提供一些App內部的資料,能夠使我們在iPhone上下拉出現得搜尋方塊中,搜尋我們使用的App中的內容(當然App必須做了適配我們才能搜尋到)。
下面借用WWDC Session 709 keynote的一張說明其中的關係:
對於CoreSpotlight可以類比NSUserDefault,都是全域的儲存空間。不同的是CoreSpotlight是系統的儲存空間,每個App都能訪問(可能這個訪問有限制,目前還沒有時間研究),但是NSUserDefault是每個App私人的。另外對於儲存的內容CoreSpotlight儲存的是item,即CSSearchableItem,而每個CSSearchableItem又有許多屬性,這些屬性是通過CSSearchableItemAttributeSet進行設定。具體都有神馬屬性,大家自己去看標頭檔吧。
下面寫一下簡單得步驟:
1 引入CoreSpotlight.framework
2 建立CSSearchableItemAttributeSet、CSSearchableItem
3 調用CSSearchableIndex.defaultSearchableIndex()的相關的方法對item進行操作。
由於本人水平有限,只找到了添加、刪除itme的操作,並沒有找到更新itme的方法,如果誰清楚了,麻煩告知一下。
下面貼出本人測試的一個簡單例子的代碼:
import UIKit@UIApplicationMainclass AppDelegate: UIResponder, UIApplicationDelegate { var window: UIWindow? // 從搜尋結果點擊的時候將會調用這個方法 func application(application: UIApplication, continueUserActivity userActivity: NSUserActivity, restorationHandler: ([AnyObject]?) -> Void) -> Bool { // 這裡能夠擷取到點擊搜尋結果的identifier 但是不清楚是不是應該這樣做 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 // 用於標識添加的itme override func viewDidLoad() { super.viewDidLoad() CSSearchableIndex.defaultSearchableIndex().deleteAllSearchableItemsWithCompletionHandler { (error) -> Void in } } @IBAction func insertItem() { let attributeSet = CSSearchableItemAttributeSet(itemContentType: "com.mxy.test") attributeSet.title = "孟祥月 測試 mxy \(index)" attributeSet.contentDescription = "this 這裡寫點什麼好呢 mxy \(index)" // 設定搜尋結果的縮圖 不知道 為何就是不生效 我給應用程式添加了icon後,搜尋結果那裡顯示的是icon attributeSet.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.defaultSearchableIndex().indexSearchableItems(tmpItmes) { (error) -> Void in } index++ } // 貌似是沒有更新操作 所以只好根據identifier先刪除,修改後再添加進去。 @IBAction func updateItem() { if index > 0 { let tmpIdentifier = "\(identifier) \(index - 1)" CSSearchableIndex.defaultSearchableIndex().deleteSearchableItemsWithIdentifiers([tmpIdentifier], completionHandler: { (error) -> Void in }) let attributeSet = CSSearchableItemAttributeSet(itemContentType: "com.mxy.test") attributeSet.title = "孟祥月 測試 mxy \(index - 1)" attributeSet.contentDescription = "this 這裡寫點更新後 mxy \(index - 1)" let item = CSSearchableItem(uniqueIdentifier: tmpIdentifier, domainIdentifier: "mxy", attributeSet: attributeSet) let tmpItmes: [CSSearchableItem] = [item] CSSearchableIndex.defaultSearchableIndex().indexSearchableItems(tmpItmes) { (error) -> Void in } } } @IBAction func deleteItem() { let identifiers = ["\(identifier) \(index)"] index-- if index <= 0 { return } CSSearchableIndex.defaultSearchableIndex().deleteSearchableItemsWithIdentifiers(identifiers) { (error) -> Void in } }}
在storyboard中只是添加了三個按鈕,關聯對應的操作。下面是示範,點擊更新的時候會更新最後一個item的內容:
例子代碼的:http://download.csdn.net/detail/mengxiangyue/8827141