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?
You need to subclass UIActivityItemProvider
@interface APCustomActivityProvider : UIActivityItemProvider <UIActivityItemSource>
@end
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
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];