How to check if a shortcut already exists?

Viewed 236

I have written this method to check if a shortcut already exists for the given id.

public static func shortcutExists(id: String, completion: @escaping ((Bool) -> Void)) {
        var exists = false
        INVoiceShortcutCenter.shared.getAllVoiceShortcuts { voiceShortcutsFromCenter, error in
            if let voiceShortcutsFromCenter = voiceShortcutsFromCenter {
                for voice in voiceShortcutsFromCenter {
                    if id == voice.shortcut.intent?.identifier {  // What to do here?
                        exists = true
                        break
                    }
                }
            } else {
                ...
            }
            completion(exists)
        }
    }

I have a unique id for each intent entry. But I don't know how to save that ID as intent's identifier. The identifier seems rather readonly and seems to be autogenerated.

How can I achieve this please?

1 Answers

How I achieve this is to add a unique property in each custom intent. Then check the intent's property after getAllVoiceShortcuts{}.

func checkExist(existShortcuts: [INVoiceShortcut]) -> Bool {
    existShortcuts.first(where: {
        let intent = $0.shortcut.intent
        return intent?.customPropertyId == self.customId
    }) != nil
}
Related