Shortcut Donations Not Working

I am implementing a couple custom intents in my app. Having completed the implementation of the custom intent definitions and the intent handlers which presumably should be called on voice command based on the suggestedInovocationPhrase, I am getting stumped with just getting IOS to recognize my shortcut donation. While the shortcut donation is executed multiple times from the view that I am trying to create the shortcut for, it is never being displayed in the shortcut app or on my lock screen as having been donated. In my settings App, I have turned on (under Developer) "Display Recent Shortcuts" and "Display Donations on Lock Screen". Here is the code that is executing each time I think I am making a donation:

- (NSUserActivity *) CreateMyShortcut {

    NSUserActivity *newActivity = [[NSUserActivity alloc] initWithActivityType: kMyShortCutActivityType];

    

    if (@available(iOS 12.0, *)) {

        newActivity.persistentIdentifier = kMyShortCutActivityType;

        newActivity.eligibleForSearch = TRUE;

        newActivity.eligibleForPrediction = TRUE;

        

        CSSearchableItemAttributeSet *attributeSet = [[CSSearchableItemAttributeSet alloc] initWithContentType: UTTypeImage];

        

        newActivity.title = @"My Shortcut";

        attributeSet.contentDescription = @"description";

        

        newActivity.suggestedInvocationPhrase = @"Create Widget";

        

        UIImage *image = [UIImage imageNamed:@"MyApp_Icon.jpg"];

        NSData *imageData = [NSData dataWithData:UIImagePNGRepresentation(image)];

        attributeSet.thumbnailData = imageData;

        

        newActivity.contentAttributeSet = attributeSet;

    }

    return newActivity;

}

This is called from the view that I want to create the shortcut for with the following code:

    // Donate a shortcut to allow Siri to assist with creating a parts list

    NSUserActivity *activity = [[MyDonationManager sharedInstance] CreateMyShortcut];

    NSLog(@"Donating Siri Shortcut");

    [activity becomeCurrent];

Is there something else I need to do?

Answered by jeffb6688 in 690305022

I found that there is something that I did not see in any documentation. Perhaps this is just for objective-C, but it fixes the problem. The solution is to assign your NSUserActivity to the view controller’s built-in userActivity property. The view controller automatically invokes becomeCurrent and resignCurrent for us. At first I tried doing this manually but that does not seem to work. The Apple documentation seems to suggest that you should manually invoke [activity becomeCurrent]. But the sample code that @edford pointed to did not manually invoke [activity becomeCurrent]. This led me to find an objective C implementation (I can't link to the article, but search for Integrating iOS 12 Siri Shortcuts using Objective-C - PQVST) of Siri Shortcuts that noted that it is important to use the view controller's built-in userActivity property.

Can you run the sample code project to see if the donations from that sample app show up for you? If so, compare your implementation here with that of the sample code project.

Accepted Answer

I found that there is something that I did not see in any documentation. Perhaps this is just for objective-C, but it fixes the problem. The solution is to assign your NSUserActivity to the view controller’s built-in userActivity property. The view controller automatically invokes becomeCurrent and resignCurrent for us. At first I tried doing this manually but that does not seem to work. The Apple documentation seems to suggest that you should manually invoke [activity becomeCurrent]. But the sample code that @edford pointed to did not manually invoke [activity becomeCurrent]. This led me to find an objective C implementation (I can't link to the article, but search for Integrating iOS 12 Siri Shortcuts using Objective-C - PQVST) of Siri Shortcuts that noted that it is important to use the view controller's built-in userActivity property.

Shortcut Donations Not Working
 
 
Q