I’m refactoring my code to use UIActivitiy and UIActivityProvider. I ran into a problem where Twitter did not show up any longer in the UIActivityViewController.
I discovered Twitter would not show up if more than one of my UIActivityProvider classes assigned the PlaceholderItem to a UIImage class object. No other Activity seems to be affected by placeholder classes the Activity can’t use.
For context, here’s some of the setup code:
NSArray *applicationActivities = @[reviewActivity, myEmailActivity];
NSArray *excludeActivities = @[UIActivityTypeAssignToContact,
UIActivityTypePrint,
UIActivityTypeSaveToCameraRoll,
UIActivityTypePostToWeibo];
UIActivityItemProvider *screenshotProvider = [[ScreenshotActivityProvider alloc] initWithView :self.scrollView];
UIActivityItemProvider *textProvider = [[TextActivityProvider alloc] initWithExchangeDoc:self.docItem];
UIActivityItemProvider *imageProvider = [[ImageActivityProvider alloc] initWithExchangeDoc:self.docItem];
NSArray *activityItems = @[textProvider, screenshotProvider, imageProvider];
applicationActivities:applicationActivities];
UIActivityViewController *activityVC = [[UIActivityViewController alloc]initWithActivityItems:activityItems
applicationActivities:applicationActivities];
A sample UIActivityItemProvider I’m using is:
- (id)initWithExchangeDoc :(ExchangeDoc *)docItem
{
self = [super initWithPlaceholderItem:[UIImage new]];
_docItem = docItem;
return self;
}
- (id)item
{
if ([self.activityType isEqualToString:UIActivityTypePostToTwitter]) {
return self.docItem.thumbImage;
} else {
return nil;
}
}
The problem lies in the initWithPlaceholderItem. I have another UIActivityProvider that identifies a UIImage as the placeholder class.
I thought the purpose of custom item providers was that my class could decide, after UIActivityViewContrller launch, what data was served. In my case, my screenshot provider does not provide anything for Twitter. Why is the iOS looking at my placeholder and making the decision not to serve Twitter as a viable Activity? My providers will sort out who gets to supply the image based upon the activity type.
The docs say this about placeholderItem:
An object that can stand in for the actual object you plan to create. The contents of the object may be empty but the class of the object must match the class of the object you plan to provide later.
Obviously, this isn’t working for me regarding Twitter. As an experiment, I changed all the placeholderItems to empty NSStrings and Twitter and all other activities ran fine.
Anyone know what these placeholderItems are used for and whether setting them to the wrong class will have any negative effect?
Sorry this is so long. I appreciate your help.