Reputation: 8608
I'm trying to share an image with text through UIActivityViewController
. If I do this:
let activityVC = UIActivityViewController(activityItems: [text, image], applicationActivities: nil)
self.presentViewController(activityVC, animated: true, completion: nil)
Everything works fine. The problem is that I only want to share the image with certain activity types. i.e. when a user shares to Facebook I don't want to have an image, for everything else I do though. My problem is this stupid method is never called:
optional func activityViewController(_ activityViewController: UIActivityViewController,
thumbnailImageForActivityType activityType: String?,
suggestedSize size: CGSize) -> UIImage?
Which should be becuase it's defined in UIActivityItemSource
protocol. Is there any work around to this?
So I believe to have made some headway here. Turns our if you pass multiple values of self
when instantiating UIActivityViewController
you can return multiple values in the itemForActivityType
delegate method. So if I do this:
let activityVC = UIActivityViewController(activityItems: [self, self], applicationActivities: nil)
I can return different values like this:
func activityViewController(activityViewController: UIActivityViewController, itemForActivityType activityType: String) -> AnyObject? {
if activityType == UIActivityTypePostToFacebook {
return ["hello", "world"]
}
else {
return ["goodnight", "moon"]
}
}
However, it seems that you can only return two values of the same type.
My new question is now, how would I return both an image and text?? The hunt continues...
Upvotes: 3
Views: 2928
Reputation: 2096
In order to share two different set of content you have to create two different itemsource
we can set different text content for different activity type.Add the MyStringItemSource class to your viewcontroller
SourceOne:
class MyStringItemSource: NSObject, UIActivityItemSource {
@objc func activityViewControllerPlaceholderItem(activityViewController: UIActivityViewController) -> AnyObject {
return ""
}
@objc func activityViewController(activityViewController: UIActivityViewController, itemForActivityType activityType: String) -> AnyObject? {
//You can pass different text for for diffrent activity type
if activityType == UIActivityTypePostToFacebook {
return "String for facebook"
}else{
return "String for Other"
}
}
}
Our requirement is to add image to all activity type except FB,to do that add the MyImageItemSource class in your VC.
SourceTwo:
class MyImageItemSource: NSObject, UIActivityItemSource {
@objc func activityViewControllerPlaceholderItem(activityViewController: UIActivityViewController) -> AnyObject {
return ""
}
@objc func activityViewController(activityViewController: UIActivityViewController, itemForActivityType activityType: String) -> AnyObject? {
//This one allows us to share image ecxept UIActivityTypePostToFacebook
if activityType == UIActivityTypePostToFacebook {
return nil
}
let Image: UIImage = UIImage(data: NSData(contentsOfURL: NSURL(string: "https://pbs.twimg.com/profile_images/604644048/sign051.gif")!)!)!
return Image
}
}
Now we are ready to set UIActivityViewController,here we go
@IBAction func Test(sender: AnyObject) {
let activityVC = UIActivityViewController(activityItems: [MyStringItemSource(),MyImageItemSource()] as [AnyObject], applicationActivities: nil)
//Instead of using rootviewcontroller go with your own way.
if let window = (UIApplication.sharedApplication().delegate as? AppDelegate)?.window
{
window.rootViewController?.presentViewController(activityVC, animated: true, completion: nil)
}
}
TWITTER Shared Dialogue: Contains image and given text
FB Share Dialogue: Contains only the given text
Upvotes: 4