UIActivityViewController customize text based on selected activity

后端 未结 3 1418
生来不讨喜
生来不讨喜 2020-12-16 09:46

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 di

相关标签:
3条回答
  • 2020-12-16 10:11

    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.

    0 讨论(0)
  • 2020-12-16 10:13

    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
    
    0 讨论(0)
  • 2020-12-16 10:13

    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)
    
    0 讨论(0)
提交回复
热议问题