Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UIActivityViewController customize text based on selected activity

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

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

Ofcourse I'm using UIActivityViewController:

UIActivityViewController *activityVC = [[UIActivityViewController alloc] initWithActivityItems:@[shareText, shareURL] applicationActivities:nil];
like image 380
Ertai Avatar asked Sep 28 '12 12:09

Ertai


3 Answers

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!

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
like image 99
NickNack Avatar answered Oct 05 '22 14:10

NickNack


Instead of passing the text strings into the initWithActivityItems call, pass in your own sub-class of the UIActivityItemProvider class and when you implement the itemForActivityType method it will provide the sharing service as the 'activityType' parameter.

You can then return the customized content from this method.

like image 28
Christopher King Avatar answered Oct 05 '22 16:10

Christopher King


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.

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)
like image 32
pkorosec Avatar answered Oct 05 '22 14:10

pkorosec