iOS9 Spotlight搜索App内部内容(比如:联系人...)

jeees 2015-12-24 11:05:04
加精
介绍
在WWDC 2015会议上,苹果官方公布了iOS9。除开许多新的特性和增强功能,这次升级也给了开发者们一个机会让他们的app里的内容能通过Spotlight 搜索功能被发现和使用。在iOS9中可用的新APIs允许你去索引APP里面的内容或者界面状态,通过Spotlight来让用户使用。 这些新的搜索APIs的三大组件为:

* NSUserActivity 类, 它是为可被看见的APP内容而设计的


* Core Spotlight 框架, 为任何APP内容而设计的 


* web markup,为这一类型的APP设计的,就是APP的内容在某个网站上有镜像


在这个教程里,我将会向你展示可以怎样在你的应用中使用NSUserActivity类以及 Core Spotlight 框架。

准备工作
这个教程需要你运行在Xcode7 和OSX 10.10、iOS9.0系统或更后的系统

步骤
 #import <CoreSpotlight/CoreSpotlight.h>


2.创建搜索属性对象
CSSearchableItemAttributeSet * attributeSet = [[CSSearchableItemAttributeSet alloc] initWithItemContentType:@""];


3.设置搜索属性
//搜索显示的名称
attributeSet.title = obj.name;
//显示的描述
attributeSet.contentDescription = obj.desc;
//搜索关键字
attributeSet.keywords = @[obj.name,@"CX"];
//显示的图标
UIImage * icon = [UIImage imageNamed:obj.imageName];
if (icon) {
attributeSet.thumbnailData = UIImageJPEGRepresentation(icon, 1);
}


4.根据搜索属性创建搜索对象(domainIdentifier:唯一标识)
CSSearchableItem * item = [[CSSearchableItem alloc] initWithUniqueIdentifier:obj.name domainIdentifier:SearchDomain attributeSet:attributeSet];


5.将搜索对象添加到搜索数组
[searchItems addObject:item];


6.设置索引目录
 CSSearchableIndex * searchableIndex = [CSSearchableIndex defaultSearchableIndex];
[searchableIndex indexSearchableItems:searchItems completionHandler:^(NSError * _Nullable error) {
if (error != nil) {//添加索引失败
NSLog(@"%@",[error localizedDescription]);
}else{//成功
NSLog(@"indexing successful");
}
}];


7.实现AppDelegate方法(用户通过spotlight搜索到APP里面的内容 点击内容进入APP 就会调用这个方法)
- (BOOL)application:(UIApplication *)application continueUserActivity:(NSUserActivity *)userActivity restorationHandler:(void (^)(NSArray * _Nullable))restorationHandler{
UINavigationController * vc = (UINavigationController *)self.window.rootViewController;

[vc.topViewController restoreUserActivityState:userActivity];

return YES;
}


8.在搜索列表控制器实现方法(activity里面有用户点击spotlight搜索列表中某条数据的所有属性 根据属性做相应的操作)
- (void)restoreUserActivityState:(NSUserActivity *)activity{}


代码地址
...全文
2025 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
i_蕊 2016-05-16
  • 打赏
  • 举报
回复
感谢楼主的分享~~
kissney 2015-12-24
  • 打赏
  • 举报
回复
感谢分享 很好
  • 打赏
  • 举报
回复
不错不错,感谢分享
lilb666 2015-12-24
  • 打赏
  • 举报
回复
谢谢分享 谢谢分享谢谢分享
qq_33496225 2015-12-24
  • 打赏
  • 举报
回复
很好,很强大
cattpon 2015-12-24
  • 打赏
  • 举报
回复
研究一下怎样~
qq_33490975 2015-12-24
  • 打赏
  • 举报
回复
看看,不错。
qq_33495906 2015-12-24
  • 打赏
  • 举报
回复
看看,不错。
wenliang0785 2015-12-24
  • 打赏
  • 举报
回复
学习一下,谢谢分享
qq_33494793 2015-12-24
  • 打赏
  • 举报
回复
谢谢了
qq_33494341 2015-12-24
  • 打赏
  • 举报
回复
有个小bug需要修改下不然不完美
Core Data is intricate, powerful, and necessary. Discover the powerful capabilities integrated into Core Data, and how to use Core Data in your iOS and OS X projects. All examples are current for OS X El Capitan, iOS 9, and the latest release of Core Data. All the code is written in Swift, including numerous examples of how best to integrate Core Data with Apple's newest programming language. Core Data expert Marcus Zarra walks you through a fully developed application based around the Core Data APIs. You'll build on this application throughout the book, learning key Core Data elements such as NSPredicate, NSFetchRequest, thread management, and memory management. Start with the basics of Core Data and learn how to use it to develop your application. Then delve deep into the API details. Explore how to get Core Data integrated into your application properly, and work with this flexible API to create convenience methods to improve your application's maintainability. Reduce your migration difficulties, integrate your Core Data app with iCloud and Watch Kit, and use Core Data in a queue-based environment. By the end of the book, you'll have built a full-featured application, gained a complete understanding of Core Data, and learned how to integrate your application into the iPhone/iPad platform. This book is based on Core Data in Objective-C, Third Edition. It focuses on Swift and adds an additional chapter on how to integrate Core Data with an efficient network implementation, with best practices on how to load and pre-load data into your Swift application. What You Need: Mac OS X El Capitan and iOS 9 and a basic working knowledge of Swift Table of Contents Chapter 1. Before We Begin Chapter 2. Under the Hood Chapter 3. iOS: NSFetchedResultsController Chapter 4. Versioning and Migration Chapter 5. Performance Tuning Chapter 6. Threading Chapter 7. Bulk Changes Chapter 8. Network Operations Chapter 9. Using Core Data with iCloud Chapter 10. Adding a Desktop Foundation Chapter 11. Bindings, KVC, and KVO Chapter 12. Spotlight, Quick Look, and Core Data

29,027

社区成员

发帖
与我相关
我的任务
社区描述
主要讨论与iOS相关的软件和技术
社区管理员
  • iOS
  • 大熊猫侯佩
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧