尝试使用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
}
}
提前谢谢!
答案 0 :(得分:1)
如果你在第二台设备上看到它正在看到潜在的切换,那么application:continueUserActivity:restorationHandler:
未被调用的唯一“好”原因是:
如果以下任一应用程序:willFinishLaunchingWithOptions:或application:didFinishLaunchingWithOptions:不返回NO。
切换“不起作用”的另一个原因是设备未登录到同一个iCloud帐户。但你看到了指标,所以不是你的情况。