xcode UIActivityViewController - 仅附加某些活动的 URL

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15994961/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 03:12:41  来源:igfitidea点击:

UIActivityViewController - Only attach a URL for certain activities

iosobjective-cxcodeurluiactivityviewcontroller

提问by mhbdr

I've been trying to use the new UIActivityViewController to replace all of my UIActionSheets for sharing, however I've run into an issue.

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

I have 5 activities, Message, Email, Copy, Twitter, and Facebook. I've already figured out how to have those show different text, through this in a custom UIActivityProvider subclass:

我有 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;
}

And this in my view controller:

这在我的视图控制器中:

    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];

However, I only want to show the URL (sharedURL) in the Facebook and Twitter share sheets, not the message, email or copy. Any way to accomplish this?

但是,我只想在 Facebook 和 Twitter 共享表中显示 URL (sharedURL),而不是消息、电子邮件或副本。有什么办法可以做到这一点吗?

回答by HalR

You can choose what to send to each type of activity. I created a custom activity provider and call it like this:

您可以选择要发送到每种类型的活动的内容。我创建了一个自定义活动提供程序并像这样调用它:

     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];

The custom provider is a subclass of UIActivityItemProvider

自定义提供程序是 UIActivityItemProvider 的子类

@interface CustomActivityItemProvider : UIActivityItemProvider

- (id)initWithText:(NSString *)text urlText:(NSString *)url;

@end

The guts of my custom provider class look something like this:

我的自定义提供程序类的内容如下所示:

- (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;
}

回答by José Manuel Sánchez

You should be able to do this implementing your own UIActivityItemProvider, and returning nil for any activity type where you don't want to use that data.

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