gpt4 book ai didi

ios - 使用UIActivityViewController是否可以为不同的社交媒体包含不同的内容?

转载 作者:行者123 更新时间:2023-12-01 17:31:06 29 4
gpt4 key购买 nike

我正在使用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发布不同的标题。可能吗?

最佳答案

  • 您需要子类化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];
  • 关于ios - 使用UIActivityViewController是否可以为不同的社交媒体包含不同的内容?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22295817/

    29 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com