Set different activity items for UIActivityViewController Swift

I have UIActivityViewController with 4 options: Message / Mail / TW / FB.
I want to send different activities for each option!

For eg:
In iMessage sheet, I need to put : String, NSURL, and UIImage.
In Mail I need to place string in Subject field, then String in body, UIImage and NSURL also.
In TW/FB I want to place image like socials post did it, also some String and NSURL.

  • How to add custom buttons to UIActivityViewController?
  • Can I exclude “More” Button in UIActivityViewController?
  • iOS: How to share text and image on social networks?
  • How to set mail subject with UIActivityItemProvider
  • Facebook, Twitter icon not showing in UIActivityViewController on iOS7
  • Share more than 5 images with UIActivityViewController
  • Do you have any idea if this is possible in iOS8, with Swift ?

    I searched a lot for some pieces of code, did not found the best for me.

    2 Solutions Collect From Internet About “Set different activity items for UIActivityViewController Swift”

    You should take advantage of the UIActivityItemSource protocol. The activityItems parameter of the initializer of UIActivityViewController accepts either an array of data objects or an array of objects that implement the UIActivityItemSource protocol.

    As an example consider an item source like the following.

    class MyStringItemSource: NSObject, UIActivityItemSource {
        @objc func activityViewControllerPlaceholderItem(activityViewController: UIActivityViewController) -> AnyObject {
            return ""
        }
    
        @objc func activityViewController(activityViewController: UIActivityViewController, itemForActivityType activityType: String) -> AnyObject? {
            if activityType == UIActivityTypeMessage {
                return "String for message"
            } else if activityType == UIActivityTypeMail {
                return "String for mail"
            } else if activityType == UIActivityTypePostToTwitter {
                return "String for twitter"
            } else if activityType == UIActivityTypePostToFacebook {
                return "String for facebook"
            }
            return nil
        }
    
        func activityViewController(activityViewController: UIActivityViewController, subjectForActivityType activityType: String?) -> String {
            if activityType == UIActivityTypeMessage {
                return "Subject for message"
            } else if activityType == UIActivityTypeMail {
                return "Subject for mail"
            } else if activityType == UIActivityTypePostToTwitter {
                return "Subject for twitter"
            } else if activityType == UIActivityTypePostToFacebook {
                return "Subject for facebook"
            }
            return ""
        }
    
        func activityViewController(activityViewController: UIActivityViewController, thumbnailImageForActivityType activityType: String!, suggestedSize size: CGSize) -> UIImage! {
            if activityType == UIActivityTypeMessage {
                return UIImage(named: "thumbnail-for-message")
            } else if activityType == UIActivityTypeMail {
                return UIImage(named: "thumbnail-for-mail")
            } else if activityType == UIActivityTypePostToTwitter {
                return UIImage(named: "thumbnail-for-twitter")
            } else if activityType == UIActivityTypePostToFacebook {
                return UIImage(named: "thumbnail-for-facebook")
            }
            return UIImage(named: "some-default-thumbnail")
        }
    }
    

    The above item source returns different string data objects, subjects and thumbnail images based on the activity type. To use, you just need to pass it into the UIActivityViewController initializer.

    UIActivityViewController(activityItems: [MyStringItemSource()], applicationActivities: nil)
    

    Similarly, you could define a custom MyUrlItemSource class that returns different URLs based on the selected activity and pass it along in the initializer.

    UIActivityViewController(activityItems: [MyStringItemSource(), MyUrlItemSource()], applicationActivities: nil)
    

    All of this is outlined in the official documentation for UIActivityViewController and UIActivityItemSource

    Same solution like from hennes translated to swift 3

    class MyStringItemSource: NSObject, UIActivityItemSource {
    
    public func activityViewControllerPlaceholderItem(_ activityViewController: UIActivityViewController) -> Any
    {
        return ""
    }
    
    public func activityViewController(_ activityViewController: UIActivityViewController, itemForActivityType activityType: UIActivityType) -> Any?
    {
        if activityType == UIActivityType.message {
            return "String for message"
        } else if activityType == UIActivityType.mail {
            return "String for mail"
        } else if activityType == UIActivityType.postToTwitter {
            return "String for twitter"
        } else if activityType == UIActivityType.postToFacebook {
            return "String for facebook"
        }
        return nil
    }
    
    public func activityViewController(_ activityViewController: UIActivityViewController, subjectForActivityType activityType: UIActivityType?) -> String
    {
        if activityType == UIActivityType.message {
            return "Subject for message"
        } else if activityType == UIActivityType.mail {
            return "Subject for mail"
        } else if activityType == UIActivityType.postToTwitter {
            return "Subject for twitter"
        } else if activityType == UIActivityType.postToFacebook {
            return "Subject for facebook"
        }
        return ""
    }
    
    public func activityViewController(_ activityViewController: UIActivityViewController, thumbnailImageForActivityType activityType: UIActivityType?, suggestedSize size: CGSize) -> UIImage?
    {
        if activityType == UIActivityType.message {
            return UIImage(named: "thumbnail-for-message")
        } else if activityType == UIActivityType.mail {
            return UIImage(named: "thumbnail-for-mail")
        } else if activityType == UIActivityType.postToTwitter {
            return UIImage(named: "thumbnail-for-twitter")
        } else if activityType == UIActivityType.postToFacebook {
            return UIImage(named: "thumbnail-for-facebook")
        }
        return UIImage(named: "some-default-thumbnail")
    }
    

    }