I finally find someone who was facing the same problem than me.
UIActivityViewController customize text based on selected activity
I want to customize the content share with the activities of the UIActivityViewController. The good answer is the following:
"Instead of passing the text strings into the initWithActivityItems call, pass in your own sub-class of the UIActivityItemProvider class and when you implement the itemForActivityType method it will provide the sharing service as the 'activityType' parameter.
You can then return the customized content from this method."
I understand tricks, but I'm not getting the way to do it...
I did this as a subclass:
@interface SharingItems : UIActivityItemProvider
@implementation SharingItems
-(id)activityViewController:(UIActivityViewController *)activityViewController itemForActivityType:(NSString *)activityType
{
// Here depending on the activityType i want to share NSString or UIImage
}
@end
But I don't know what to do now in my original viewController:
-(void)actionSheet
{
if ([[UIActivityViewController class] respondsToSelector:@selector(alloc)])
{
__block NSString *imgName = [[NSString alloc] initWithFormat:@"%@", _sharingUrl];
NSArray *activityItems = [NSArray arrayWithObjects:imgName, nil];
UIActivityViewController *activityController = [[UIActivityViewController alloc] initWithActivityItems:activityItems applicationActivities:nil];
[self presentViewController:activityController animated:YES completion:nil];
__block NSString *chan = [[NSString alloc] initWithFormat:@"%@", _channel];
[activityController setCompletionHandler:^(NSString* activityType, BOOL completed)
{
if (completed)
{
}
}];
}
else
[self displayActionSheet];
}
Here's an example UIActivityItemProvider (not tested but adapted from working code):
@implementation StringProvider
- (id)initWithPlaceholderString:(NSString*)placeholder facebookString:(NSString*)facebookString
{
self = [super initWithPlaceholderItem:placeholder];
if (self) {
_facebookString = facebookString;
}
return self;
}
- (id)item
{
if ([self.activityType isEqualToString:UIActivityTypePostToFacebook]) {
return _facebookString;
} else {
return self.placeholderItem;
}
}
@end
Then when you set up the activity view controller:
StringProvider *stringProvider = [[StringProvider alloc] initWithPlaceholderString:@"Default string" facebookString:@"Hello, Facebook."];
UIActivityViewController *shareController = [[UIActivityViewController alloc] initWithActivityItems:@[stringProvider] applicationActivities:nil];
Basically you create UIActivityItemProviders that provide the right data when the -(id)item method is called and pass in those activity item providers when you create the activity view controller. You need to initialize with a placeholder item so the OS knows what class the final item will be (most likely NSString, NSURL, UIImage). Hope that helps!