xcode UIActivityViewController 根据选定的活动自定义文本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12639982/
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 01:40:55  来源:igfitidea点击:

UIActivityViewController customize text based on selected activity

xcodeios6sharing

提问by Ertai

I want to customize text for the same information but when I am sharing it on Facebook I don't want to use the twitter hash tags or @username scheme...

我想为相同的信息自定义文本,但是当我在 Facebook 上共享它时,我不想使用 twitter 哈希标签或 @username 方案......

How can I diversify text for sharing based on which sharing service would be used?

如何根据将使用的共享服务使共享文本多样化?

Ofcourse I'm using UIActivityViewController:

当然我正在使用 UIActivityViewController:

UIActivityViewController *activityVC = [[UIActivityViewController alloc] initWithActivityItems:@[shareText, shareURL] applicationActivities:nil];

采纳答案by Christopher King

Instead of passing the text strings into the initWithActivityItemscall, pass in your own sub-class of the UIActivityItemProviderclass and when you implement the itemForActivityTypemethod it will provide the sharing service as the 'activityType' parameter.

不是将文本字符串传递到initWithActivityItems调用中,而是传递您自己的类的子UIActivityItemProvider类,当您实现该itemForActivityType方法时,它将提供共享服务作为“activityType”参数。

You can then return the customized content from this method.

然后,您可以从此方法返回自定义内容。

回答by NickNack

I took this answer and made a simple class for it. The default message will be seen by sharing outlets other than Twitter, and for Twitter words within the hashWords array will appear with hashes if they are present in the default message. I thought I would share it for anyone else who needs it. Thanks Christopher!

我接受了这个答案,并为它做了一个简单的类。默认消息将被 Twitter 以外的共享渠道看到,并且 hashWords 数组中的 Twitter 单词将与哈希一起出现,如果它们存在于默认消息中。我想我会把它分享给任何需要它的人。谢谢克里斯托弗!

Usage:

用法:

TwitterHashActivityItemProvider *twit = [[TwitterHashActivityItemProvider alloc] initWithDefaultText:@"I really like stackoverflow and code"
                                                                                           hashWords:@[@"stackoverflow", @"code"]];
NSArray *items = @[twit];
UIActivityViewController *act = [[UIActivityViewController alloc] initWithActivityItems:items applicationActivities:nil];

Header:

标题:

@interface TwitterHashActivityItemProvider : UIActivityItemProvider

- (id)initWithDefaultText:(NSString*)text hashWords:(NSArray*)hashItems;

@property (nonatomic,strong) NSArray *hashItems;

@end

Implementation:

执行:

#import "TwitterHashActivityItemProvider.h"

@implementation TwitterHashActivityItemProvider

- (id)initWithDefaultText:(NSString*)text hashWords:(NSArray*)hashItems;
{
    self = [super initWithPlaceholderItem:text];
    if ( self )
    {
        self.hashItems = hashItems;
    }
    return self;
}

- (id)item
{
    if ( [self.placeholderItem isKindOfClass:[NSString class]] )
    {
        NSString *outputString = [self.placeholderItem copy];

        // twitter gets some hash tags!
        if ( self.activityType == UIActivityTypePostToTwitter )
        {
            // go through each potential hash item and augment the main string
            for ( NSString *hashItem in self.hashItems)
            {
                NSString *hashed = [@"#" stringByAppendingString:hashItem];
                outputString = [outputString stringByReplacingOccurrencesOfString:hashItem withString:hashed];
            }
        }

        return outputString;
    }

    // else we didn't actually provide a string...oops...just return the placeholder
    return self.placeholderItem;
}

@end

回答by pkorosec

Swift implementation example of an UIActivityItemProvider subclass. Copy option will use only the password, other activity types will use the full share text. Should be easy to customize for different use cases. Credit to Cristopher & NickNack for their answers.

UIActivityItemProvider 子类的 Swift 实现示例。复制选项将仅使用密码,其他活动类型将使用完整的共享文本。应该很容易针对不同的用例进行定制。感谢 Cristopher 和 NickNack 的回答。

class PasswordShareItemsProvider: UIActivityItemProvider {

    private let password: String

    private var shareText: String {
        return "This is my password: " + password
    }

    init(password: String) {
        self.password = password
        // the type of the placeholder item is used to
        // display correct activity types by UIActivityControler
        super.init(placeholderItem: password)
    }

    override var item: Any {
        get {
            guard let activityType = activityType else {
                return shareText
            }

            // return desired item depending on activityType

            switch activityType {
            case .copyToPasteboard: return password
            default: return shareText
            }
        }
    }
}

Usage:

用法:

let itemProvider = PasswordShareItemsProvider(password: password)
let activityViewController = UIActivityViewController(activityItems: [itemProvider], applicationActivities: nil)