具有多个快捷方式(NSUserActivity)时,siri快捷方式按钮(INUIAddVoiceShortcutButton)显示错误的标题 [英] siri shortcut button (INUIAddVoiceShortcutButton) shows wrong title when have multiple shortcuts (NSUserActivity)

查看:438
本文介绍了具有多个快捷方式(NSUserActivity)时,siri快捷方式按钮(INUIAddVoiceShortcutButton)显示错误的标题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序中有2个siri快捷方式. 我使用NSUserActivity捐赠了这些快捷方式.我还在info.plist中创建了2个NSUserActivityTypes.

I've 2 siri shortcuts in my App. I use NSUserActivity to donate these shortcuts. I've also created 2 NSUserActivityTypes in my info.plist.

有2个用于处理这些快捷方式的视图控制器(1个视图控制器对应1个快捷方式).

There are 2 view controllers which handle these shortcuts (1 view controller for 1 shortcut).

如果我从1个视图控制器添加1个siri快捷方式,然后转到第2个视图控制器,则第2个视图控制器上的本机siri快捷方式按钮(INUIAddVoiceShortcutButton)会自动选择第一个快捷方式(从第一个视图控制器创建),并显示已添加到Siri",而不显示添加到Siri"按钮.我再次检查了每个NSUserActivity是否具有不同的标识符,但仍然以某种方式选择了错误的快捷方式.

If I add 1 siri shortcut from 1 view controller and then go to 2nd view controller the native siri shortcut button (INUIAddVoiceShortcutButton) on 2nd view controller automatically picks the first shortcut (created from 1st view controller) and shows "Added to Siri" with suggested phrase instead of showing "Add to Siri" button. I double checked that each NSUserActivity has different identifier but still somehow its picks the wrong shortcut.

View Controller 1:

View Controller 1:

let userActivity = NSUserActivity(activityType: "com.activity.type1")
userActivity.isEligibleForSearch = true
userActivity.isEligibleForPrediction = true
userActivity.title = shortcut.title
userActivity.suggestedInvocationPhrase = suggestedPhrase

let attributes = CSSearchableItemAttributeSet(itemContentType: kUTTypeItem as String)
attributes.contentDescription = description
userActivity.contentAttributeSet = attributes
let shortcut = INShortcut(userActivity: userActivity)
let siriButton = INUIAddVoiceShortcutButton(style: .whiteOutline)
siriButton.translatesAutoresizingMaskIntoConstraints = false
siriButton.shortcut = shortcut
self.view.addSubview(siriButton)

View Controller 2:

View Controller 2:

let userActivity2 = NSUserActivity(activityType: "com.activity.type2")
userActivity2.isEligibleForSearch = true
userActivity2.isEligibleForPrediction = true
userActivity2.title = shortcut.title
userActivity2.suggestedInvocationPhrase = suggestedPhrase

let attributes = CSSearchableItemAttributeSet(itemContentType: kUTTypeItem as String)
attributes.contentDescription = description
userActivity2.contentAttributeSet = attributes

let shortcut = INShortcut(userActivity: userActivity2)
let siriButton = INUIAddVoiceShortcutButton(style: .whiteOutline)
siriButton.translatesAutoresizingMaskIntoConstraints = false
siriButton.shortcut = shortcut
self.view.addSubview(siriButton)

当我删除该应用程序并重新安装而不删除手机设置应用程序中的快捷方式时,也会发生类似的事情.

A similar thing happens when I delete the App and reinstall without deleting the shortcuts from Phone's Settings App.

推荐答案

似乎是一个IOS错误.我想出了解决此问题的方法.每次用户添加/编辑siri快捷方式时,您都必须创建一个新的siri按钮.在创建siri按钮之前,请执行以下操作

Seems like its an IOS bug. I figured out a workaround for this problem. You have to create a new siri button every time the user add/edit the siri shortcut. Before creating siri button do the following things

1-通过调用函数从INVoiceShortcutCenter获取所有语音快捷方式.请注意,这是异步发生的,因此您需要花一些时间才能获取数据(例如,在AppDelegate中).每当用户添加Siri快捷方式(可能在INUIAddVoiceShortcutViewControllerDelegate.addVoiceShortcutViewController(_:didFinishWith:error)方法中)时,您还需要重新加载它.

1- Get all the voice shortcuts from INVoiceShortcutCenter by calling the function. Note that this happens asynchronously, so you need to do it some time before you need the data (e.g. in your AppDelegate). You'll also need to re-load this whenever the user adds a Siri Shortcut (probably in the INUIAddVoiceShortcutViewControllerDelegate.addVoiceShortcutViewController(_:didFinishWith:error) method).

INVoiceShortcutCenter.shared.getAllVoiceShortcuts  { (voiceShortcutsFromCenter, error) in
    guard let voiceShortcutsFromCenter = voiceShortcutsFromCenter else {
            if let error = error as NSError? {
                os_log("Failed to fetch voice shortcuts with error: %@", log: OSLog.default, type: .error, error)
            }
            return
        }
        self.voiceShortcuts = voiceShortcutsFromCenter
}

2-在View Controller-1中,通过迭代所有语音快捷方式来检查快捷方式是否已添加

2- In View Controller-1 check if the shortcut is already added or not by iterating all the voice shortcuts

let voiceShorcut = voiceShortcuts.first { (voiceShortcut) -> Bool in
    if let activity = voiceShortcut.shortcut.userActivity, activity.activityType == "com.activity.type1" {
        return true
    }
    return false
}

3-如果您的语音快捷方式已注册,则将INShortcut传递给siri按钮,否则请勿进行设置.

3- If your voice shortcut is registered then pass the INShortcut to siri button otherwise don't set it.

if voiceShorcut != nil {
    let shortcut = INShortcut(userActivity: userActivity1)
    siriButton.shortcut = shortcut
} 

在Second View Controller中执行相同的操作.

Do the same thing in Second View Controller.

这篇关于具有多个快捷方式(NSUserActivity)时,siri快捷方式按钮(INUIAddVoiceShortcutButton)显示错误的标题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆