Core SpotLight
来源:互联网 发布:网络安全法案例 编辑:程序博客网 时间:2024/06/05 23:04
原文
今天看到网上这篇关于SpotLight的文章,个人觉得随着手机内存容量的越来越大,搜索功能会显得越来越重要。苹果在iOS9就推出了SpotLight,通过使用iOS9中提供的Core Spotlight 框架,手机上的任何一款应用都可以通过Spotlight功能搜索到。在Spotlight上通过用户的搜索行为找到app的关键在于使用Core Spotlight API索引到我们应用的数据。但是既不是我们的app也是不是CS API接口决定应该设置什么类型的数据。所以我们需要提供特定的数据格式给 API接口。配置SpotLight的内容的相关代码不多,就是有一些注意点需要注意。
首先是设置一些关键字或者是图片方便搜索时查找。这里用到的框架有CoreSpotlight,用到的类有:CSSearchableItemAttributeSet、CSSearchableItem、CSSearchableIndex。
主要代码如下:在ViewController实现
var searchItems = [CSSearchableItem]()
for i in0 ... self.dataArr.count -1{
let title = self.dataArr[i] as [String : String]
let content = self.arr[i] as [String : String]
let searchableItemAttributesSet = CSSearchableItemAttributeSet(itemContentType: "124563")
// title、contentDescription thumbnailURL等常用的关键字,都能在搜索时显示出来
searchableItemAttributesSet.title = title["key"]
//当搜索有图片时
let imagePathParts = "pic.png".componentsSeparatedByString(".")
searchableItemAttributesSet.thumbnailURL =NSBundle.mainBundle().URLForResource(imagePathParts[0], withExtension: imagePathParts[1])
searchableItemAttributesSet.contentDescription = content["content"]
// 要搜索的关键字数组
var keyWords = [String]()
//当多个关键字时可分隔开
let keys = title["key"]?.componentsSeparatedByString(", ")
for key in keys!{
keyWords.append(key);
}
searchableItemAttributesSet.keywords = keyWords
// 设置唯一的跳转路径uniqueIdentifier 一个app中不同搜索的分组domainIdentifier
let searchItem = CSSearchableItem(uniqueIdentifier: "com.app.SpotLight.\(i)", domainIdentifier:"Demo", attributeSet: searchableItemAttributesSet)
searchItems.append(searchItem)
CSSearchableIndex.defaultSearchableIndex().indexSearchableItems(searchItems, completionHandler: { (error) ->Void in
if error != nil{
print("i am sorry");
}
})
}
在viewController中实现,但是仅仅实现下面的方法还是不够的,还需要在AppDelegate中实现一个一个代理方法,每次点击在Spotlight上搜索的一个结果时,application:UIApplication, continueUserActivity userActivity:NSUserActivity, restorationHandler: ([AnyObject]?) -> Void) ->Bool
就会调用一次,我们现在的任务是调用我们上面已经实现的函数,传递userActivity对象,请看下面的实现代码
// NSUserActivity 负责存储app关闭时的状态便于之后app恢复到当前状态
override func restoreUserActivityState(activity:NSUserActivity) {
if activity.activityType ==CSSearchableItemActionType {
if let userInfo = activity.userInfo {
let selectedMovie = userInfo[CSSearchableItemActivityIdentifier]as! String
self.selectIndex =Int(selectedMovie.componentsSeparatedByString(".").last!)!
let nextVC = NextViewController()
nextVC.name = self.dataArr[self.selectIndex]["key"]!
self.navigationController?.pushViewController(nextVC, animated:false)
}
}
}
AppDelegate实现:
func application(application: UIApplication, continueUserActivity userActivity: NSUserActivity, restorationHandler: ([AnyObject]?) ->Void) -> Bool {
let idetifier = userActivity.userInfo!["kCSSearchableItemActivityIdentifier"];
print(idetifier)
let viewController = (self.window?.rootViewControlleras! UINavigationController).viewControllers[0]as! ViewController
viewController.restoreUserActivityState(userActivity)
return true
}
实现以上代码之后在SpotLight里搜索就可以跳转到搜索页面的详情页面了,这样更快速方便的让用户得到自己想要的数据。- Core SpotLight
- Spotlight
- Core Spotlight和深度链接结合使用(上)
- Core Spotlight和深度链接结合使用(下)
- 如何使用 iOS 9 的 Core Spotlight 框架
- iOS应用外搜索之 Core Spotlight 适配
- iOS开发之集成iOS9中的Core Spotlight Framework搜索App的内容
- iOS开发之集成iOS9中的Core Spotlight Framework搜索App的内容
- iOS开发之集成iOS9中的Core Spotlight Framework搜索App的内容
- Swift基础之如何使用iOS 9的Core Spotlight框架
- 禁用spotlight
- 禁用spotlight
- Developer spotlight:Danny Thorpe
- 焦点效应 spotlight effect
- How to disable Spotlight
- Spotlight on oracle 使用
- 如何关闭Spotlight
- Spotlight on oracle 使用
- 【Spark1.6官方翻译】Spark快速入门
- 数据结构实验之链表四:有序链表的归并
- Java设计模式(六)----适配器模式
- Java 获取http请求头内容
- C++ 类与对象
- Core SpotLight
- 使用Fiddler——模拟Http请求及对android应用抓包
- Git——Git相关基础知识
- linux下使用split 来分割大文件
- Oracle更新date型字段基本语句
- MySQL 5.6.24 for Windows 解压缩版配置安装
- ww控件实现某字段多个单选按钮操作
- 四轴项目总结之三--pid
- C++程序Windows移植Linux注意事项