【Swift】SpotLight搜索
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 }
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 一个奇形怪状的面试题:Bean中的CHM要不要加volatile?
· [.NET]调用本地 Deepseek 模型
· 一个费力不讨好的项目,让我损失了近一半的绩效!
· 百万级群聊的设计实践
· 永远不要相信用户的输入:从 SQL 注入攻防看输入验证的重要性
· 全网最简单!3分钟用满血DeepSeek R1开发一款AI智能客服,零代码轻松接入微信、公众号、小程
· .NET 10 首个预览版发布,跨平台开发与性能全面提升
· 《HelloGitHub》第 107 期