前言
感觉 spotlight 这个功能还是蛮有用的,能提升用户活跃,增加应用内容曝光几率。
正文
一、实现(ios 9.0)
1.1 添加索引
var searchableitems = [cssearchableitem]() for app in apps { let searchableitemattributeset = cssearchableitemattributeset(itemcontenttype: kuttypetext as string) searchableitemattributeset.title = app.label searchableitemattributeset.contentdescription = "watch \(app.label) live on shou" let searchableitem = cssearchableitem(uniqueidentifier: app.id, domainidentifier: "youappbundlerid.apps", attributeset: searchableitemattributeset) searchableitems.append(searchableitem) } cssearchableindex.defaultsearchableindex().indexsearchableitems(searchableitems, completionhandler: { (error: nserror?) in if error == nil { print("initspotlight...completed") } else { print("\(error)") } })
一定要注意把上面代码加到子线程里面执行,略慢。可以在 appdelegate 里面从服务器请求完数据再加索引的。
1.2 接收点击事件
@available(ios 8.0, *) func application(application: uiapplication, continueuseractivity useractivity: nsuseractivity, restorationhandler: ([anyobject]?) -> void) -> bool { if #available(ios 9.0, *) { if useractivity.activitytype == cssearchableitemactiontype { if let uniqueidentifier = useractivity.userinfo?[cssearchableitemactivityidentifier] as? string, let label = useractivity.title { // 页面跳转 } } } return true }
二、额外经验
2.1 cssearchableitemattributeset
可以不设置 thumbnaildata ,这样默认就是当前应用的 logo;另外注意 thumbnailurl 并不是用来指定网络图片地址的 - - #,所以你要加缩略图的话需要先把网上的下载到本地再设置
2.2 cssearchableitem
默认失效是一个月,另外发现无法携带更多数据(比如通过 cssearchableitemattributeset 的 setvalue ,在 useractivity 那边都接收不到)
2.3 没有找到索引条数的限制
http://stackoverflow.com/questions/35284223/is-there-a-limit-for-number-of-items-cssearchableitem-in-core-spotlight-cssear
我发现添加索引还是挺慢的,另外那个批量索引没弄懂,感觉应该是又有删除又有新增的话适合批量处理。
2.4 没用找到重新索引的方法
重复调用也没用问题,只要 uniqueidentifier 匹配会覆盖老的数据
2.5 没有找到提升排名的方法
好像系统会有一套算法来自动提升排名。
总结:以上就是 ios 开发 core spotlight 实例应用,以及经验分享,希望能帮助开发ios的同学。
如对本文有疑问, 点击进行留言回复!!
Codeforces Round #657 (Div. 2) C题
hdu2222 Keywords Search(ac自动机模板-kuangbin)
网友评论