UIActivityViewController根据所选活动自定义文本

时间:2012-09-28 12:24:11

标签: xcode ios6 sharing

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

如何根据将使用哪种共享服务来分散文本以进行共享?

当然我正在使用UIActivityViewController:

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

3 个答案:

答案 0 :(得分:40)

我接受了这个答案并为它做了一个简单的课程。通过除Twitter之外的共享出口可以看到默认消息,并且如果默认消息中存在哈希词阵列中的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

答案 1 :(得分:28)

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

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

答案 2 :(得分:3)

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

catch

用法:

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