Search code examples
uiactivityviewcontroller

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?


Solution

  • 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) {