首页 > 解决方案 > 无法在 iOS 14.6 中使用 siri 快捷方式

问题描述

我正在创建一个类似于这个的应用程序

我正在努力将 siri 快捷方式添加到我的应用程序中,以按类别获取报价,就像这个应用程序一样。到目前为止,我已经成功地创建了 NSUserActivity 并捐赠了快捷方式。我的快捷方式出现在聚光灯下,它们按预期工作(在不打开应用程序的快捷方式的覆盖窗口中)

但是,当我显示 INUIAddVoiceShortcutViewController 并使用它将该快捷方式添加到快捷方式应用程序时,它会停止在叠加层中工作并打开我的应用程序

我试图从中复制功能的应用程序也有一个选项来编辑我无法在我的应用程序中获得的快捷操作。

是我尝试复制的应用程序如何显示工作表的示例,请注意“执行”部分中的箭头

打开后是这个样子

但是即使我设置了 intentDefinition 文件,我的应用程序也不能那样工作

我的应用看起来像这样。看在上帝的份上,我不能让它成为可编辑的。我已经阅读了文档,阅读了网络上的每个教程,完全浏览了谷歌的第一页,但仍然找不到相关的更新代码示例。

是我的 IntentDefinition 文件

一旦我的应用程序在仪表板中打开,我就会捐赠所有可用的快捷方式(我这样做是为了测试在将快捷方式添加到 siri 之前是否需要捐赠)

这是我生成新用户活动的代码

public let kNewArticleActivityType = "\(bundleID).GetQuotes"

class Shortcuts {

public static func newUserActivity(category: Categories, thumbnail: UIImage? = UIImage(named: "icon")) -> NSUserActivity {
    
    let categoryString = category.getStringValue().capitalized
    
    let invocationPhrase = "\(categoryString) quote"
    
    let attributes = CSSearchableItemAttributeSet(itemContentType: kUTTypeItem as String)
    
    attributes.contentDescription = "Get quote from the \(categoryString) category"
    attributes.thumbnailData = thumbnail?.jpegData(compressionQuality: 1.0)
    attributes.keywords = ["quote", "motivation", "positive", "vibrations"]
    
    let activity = NSUserActivity(activityType: kNewArticleActivityType)
    activity.persistentIdentifier = NSUserActivityPersistentIdentifier(kNewArticleActivityType)
    activity.isEligibleForSearch = true
    activity.isEligibleForPrediction = true
    activity.suggestedInvocationPhrase = invocationPhrase
    activity.title = "Get a quote"
    activity.userInfo = ["category": categoryString]
    activity.contentAttributeSet = attributes
    
    return activity
}

static func donateInteraction(with category: Categories) {
    
    let categoryString = category.getStringValue().capitalized
    
    let phrase = "\(categoryString) quote"
    
    let intent = GetQuotesIntent()
    
    intent.suggestedInvocationPhrase = phrase
    intent.category = category
    
    let interaction = INInteraction(intent: intent, response: nil)
    
    interaction.donate { (error) in
        if let error = error {
            print("Interaction donation failed because \(error.localizedDescription)")
        } else {
            print("Successfully donated interaction")
        }
    }
    
}

}

单击类别名称后,我使用此代码将其添加到 siri

func addToSiriClicked(category: String) {
    let categoryEnum = Categories.getEnum(from: category)
    let activity = Shortcuts.newUserActivity(category: categoryEnum)
    self.userActivity = activity
    
    let shortcut = INShortcut(userActivity: activity)
    let viewController = INUIAddVoiceShortcutViewController(shortcut: shortcut)
    viewController.modalPresentationStyle = .formSheet
    viewController.delegate = self
    self.present(viewController, animated: true, completion: nil)
}

如果有人能找到任何适用于 ios 14 或 2 岁以下的 siri 快捷方式的相关教程,请给我一个链接。我的捐款在聚光灯下按预期工作,但是当我将它们添加到 siri 并且它们停止工作并从该快捷方式打开应用程序时,它们是作为另一种交互生成的。

标签: iosswiftios14sirishortcutsnsuseractivity

解决方案


我终于弄清楚了问题所在。OP 实际上是我的公司要求我制作的新 ID,所以我从原来的 ID 中回答,(我不会将我的答案标记为正确,因为这不是为了声誉)

要获得我提到的编辑功能,您必须使用您的自定义意图初始化 INUIAddVoiceShortcutViewController 类

它有 2 个初始化器,1 个用于用户活动,另一个用于自定义意图。使用意图初始化程序为我解决了这个问题。

let shortcut = INShortcut(intent: Shortcuts.newIntent(category: categoryEnum))
    
    if let shortcut = shortcut {
        
        INVoiceShortcutCenter.shared.setShortcutSuggestions([shortcut])
        
        let viewController = INUIAddVoiceShortcutViewController(shortcut: shortcut)
        viewController.modalPresentationStyle = .formSheet
        viewController.delegate = self
        self.present(viewController, animated: true, completion: nil)
        
    }

希望它可以帮助任何有同样问题的人


推荐阅读