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

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)