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?

Accepted Reply

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.

  • The sample code project I suggested does exactly this, and there is a comment about using the property provided by UIViewController in LocationViewController on line 72. Was that comment not helpful or clear enough?

Add a Comment

Replies

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.

  • Thanks for your suggestion, but I am still stumped. It's a little difficult to compare since my code is objective C, but as near as I can tell my code is functionally the same. I have a table view controller in which I am creating a NSUserActivity similar to the sample code project. What surprises me about the sample code project is that there is no where in the code where a call is made to activity.becomeCurrent(). My read on the documentation (even the README in the sample code project is that this is necessary to register the activity with the system.

    Is there more to this than the code (from sample code project) private func configureUserActivity() of the LocationViewController? This is what I have implemented. This is what the tutorials all suggest is all you need to do. But there must be something else that I am missing.

    Is there something in the build configuration or settings that I need to activate? I am also including NSUserActivityTypes in my info.plist that correspond to the 2 activities I am trying to create shortcuts for. Is there something else needed in the info.plist?

    Any suggestions would be appreciated.

Add a Comment

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.

  • The sample code project I suggested does exactly this, and there is a comment about using the property provided by UIViewController in LocationViewController on line 72. Was that comment not helpful or clear enough?

Add a Comment