我一直在尝试使用新的UIActivityViewController来替换所有的UIActionSheets进行共享,但是我遇到了一个问题。
我有5个活动,消息,电子邮件,复制,推特和Facebook。我已经想出了如何在自定义的UIActivityProvider子类中显示不同的文本:
- (id) activityViewController:(UIActivityViewController *)activityViewController
itemForActivityType:(NSString *)activityType
{
if ( [activityType isEqualToString:UIActivityTypePostToTwitter] )
return twitter;
if ( [activityType isEqualToString:UIActivityTypePostToFacebook] )
return facebook;
if ( [activityType isEqualToString:UIActivityTypeMessage] )
return urlScheme;
if ( [activityType isEqualToString:UIActivityTypeMail] )
return urlScheme;
if ( [activityType isEqualToString:UIActivityTypeCopyToPasteboard])
return urlScheme;
return nil;
}
这在我的视图控制器中:
ActivityProvider *aProvider = [[ActivityProvider alloc] init];
aProvider.facebook = facebook;
aProvider.twitter = twitter;
aProvider.urlScheme = URL;
NSArray *Items = @[aProvider, sharedURL];
UIActivityViewController *activityVC = [[UIActivityViewController alloc]initWithActivityItems:Items applicationActivities:Nil];
activityVC.excludedActivityTypes = @[UIActivityTypePrint, UIActivityTypeAssignToContact,UIActivityTypeSaveToCameraRoll, UIActivityTypePostToWeibo];
[self presentViewController:activityVC animated:TRUE completion:nil];
但是,我只想在Facebook和Twitter共享表中显示URL(sharedURL),而不是消息,电子邮件或副本。有什么方法可以实现这个目标吗?
答案 0 :(得分:15)
您可以选择向每种活动发送的内容。我创建了一个自定义活动提供程序,并将其命名为:
NSMutableArray *activityItems = [NSMutableArray array];
CustomActivityItemProvider *activityItemProvider =
[[CustomActivityItemProvider alloc] initWithText:text
urlText:urlString];
[activityItems addObject:activityItemProvider];
//you can have your own custom activities too:
NSArray *applicationActivities = @[[CustomActivity new],
[OtherCustomActivity new]];
UIActivityViewController *vc = [[UIActivityViewController alloc]initWithActivityItems:activityItems
applicationActivities:applicationActivities];
自定义提供程序是UIActivityItemProvider
的子类@interface CustomActivityItemProvider : UIActivityItemProvider
- (id)initWithText:(NSString *)text urlText:(NSString *)url;
@end
我的自定义提供程序类的内容看起来像这样:
- (id)initWithText:(NSString *)text urlText:(NSString *)url{
if ((self = [super initWithPlaceholderItem:text])) {
self.text = text ?: @"";
self.url = url ?: @"";
}
return self;
}
- (id)item {
NSString *activityType = self.activityType;
if ([self.placeholderItem isKindOfClass:[NSString class]]) {
if ([self.activityType isEqualToString:UIActivityTypePostToFacebook] ||
[self.activityType isEqualToString:UIActivityTypeMail]) {
return [NSString stringWithFormat:@"%@\n%@", self.text, self.url];
} else if ([activityType isEqualToString:UIActivityTypePostToTwitter]) {
return [self findBestStringOfLength:kTwitterMessageLength hashTags:YES];
} else if ([activityType isEqualToString:UIActivityTypeMessage]) {
return [self findBestStringOfLength:kSMSMessageLength hashTags:YES];
} else {
return self.text;
}
}
return self.placeholderItem;
}
答案 1 :(得分:0)
您应该能够实现自己的UIActivityItemProvider,并为您不想使用该数据的任何活动类型返回nil。