Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UIActivityViewController - is there a way to know which activity was selected?

Id like to be able to track if the user shared by facebook, twitter, etc, but it seems there's no way to know which method was selected. Is there?

like image 267
Adam Avatar asked Jan 04 '13 00:01

Adam


3 Answers

You can use Activity Types in setCompletionHandler

UIActivityViewController *activityVC = [[UIActivityViewController alloc]initWithActivityItems:activityItems applicationActivities:nil];

[activityVC setCompletionHandler:^(NSString *activityType, BOOL completed) {
    if([activityType isEqualToString: UIActivityTypeMail]){
        NSLog(@"Mail");
    }
    if([activityType isEqualToString: UIActivityTypePostToFacebook]){
        NSLog(@"Facebook");
    }

}];
[self presentViewController:activityVC animated:TRUE completion:nil];

Built-in activity types for which the system has built-in support for.

NSString *const UIActivityTypePostToFacebook;

NSString *const UIActivityTypePostToTwitter;

NSString *const UIActivityTypePostToWeibo;

NSString *const UIActivityTypeMessage;

NSString *const UIActivityTypeMail;

NSString *const UIActivityTypePrint;

NSString *const UIActivityTypeCopyToPasteboard;

NSString *const UIActivityTypeAssignToContact;

NSString *const UIActivityTypeSaveToCameraRoll;

Edited for iOS 8

Please note that this will generate a compiler warning in iOS 8, you need to use the setCompletionWithItemsHandler method instead of the setCompletionHandler method.

Replace:

[activityVC setCompletionHandler:^(NSString *activityType, BOOL completed) {

with:

[activityVC setCompletionWithItemsHandler:^(NSString *activityType, BOOL completed,  NSArray *returnedItems, NSError *activityError) {
like image 192
Yossarian Avatar answered Nov 17 '22 10:11

Yossarian


In Swift 4:

activityViewController.completionWithItemsHandler = { activity, success, items, error in
        if !success{
            print("cancelled")
            return
        }

        if activity == .postToTwitter {
            print("twitter")
        }

        if activity == .mail {
            print("mail")
        }
    }
like image 34
Allan Scofield Avatar answered Nov 17 '22 10:11

Allan Scofield


swift 3

let activityViewController:UIActivityViewController = UIActivityViewController(activityItems: [screenCapture], applicationActivities: nil)
activityViewController.excludedActivityTypes = [UIActivityType.print, UIActivityType.postToWeibo, UIActivityType.copyToPasteboard, UIActivityType.addToReadingList, UIActivityType.postToVimeo]

activityViewController.completionWithItemsHandler = { activity, success, items, error in
    if !success{
        print("cancelled")
        return
    }

    if activity == UIActivityType.postToFacebook {
        print("facebook")
    }

    if activity == UIActivityType.mail {
        print("mail")
    }

}

self.present(activityViewController, animated: true, completion: nil)
like image 4
Ahmed Safadi Avatar answered Nov 17 '22 09:11

Ahmed Safadi