UIActivityViewController customize text based on selected activity

后端 未结 3 1504
生来不讨喜
生来不讨喜 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: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)
    

提交回复
热议问题