Yuchen Zhong Yuchen Zhong - 1 month ago 11x
iOS Question

Share data with different types in UIActivityViewController

There is a extremely similar question asked by the following post: Different data for sharing providers in UIActivityViewController. But my question is different.

I know how to share different different data of the same type with different activities by using

. For example:

- (id) activityViewController:(UIActivityViewController *)activityViewController
itemForActivityType:(NSString *)activityType
if ( [activityType isEqualToString:UIActivityTypePostToTwitter] )
return @"This is a #twitter post!";
if ( [activityType isEqualToString:UIActivityTypePostToFacebook] )
return @"This is a facebook post!";
if ( [activityType isEqualToString:UIActivityTypeAirDrop] )
return @"Airdrop message text";
return nil;

However, my question is: what if I have different kind of data to share with different activities, what should I do?. For example, what if I would like to share:

  • a string on Twitter;

  • a array of an string and and image on Facebook;

  • the actual data of the image (e.g. NSData) with Airdrop.


I also looked at the following protocol function:

- (id)activityViewControllerPlaceholderItem:;

However, I cannot use it because we don't know the value of


You'd want to create and share two or more objects that conform to the UIActivityItemSource, where one returns the String, another one an Image, etc. Then when the delegate callback requesting the item is called you check which activity type was selected (Facebook, Mail, AirDrop, etc) and have one or multiple of the ItemSource's return nil if that item doesn't apply to that activity. Make sure for any chosen activity that at least one of the item's return a non-nil value.

You can take a look at the airdrop sample code to get some examples of how to implement UIActivityItemSource