试图强制 Handoff 功能在 iOS 9 设备上的 iPhone 5 和 iPad Air 上运行,并遇到我认为与 iOS9 相关的问题 - 无法在 iOS 8 上检查,因为我没有配备此系统的设备。
问题:
- 创建用户活动时,当主屏幕时,我在第一台设备上打开应用程序
- 在第二台设备上,我在锁定屏幕上看到有一些与我的应用相关的活动
- 我正在打开这个应用程序并且
application:continueUserActivity:restorationHandler:
没有在这个设备上调用,所以我无法向用户显示在第一台设备上呈现的相同内容。
您可以在 github 上浏览整个项目:https ://github.com/tomkowz/Quotes
实施细节:
我已经更新了目标的 plist 文件并添加了NSUserActivityTypes
.
<key>NSUserActivityTypes</key>
<array>
<string>com.tomaszszulc.Quotes.quotesList</string>
<string>com.tomaszszulc.Quotes.browseQuote</string>
</array>
接下来,我使用这些声明的类型创建了枚举:
enum ActivityType: String {
case BrowseQuote = "com.tomaszszulc.Quotes.browseQuote"
case QuotesList = "com.tomaszszulc.Quotes.quotesList"
}
当用户在主屏幕上时,我正在启动NSUserActivity
.
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
quotes = Quote.findAll(CoreDataStack.sharedInstance().mainContext)
tableView.reloadData()
startUserActivity()
}
override func viewWillDisappear(animated: Bool) {
super.viewWillDisappear(animated)
userActivity?.invalidate()
}
func startUserActivity() {
let activity = NSUserActivity(activityType: ActivityType.QuotesList.rawValue)
activity.title = "Viewing Quotes List"
userActivity = activity
userActivity?.becomeCurrent()
}
当用户转到所选项目的详细信息时,会激活第二种类型的活动。这是视图流:
UINavigationController -> (root) QuotesListViewController -> (push) QuoteDetailsViewController
以下是在中创建用户活动的代码QuoteDetailsViewController
:
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
userActivity = viewModel.userActivity // (viewModel) quote.userActivity
userActivity?.becomeCurrent()
}
override func viewDidDisappear(animated: Bool) {
super.viewDidDisappear(animated)
userActivity?.invalidate()
}
和viewModel.userActivity
电话:
extension Quote {
@available(iOS 8.0, *)
var userActivity: NSUserActivity {
let activity = NSUserActivity(activityType: ActivityType.BrowseQuote.rawValue)
activity.title = "Reading " + self.author + " quote"
activity.userInfo = [QuoteUserActivityKey.Identifier.rawValue: self.identifier]
// Core Spotlight support
if #available(iOS 9.0, *) {
activity.contentAttributeSet = self.searchableItemAttributeSet()
activity.keywords = Set([self.author])
activity.eligibleForSearch = true
activity.eligibleForHandoff = true
}
return activity
}
}
先感谢您!