我正在使用ios 7的MobileCoreServices将内容从应用发布到社交媒体。 。这是我的代码..
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];
现在它发送普通文本。我希望为Facebook和Twitter发布不同的标题。有可能吗?
答案 0 :(得分:7)
您需要继承UIActivityItemProvider
@interface APCustomActivityProvider : UIActivityItemProvider <UIActivityItemSource>
@end
将以下内容添加到您的实施文件
@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
要使用自定义ActivityProvider,请转到您展示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];