iosiphoneios7facebook-social-plugins

Is it possible to include different content for different social media using UIActivityViewController?


I am using MobileCoreServices of ios 7 to post content from app to social media. . And here is my code ..

NSArray *activityItems;

NSString *title =  _titleLabel.text;

if (_imageLabel.image != nil) {
    activityItems = @[title, _imageLabel.image];
} else {
    activityItems = @[title];
}

UIActivityViewController *activityController =
[[UIActivityViewController alloc]
 initWithActivityItems:activityItems
 applicationActivities:nil];

[self presentViewController:activityController

    animated:YES completion:nil];

Now it sends common text. I wish to post different titles for Facebook and twitter. Is it possible?


Solution

    1. You need to subclass UIActivityItemProvider

      @interface APCustomActivityProvider : UIActivityItemProvider <UIActivityItemSource>
      @end
      
    2. Add the following to your implementation file

      @implementation APCustomActivityProvider
      - (id) activityViewController:(UIActivityViewController *)activityViewController
        itemForActivityType:(NSString *)activityType
      {
        if ( [activityType isEqualToString:UIActivityTypePostToTwitter] ){
          NSString *title = @"Twitter title";
          return title;
        }
        if ( [activityType isEqualToString:UIActivityTypePostToFacebook] ) {
          NSString *title = @"Facebook title";
          return title;
        }
        return nil;
      }
      
      - (id) activityViewControllerPlaceholderItem:(UIActivityViewController*)activityViewController { return @""; }
      @end
      
    3. To use your custom ActivityProvider, go to where you are presenting your UIActivityViewController

      NSArray *activityItems;
      APCustomActivityProvider *activityProvider = [[APCustomActivityProvider alloc] init];
      if (_imageLabel.image != nil) {
          activityItems = @[activityProvider, _imageLabel.image];
      } else {
          activityItems = @[activityProvider];
      }
      
      
      UIActivityViewController *activityController = 
      [[UIActivityViewController alloc] initWithActivityItems:activityItems   
                                        applicationActivities:nil];
      
      [self presentViewController:activityController
                         animated:YES completion:nil];