SpotLight主要功能是在iOS9系統界面下拉會出現搜索功能界面,在這裡可以搜索關鍵信息,來展示應用內的內容,並且點擊信息打開應用跳轉具體頁面等操作。這個功能還是很有用的,面對這越來越多的應用霸占你手機,給你的應用加上這麼一個搜搜功能,還是有點與眾不同,主要是提升逼格。 1、導入頭文件 impo ...
SpotLight主要功能是在iOS9系統界面下拉會出現搜索功能界面,在這裡可以搜索關鍵信息,來展示應用內的內容,並且點擊信息打開應用跳轉具體頁面等操作。這個功能還是很有用的,面對這越來越多的應用霸占你手機,給你的應用加上這麼一個搜搜功能,還是有點與眾不同,主要是提升逼格。
1、導入頭文件
import CoreSpotlight
import MobileCoreServices
2、設置應用內搜索
func indexItem(title: String, desc: String, identifier: String) { let attributeSet = CSSearchableItemAttributeSet(itemContentType: kUTTypeText as String) attributeSet.title = title attributeSet.contentDescription = desc attributeSet.keywords = ["abc","cd","efg"] let item = CSSearchableItem(uniqueIdentifier: "\(identifier)", domainIdentifier: "Lingye.OopsVoice", attributeSet: attributeSet) CSSearchableIndex.default().indexSearchableItems([item]) { error in if let error = error { print("Indexing error: \(error.localizedDescription)") } else { print("Search item successfully indexed!") } } } func deindexItem(identifier: String) { CSSearchableIndex.default().deleteSearchableItems(withIdentifiers: ["\(identifier)"]) { error in if let error = error { print("Deindexing error: \(error.localizedDescription)") } else { print("Search item successfully removed!") } } }
3、在Appdelegate中實現SpotLight搜索
func application(_ application: UIApplication, continue userActivity: NSUserActivity, restorationHandler: @escaping ([UIUserActivityRestoring]?) -> Void) -> Bool { if userActivity.activityType == CSSearchableItemActionType { if let uniqueIdentifier = userActivity.userInfo?[CSSearchableItemActivityIdentifier] as? String { deindexItem(identifier: uniqueIdentifier) } } return true }