使用iOS13.6.1、Swift5.2.4、XCode11.6、
按照Apple 的文档,我尝试从我的应用程序中删除特定的 Siri 快捷方式。
下面的代码首先展示了我是如何创建 Siri 快捷方式的。
下面的代码显示了我尝试从我的应用程序中再次删除相同的 Siri 快捷方式。
不幸的是,Siri 快捷方式并未从我的应用程序中删除。
知道如何永久删除它吗?
extension EditMediaViewController {
@available(iOS 12.0, *)
func createSiriButton(documentID: String, invitationCode: String) -> INUIAddVoiceShortcutButton {
let siriShortcutButton = INUIAddVoiceShortcutButton(style: .whiteOutline)
let activity = NSUserActivity(activityType: "MyIdentifierStringCodeABCDEF")
activity.title = "Test"
activity.suggestedInvocationPhrase = "Test"
activity.isEligibleForSearch = true
activity.isEligibleForPrediction = true
activity.persistentIdentifier = NSUserActivityPersistentIdentifier("MyIdentifierStringCodeABCDEF")
activity.becomeCurrent()
siriShortcutButton.shortcut = INShortcut(userActivity: activity)
return siriShortcutButton
}
}
extension EditMediaViewController: INUIAddVoiceShortcutButtonDelegate {
@available(iOS 12.0, *)
func present(_ addVoiceShortcutViewController: INUIAddVoiceShortcutViewController, for addVoiceShortcutButton: INUIAddVoiceShortcutButton) {
addVoiceShortcutViewController.delegate = self
addVoiceShortcutViewController.modalPresentationStyle = .formSheet
present(addVoiceShortcutViewController, animated: true, completion: nil)
}
@available(iOS 12.0, *)
func present(_ editVoiceShortcutViewController: INUIEditVoiceShortcutViewController, for addVoiceShortcutButton: INUIAddVoiceShortcutButton) {
editVoiceShortcutViewController.delegate = self
editVoiceShortcutViewController.modalPresentationStyle = .formSheet
present(editVoiceShortcutViewController, animated: true, completion: nil)
}
}
extension EditMediaViewController: INUIAddVoiceShortcutViewControllerDelegate {
@available(iOS 12.0, *)
func addVoiceShortcutViewController(_ controller: INUIAddVoiceShortcutViewController, didFinishWith voiceShortcut: INVoiceShortcut?, error: Error?) {
controller.dismiss(animated: true, completion: nil)
}
@available(iOS 12.0, *)
func addVoiceShortcutViewControllerDidCancel(_ controller: INUIAddVoiceShortcutViewController) {
controller.dismiss(animated: true, completion: nil)
}
}
extension EditMediaViewController: INUIEditVoiceShortcutViewControllerDelegate {
@available(iOS 12.0, *)
func editVoiceShortcutViewController(_ controller: INUIEditVoiceShortcutViewController, didUpdate voiceShortcut: INVoiceShortcut?, error: Error?) {
controller.dismiss(animated: true, completion: nil)
}
@available(iOS 12.0, *)
func editVoiceShortcutViewController(_ controller: INUIEditVoiceShortcutViewController, didDeleteVoiceShortcutWithIdentifier deletedVoiceShortcutIdentifier: UUID) {
controller.dismiss(animated: true, completion: nil)
}
@available(iOS 12.0, *)
func editVoiceShortcutViewControllerDidCancel(_ controller: INUIEditVoiceShortcutViewController) {
controller.dismiss(animated: true, completion: nil)
}
}
在这里我尝试删除相同的 Siri 快捷方式(但它不起作用 - 为什么?):
let activity = NSUserActivity(activityType: "MyIdentifierStringCodeABCDEF")
let activityPersistentIdentifier = NSUserActivityPersistentIdentifier("MyIdentifierStringCodeABCDEF")
NSUserActivity.deleteSavedUserActivities(withPersistentIdentifiers: [activityPersistentIdentifier]) {
print("one deleted")
}
INInteraction.deleteAll { (error) in
print("all deleted")
}
INInteraction.delete(with: [activityPersistentIdentifier]) { (error) in
print(error)
}