Search code examples
ioscocoa-touchairdrop

Is it Correct / Possible to Explicitly Dismiss a UIActivityViewController (after AirDrop)?


In the screen shot below, some data has been successfully sent via AirDrop. The UIActivityViewController updates to show "Sent" under the recipient and Cancel is changed to Done.

enter image description here

  • In this case, is it correct / good practice / possible, for the presenting view controller to dismiss the UIActivityViewController?
  • Or should that task be left to the user (and the Done button)?

From Apple's docs on UIActivityViewController:

Your app is responsible for configuring, presenting, and dismissing this view controller.

If the UIActivityViewController should be dismissed in the AirDrop case, should the completion handler be used for this purpose?

@property(nonatomic, copy) UIActivityViewControllerCompletionWithItemsHandler completionWithItemsHandler

Related questions:


Solution

  • After further consideration of the documentation, and the flow of events when using either a built-in activity (e.g. UIActivityTypeMail) or a 3rd-party UIActivity, I think the following...

    1) AirDrop is a special case, in that the user's interaction is performed within the UIActivityViewController itself. The UI in the UIActivityViewController updates accordingly, and the application should leave dismissing the UIActivityViewController to the user (and the Cancel / Done buttons).

    2) The completion handler is intended to be used after the UIActivityViewController has been dismissed...

    ...and not for dismissing the UIActivityViewController.

    To experiment, I added the following code:

    [controller setCompletionWithItemsHandler:^(NSString *activityType, BOOL completed, NSArray *returnedItems, NSError *activityError) {
        NSLog(@"completed: %@, \n%d, \n%@, \n%@,", activityType, completed, returnedItems, activityError);
    
    }];
    
    • In the AirDrop case I get the log statement after tapping Done and the UIActivityViewController is dismissed (by the user) - not after the AirDrop action completes (and "Sent" is shown).
    • If I use either a built-in activity (e.g. UIActivityTypeMail) or a custom, third party option, those actions (a) present additional UI and (b) result in the UIActivityViewController being dismissed, when the user finishes with that UI.

    In both cases, I get the log statement after the UIActivityViewController is gone.

    The documentation with regard to Accessing the Completion Handler is actually quite clear:

    The completion handler to execute after the activity view controller is dismissed.

    @property(nonatomic, copy) UIActivityViewControllerCompletionWithItemsHandler completionWithItemsHandler
    

    Discussion When the user-selected service finishes operating on the data, or when the user dismisses the view controller, the view controller executes this completion handler to let your app know the final result of the operation.

    I suppose it might have been more clear if this part:

    When the user-selected service finishes operating on the data...

    indicated that the user-selected service finishing would result in the dismissal of the view controller.

    3) The following, from Apple's documentation on UIActivityViewController is a little misleading:

    Your app is responsible for configuring, presenting, and dismissing this view controller.