这个教程需要你运行在Xcode7 和OSX 10.10系统或更后的系统。为了紧跟我的步伐,还需要你去GitHub上下载初始工程。
1.使用 NSUserActivity
在这个教程的开始部分,我将会给你展示怎样通过NSUserActivity类来索引一个APP里的内容。这个API也是在Handoff里使用的那一个,handoff是在去年iOS8中介绍的功能,它用于保存和复原一个应用的当前状态。
如果你之前没有使用过NSUserActivity,那么我建议你在开始这个教程之前先阅读我的这篇教程 ,它覆盖了Handoff和NSUserActivity的基础内容。
在开始写代码之前,打开初始工程,然后在iOS模拟器上或者测试机上运行APP。在这个阶段,你会看到这个APP简单地展示了一个有着4个TV节目的列表,以及每个节目的详细页面。


首先,打开工程然后到DetailViewController.swift文件。把DetailViewController类里configureView方法里的内容替换为如下:
func configureView() {
// Update the user intece for the detail item.
if self.nameLabel != nil && self.detailItem != nil {
self.nameLabel.text = detailItem.name
self.genreLabel.text = detailItem.genre
let dateFormatter = NSDateFormatter()
dateFormatter.timeStyle = .ShortStyle
self.timeLabel.text = dateFormatter.stringFromDate(detailItem.time)
let activity = NSUserActivity(activityType: "com.tutsplus.iOS-9-Search.displayShow")
activity.userInfo = ["name": detailItem.name, "genre": detailItem.genre, "time": detailItem.time]
activity.title = detailItem.name
var keywords = detailItem.name.componentsSeparatedByString(" ")
keywords.append(detailItem.genre)
activity.keywords = Set(keywords)
activity.eligibleForHandoff = false
activity.eligibleForSearch = true
//activity.eligibleForPublicIndexing = true
//activity.expirationDate = NSDate()
activity.becomeCurrent()
}
}
本文来自电脑杂谈,转载请注明本文网址:
http://www.pc-fly.com/a/tongxinshuyu/article-51781-1.html
但这是没有什么用的
跟谁打也不要跟英美打