1
votes

I'm creating an app similar to THIS one

I'm working on adding siri shortcuts into my app to get quotes by categories just like this app. So far I've succeeded in creating NSUserActivity and donating shortcuts. My shortcuts are showing up in spotlight and they work just as expected ( In the overlay window for a shortcut without opening the app)

However, when I show the INUIAddVoiceShortcutViewController and use that to add that shortcut to the shortcuts app, it stops working in the overlay and opens my app instead

The app I'm trying to copy the feature from also has an option to edit their shortcut action which I can't get in my app.

Here is an example of how the app I'm trying to copy shows the sheet, notice the arrow in the 'Do' section

It looks like this when opened

But my app doesn't work like that even though I've set up the intentDefinition file

My app looks like this. I can't for the love of god get it to be editable. I've read the documentation, read through every tutorial on the web, gone through google's first page completely and still can't find a relevant updated code sample.

This is my IntentDefinition File

I'm donating all available shortcuts as soon as my app opens into the dashboard (I did that to test if I need to donate shortcuts before adding them to siri)

This is my code to generate a new user activity

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")
        }
    }
    
}

}

As soon as a category name is clicked, I add it to siri using this code

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)
}

If anyone can find any relevant tutorial for ios 14 or for siri shortcuts that isn't 2 years old, please send me a link. My donations work as expected from the spotlight, but they are generated as another interaction when I add them to siri and they stop working and open the app from that shortcut.

1

1 Answers

0
votes

I finally figured out the problem. The OP is actually my new ID which my company asked me to make so I'm answering from my original one, ( I won't mark my answer as correct as this is not for the reputation)

To get the edit function as I mentioned, you have to initialize the INUIAddVoiceShortcutViewController class with your custom intent

It has 2 initializers, 1 for the user activity and another one for a custom intent. Using the intent initializer solved this issue for me.

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)
        
    }

Hope it helps anyone with the same issue