Detect dismissal of UIActivityViewController

2020-03-14 03:15发布

问题:

I need to present a view, after UIActivityViewController will move to parent VC

...
UIActivityViewController *avvc = [[UIActivityViewController alloc] initWithActivityItems:shareItems applicationActivities:nil];
[self presentViewController:avvc animated:YES completion:nil];

回答1:

[avvc setCompletionHandler:^(NSString *activityType, BOOL completed) {
    NSLog(@"after dismiss");
    //Present another VC
}];

Hope this help you.



回答2:

Swift 3 Version

avvc.completionWithItemsHandler = { (activityType, completed:Bool, returnedItems:[Any]?, error: Error?) in
   if completed {
      // Do something 
   }
}


回答3:

Xamarin / C#

avvc.CompletionWithItemsHandler = MyCompletionWithItemsHandler;

// ...

void MyCompletionWithItemsHandler(NSString activityType, bool completed, NSExtensionItem[] returnedItems, NSError error)
{
    if (completed)
    {
        // Did not tap Cancel
    } 
    else
    {
        // Cancel was tapped
    }
}


回答4:

I just want to clarify that the boolean value completed represents the completion state of the individual UIActivity. If the UIActivityViewController is dismissed without any action, the value for activityType will be nil and the value of completed will be false.

[avvc setCompletionWithItemsHandler:^(UIActivityType _Nullable activityType, BOOL completed,
                                      NSArray * _Nullable returnedItems,
                                      NSError * _Nullable activityError) {

    if (activityType == nil)    {
        NSLog(@"UIActivityViewController dismissed without any action.");
    } else {
        NSLog(@"completionWithItemsHandler, activityType: %@, completed: %d, returnedItems: %@, activityError: %@",
         activityType, completed, returnedItems, activityError);
    }
}];