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?
Asked
Active
Viewed 2.0k times
54
-
4Always nice to see a question that I was going to ask. – Morkrom Sep 25 '13 at 18:54
4 Answers
48
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) {
-
2Thanks this is helpful! This is good if I need to know what they picked afterwards, which I do, but I also need to know beforehand. For instance if someone hits the twitter button, I want to display a different message than I do with Facebook – Adam Jan 18 '13 at 23:25
-
You'd need to implement your custom data objects that conform to the `UIActivityItemSource` protocol. You can then present your data in a format specific to the activity type (or just track the type). [NSHipster](http://nshipster.com/uiactivityviewcontroller/) has all the details. – hagi Nov 27 '14 at 13:19
-
-
1@Stela if you are using the simulator, only the mail option will be displayed. You will need to test on a device with twitter, Facebook, etc. installed. – dstudeba Nov 08 '15 at 15:23
34
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")
}
}

Allan Scofield
- 1,884
- 18
- 14
4
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)

Ahmed Safadi
- 4,402
- 37
- 33
1
You can use this lib https://github.com/rdougan/RDActivityViewController to identify which activity has been touched and return custom data by activity just by implementing its protocol.
Like This:
- (NSArray *)activityViewController:(NSArray *)activityViewController itemsForActivityType:(NSString *)activityType
{
if ([activityType isEqualToString:UIActivityTypePostToTwitter]) {
return @[@"Twitter text"];
} else if ([activityType isEqualToString:UIActivityTypeMessage]) {
return @[@"Message text"];
} else if ([activityType isEqualToString:UIActivityTypeMail]) {
return @[@"Mail text"];
} else if ([activityType isEqualToString:UIActivityTypePostToFacebook]) {
return @[@"Facebook text"];
} else {
return @[@"Default text"];
}
}
I hope I have helped you!

Cassius Pacheco
- 57
- 4
-
5Just got my app rejected because of this protocol call. Apparently it's a private API. Do not use this library. – L_Sonic Sep 15 '16 at 08:29