Mac Catalyst: UIActivityViewController's completionWithItemsHandler block is never called when a system provided UIActivity is selected.

According to the UIActivityViewController documentation for UIActivityViewController's completionWithItemsHandler:

Upon the completion of an activity, or the dismissal of the activity view controller, the view controller’s completion block is executed. You can use this block to execute any final code related to the service.

However when invoking the "Messages" or "Mail" actions in the UIActivityViewController this block is never called. I'm presenting the UIActivityViewController in a popover. Simple to reproduce. Just do this on Mac Catalyst in a table view delegate (sorry for the poor code formatting but it is hard to format code well on these forums).

-(UISwipeActionsConfiguration*)tableView:(UITableView *)tableView leadingSwipeActionsConfigurationForRowAtIndexPath:(nonnull NSIndexPath *)indexPath

{
    UIContextualAction *shareAction = [UIContextualAction contextualActionWithStyle:UIContextualActionStyleNormal
                    title:@"Share"
                  handler:^(UIContextualAction *action,
                                                                                      UIView * _Nonnull sourceView,
                                                                                      void (^_Nonnull completionHandler)(BOOL))
    {
        [self showPopoverWithSourceItem:sourceView completionWithItemsHandler:^(UIActivityType  _Nullable activityType,
                                                                                BOOL completed,
                                                                                NSArray * _Nullable returnedItems,
                                                                                NSError * _Nullable activityError) {

            //This block isn't called when Messages/Mail activities are selected. Probably other too but that's all I tested.
            completionHandler(completed); //Need to call the UIContextualAction's completionHandler here to close up the swipe actions. 
        }];
    }];

    shareAction.image = [UIImage systemImageNamed:@"square.and.arrow.up.fill"];
    UISwipeActionsConfiguration *config = [UISwipeActionsConfiguration configurationWithActions:@[shareAction]];

    return config;

}

-(void)showPopoverWithSourceItem:(id<UIPopoverPresentationControllerSourceItem>)sourceItem

      completionWithItemsHandler:(UIActivityViewControllerCompletionWithItemsHandler)handler

{

    NSURL *shareLink = [NSURL URLWithString:@"https://www.apple.com"];

    

    UIActivityViewController *activityViewController = [[UIActivityViewController alloc]initWithActivityItems:@[shareLink]

                                                                                        applicationActivities:nil];

    

    activityViewController.completionWithItemsHandler = handler;

       

    activityViewController.modalPresentationStyle = UIModalPresentationPopover;

    UIPopoverPresentationController *presentationController;

    presentationController = (UIPopoverPresentationController*)activityViewController.presentationController;

    presentationController.sourceItem = sourceItem;

    presentationController.permittedArrowDirections = UIPopoverArrowDirectionAny;

    [self presentViewController:activityViewController animated:YES completion:nil];

}

Run that and choose "Mail" or "Messages" in the activity view controller. The table view remains swiped after the activity is invoked and the popover is dismissed. Now if you click outside the popover without invoking an activity the popover dismisses and the completionWithItemsHandler is called.

Related but different UIActivityViewController/UIActivity bug I reported here yesterday: https://developer.apple.com/forums/thread/722003

I will file a bug on this and post the number here soon.

Can I please work a full day without running into a system bug in this framework....just one day..please.

I wouldn't be so disgruntled about all this if my obvious bug reports actually got fixed in a reasonable amount of time but I've been a developer long enough to know that I'm lucky if they get fixed for macOS 14, if ever.

FB11889554

Mac Catalyst: UIActivityViewController's completionWithItemsHandler block is never called when a system provided UIActivity is selected.
 
 
Q