I am using UIActivityViewController to share text, a screenshot and a URL. It works fine for Twitter, email, SMS etc. In fact everything except Facebook.
As I understand it, Facebook no longer allows pre-filled text to be shared, so I expect just the screenshot and URL to be shared. But when the user selects Facebook, the image that appears is an image taken directly from the URL instead of the image I specify (ie the screenshot). Here is a screenshot of what happens when I use www.google.com as the URL.
If I don't include a URL, then the screenshot appears just fine, but in effect it is just sharing a photo (which is how it appears on Facebook, saying "John Doe added a new photo" and then my screenshot is shown). This all happens whether I'm logged into the Facebook app or not.
Here is my code:
@IBAction func shareButton(sender: AnyObject) {
// Get screenshot
UIGraphicsBeginImageContextWithOptions(UIScreen.mainScreen().bounds.size, false, 0)
self.view.drawViewHierarchyInRect(view.bounds, afterScreenUpdates: true)
let screenshot:UIImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
let textToShare = "My text here"
let myWebsite = NSURL(string: "http://www.example.com/")
socialShare(sharingText: textToShare, sharingImage: screenshot, sharingURL: myWebsite)
}
And then:
func socialShare(sharingText sharingText: String?, sharingImage: UIImage?, sharingURL: NSURL?) {
var sharingItems = [AnyObject]()
if let text = sharingText {
sharingItems.append(text)
}
if let image = sharingImage {
sharingItems.append(image)
}
if let url = sharingURL {
sharingItems.append(url)
}
let activityViewController = UIActivityViewController(activityItems: sharingItems, applicationActivities: nil)
// Excluded sharing options
activityViewController.excludedActivityTypes = [UIActivityTypeCopyToPasteboard,UIActivityTypeAirDrop,UIActivityTypeAddToReadingList,UIActivityTypeAssignToContact,UIActivityTypePostToTencentWeibo,UIActivityTypePostToVimeo,UIActivityTypePrint,UIActivityTypePostToWeibo]
// The popover needs additional code to work on iPad, so distinguish between iPhone & iPad
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiom.Phone) { // If iPhone
self.presentViewController(activityViewController, animated: true, completion: nil)
} else { // If iPad
let popoverCntlr = UIPopoverController(contentViewController: activityViewController)
popoverCntlr.presentPopoverFromRect(CGRectMake(self.view.frame.size.width - 40, 80, 0, 0), inView: self.view, permittedArrowDirections: UIPopoverArrowDirection.Up, animated: true) // position where the popover appears
}
}
So does anybody know how to share a screenshot AND a URL via Facebook using UIActivityViewController? I suspect the problem is caused from the Facebook end as everything works fine for Twitter etc, but is there a workaround?