9

我一直在尝试使用新的 UIActivityViewController 来替换我所有的 UIActionSheets 进行共享,但是我遇到了一个问题。

我有 5 个活动,消息、电子邮件、复制、Twitter 和 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),而不是消息、电子邮件或副本。有什么办法可以做到这一点?

4

2 回答 2

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;
}
于 2013-04-14T03:30:38.097 回答
0

您应该能够实现自己的 UIActivityItemProvider,并为您不想使用该数据的任何活动类型返回 nil。

于 2013-05-16T14:35:21.993 回答