【译】如何通过 INUIAddVoiceShortcutButtonDelegate 正确地使用 INUIAddVoiceShortcutButton

【译】如何通过 INUIAddVoiceShortcutButtonDelegate 正确地使用 INUIAddVoiceShortcutButton本文翻译自 Funkenstrahl 发布的文章 How to use INUIAddVoice correctly with INUIAddVoice 原文链接 funkenstrahl de blog 2018 0 Siri 捷径是 Apple 在 iOS 12

大家好,我是讯享网,很高兴认识大家。

本文翻译自 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 相应的代理方法会被调用,以允许用户设置自定义短语或者修改已有短语。

还有一件事不要忘了,就是实现 INUIAddVoiceShortcutViewControllerDelegateINUIEditVoiceShortcutViewControllerDelegate 这两个代理。以下是一个非常简化的实现版本,你可以根据你的需要来扩展它:

讯享网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 的人。当我最开始试着添加的时候,我找不到任何优秀的文档来告诉我如何去做,这也就是为什么我要写这篇小文章。

小讯
上一篇 2025-01-18 23:41
下一篇 2025-04-07 14:10

相关推荐

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容,请联系我们,一经查实,本站将立刻删除。
如需转载请保留出处:https://51itzy.com/kjqy/130244.html