如何通過 INUIAddVoiceShortcutButtonDelegate 正確地使用 INUIAddVoiceShortcutButton
本文翻譯自 Funkenstrahlen 釋出的文章《How to use INUIAddVoiceShortcutButton correctly with INUIAddVoiceShortcutButtonDelegate》,原文連結:funkenstrahlen.de/blog/2018/0…
Siri 捷徑是 Apple 在 iOS 12 上重點推介的一項功能。它允許使用者通過給 Siri 新增自定義短語來執行由 app 提供的特定動作。app 可以通過多種方式向用戶提供這個捷徑的入口,其中很重要的一種就是在應用內展示一個按鈕 。
你可以自行設計一個定製化的按鈕,但是 Apple 更鼓勵你使用官方的INUIAddVoiceShortcutButton
。樣式也可以設定(黑或白),而且如果使用得當的話,你會發現
官方的INUIAddVoiceShortcutButton
文件
提供了一些示例程式碼:
// Add an "Add to Siri" button to a view. func addSiriButton(to view: UIView) { let button = INUIAddVoiceShortcutButton(style: .blackOutline) button.translatesAutoresizingMaskIntoConstraints = false view.addSubview(button) view.centerXAnchor.constraint(equalTo: button.centerXAnchor).isActive = true view.centerYAnchor.constraint(equalTo: button.centerYAnchor).isActive = true button.addTarget(self, action: #selector(addToSiri(_:)), for: .touchUpInside) } // Present the Add Shortcut view controller after the // user taps the "Add to Siri" button. @objc func addToSiri(_ sender: Any) { if let shortcut = INShortcut(intent: orderSoupOfTheDayIntent) { let viewController = INUIAddVoiceShortcutViewController(shortcut: shortcut) viewController.modalPresentationStyle = .formSheet viewController.delegate = self // Object conforming to `INUIAddVoiceShortcutViewControllerDelegate`. present(viewController, animated: true, completion: nil) } } 複製程式碼
然而這份示例程式碼並沒有實現一個好的使用者體驗!
- 當捷徑已經被使用者新增過時,按鈕沒有自動更新UI(變成“Added to Siri”)
- 點選按鈕無法對已有的捷徑進行編輯
關於這裡有太多可以優化的地方,但不幸的是,Apple 並沒有在文件中說明這一情況。
要實現更好的使用者體驗,關鍵就在於
INUIAddVoiceShortcutButtonDelegate
。它可以讓你根據使用者是否已經建立過捷徑來決定應該展示INUIAddVoiceShortcutViewController
還是INUIEditVoiceShortcutViewController
。
代理方法可以這樣實現:
extension MyViewController: INUIAddVoiceShortcutButtonDelegate { func present(_ addVoiceShortcutViewController: INUIAddVoiceShortcutViewController, for addVoiceShortcutButton: INUIAddVoiceShortcutButton) { addVoiceShortcutViewController.delegate = self addVoiceShortcutViewController.modalPresentationStyle = .formSheet present(addVoiceShortcutViewController, animated: true, completion: nil) } func present(_ editVoiceShortcutViewController: INUIEditVoiceShortcutViewController, for addVoiceShortcutButton: INUIAddVoiceShortcutButton) { editVoiceShortcutViewController.delegate = self editVoiceShortcutViewController.modalPresentationStyle = .formSheet present(editVoiceShortcutViewController, animated: true, completion: nil) } } 複製程式碼
別忘了指定INUIAddVoiceShortcutButtonDelegate
:
let button = INUIAddVoiceShortcutButton(style: .black) button.translatesAutoresizingMaskIntoConstraints = false button.shortcut = INShortcut(intent: WarningLevelIntent())! button.delegate = self // then add the button as a subview and create constraints to place it correctly 複製程式碼
你還需要把你要設定的捷徑賦值給button.shortcut
,這樣就可以使按鈕在已有捷徑的場景下自動更新UI了。當按鈕被點選時,INUIAddVoiceShortcutButtonDelegate
相應的代理方法會被呼叫,以允許使用者設定自定義短語或者修改已有短語。
還有一件事不要忘了,就是實現INUIAddVoiceShortcutViewControllerDelegate
和INUIEditVoiceShortcutViewControllerDelegate
這兩個代理。以下是一個非常簡化的實現版本,你可以根據你的需要來擴充套件它:
extension MyViewController: INUIAddVoiceShortcutViewControllerDelegate { func addVoiceShortcutViewController(_ controller: INUIAddVoiceShortcutViewController, didFinishWith voiceShortcut: INVoiceShortcut?, error: Error?) { controller.dismiss(animated: true, completion: nil) } func addVoiceShortcutViewControllerDidCancel(_ controller: INUIAddVoiceShortcutViewController) { controller.dismiss(animated: true, completion: nil) } } extension MyViewController: INUIEditVoiceShortcutViewControllerDelegate { func editVoiceShortcutViewController(_ controller: INUIEditVoiceShortcutViewController, didUpdate voiceShortcut: INVoiceShortcut?, error: Error?) { controller.dismiss(animated: true, completion: nil) } func editVoiceShortcutViewController(_ controller: INUIEditVoiceShortcutViewController, didDeleteVoiceShortcutWithIdentifier deletedVoiceShortcutIdentifier: UUID) { controller.dismiss(animated: true, completion: nil) } func editVoiceShortcutViewControllerDidCancel(_ controller: INUIEditVoiceShortcutViewController) { controller.dismiss(animated: true, completion: nil) } } 複製程式碼
希望這篇文章可以真正幫助到那些想在 app 中新增INUIAddVoiceShortcutButton
的人。當我最開始試著新增的時候,我找不到任何優秀的文件來告訴我如何去做,這也就是為什麼我要寫這篇小文章。