34

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

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

当然我正在使用 UIActivityViewController:

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

3 回答 3

41

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

用法:

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

标题:

@interface TwitterHashActivityItemProvider : UIActivityItemProvider

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

@property (nonatomic,strong) NSArray *hashItems;

@end

执行:

#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
于 2012-11-12T16:59:07.297 回答
29

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

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

于 2012-09-30T02:42:51.937 回答
4

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

用法:

let itemProvider = PasswordShareItemsProvider(password: password)
let activityViewController = UIActivityViewController(activityItems: [itemProvider], applicationActivities: nil)
于 2018-02-12T11:31:25.290 回答