UIActivityViewControllerCompletionHandler How to?

2019-03-10 21:22发布

I am trying to use the UIActivityViewControllerCompletionHandler , but i dont quite get how..

I want to detect when the user finishes or dismisses this view controller (UIActivityViewController).

Anyone knows how?

4条回答
\"骚年 ilove
2楼-- · 2019-03-10 21:24
typedef void (^UIActivityViewControllerCompletionHandler)
    (NSString *activityType, BOOL completed);

The second parameter of the completion handler tells you whether the user dismissed the controller. If they dismissed the controller, completed will be set to NO.

There's more details in the completion handler's documentation.

查看更多
forever°为你锁心
3楼-- · 2019-03-10 21:24

With iOS 8 completionHandler is deprecated, so you will want to provide alternative completionWithItemsHandler. It also allows to handle if extension modified data you set (say Photo extension). See Documentation

    UIActivityViewController *activityViewController = [[UIActivityViewController alloc] initWithActivityItems:@[text, URL, image] applicationActivities:nil];
    activityViewController.excludedActivityTypes = @[UIActivityTypePrint, UIActivityTypeCopyToPasteboard, UIActivityTypeAssignToContact, UIActivityTypeAirDrop, UIActivityTypeAddToReadingList];

    // check if new API supported
    if ([activityViewController respondsToSelector:@selector(completionWithItemsHandler)]) {
        activityViewController.completionWithItemsHandler = ^(NSString *activityType, BOOL completed, NSArray *returnedItems, NSError *activityError) {
            // When completed flag is YES, user performed specific activity
        };
    } else {
        activityViewController.completionHandler = ^(NSString *activityType, BOOL completed) {
            // When completed flag is YES, user performed specific activity
        };
    }
查看更多
闹够了就滚
4楼-- · 2019-03-10 21:32

Here's how you show a sharing dialog and set its completion handler

NSArray *imageArray = [NSArray arrayWithObject:shareImage];

UIActivityViewController *sharing = [[UIActivityViewController alloc] initWithActivityItems:imageArray applicationActivities:nil];

[sharing setCompletionHandler:^(NSString *activityType, BOOL completed) {
    NSLog(@"completed dialog - activity: %@ - finished flag: %d", activityType, completed);
}];

[self presentViewController:sharing animated:YES completion:nil];
查看更多
Melony?
5楼-- · 2019-03-10 21:38

For Swift, this one works for me...

    // Configure UIActivityViewController
    let activityViewController = UIActivityViewController(activityItems: objectsToShare, applicationActivities: nil)
    activityViewController.excludedActivityTypes = [UIActivityTypeAirDrop,
        UIActivityTypeAddToReadingList,
        UIActivityTypeAssignToContact,
        UIActivityTypePrint,
        UIActivityTypeCopyToPasteboard]

    // Show UIActivityViewController
    presentViewController(activityViewController, animated: true, completion: nil)

    // Define completion handler
    activityViewController.completionWithItemsHandler = doneSharingHandler

...

func doneSharingHandler(activityType: String!, completed: Bool, returnedItems: [AnyObject]!, error: NSError!) {

    // Return if cancelled
    if (!completed) {
        return
    }
    // If here, log which activity occurred
    println("Shared video activity: \(activityType)")
}
查看更多
登录 后发表回答