iOS11: UIActivityViewController not successfully sharing UIImage to 3rd party apps
Short Version / TLDR
By converting your image into Data first, and then passing that object to the UIActivityViewController solves the Twitter / Facebook problem when trying to pass an image:
if let jpgImage = UIImageJPEGRepresentation(image, 0.8) {
let vc = UIActivityViewController(activityItems: [jpgImage], applicationActivities: [])
}
Longer explantation and full code can be found below:
Longer Version
I reached out to a few experts who all implied it should just work, but even when looking at their sample apps, they all exhibited the same problem. So I ultimately thought this might be an issue with Twitter or Facebook since sharing to the Apple apps just worked. I then got around to playing with the Photos app and decided to hit the share button there, and voila, it worked with Twitter and Facebook! This made me wonder whether this meant there was some incremental setup needed to get it this to work. After trying many different things I finally found a method that worked. By converting the image to Data via a PNG or JPG and passing that new object to your activityItems solves the Twitter / Facebook problem and still seems to work with everything else. The updated swift4 / ios11 code converting the image to JPG would look like:
@objc func shareTapped() {
if let image = imageView.image {
if let jpgImage = UIImageJPEGRepresentation(image, 0.8) {
let vc = UIActivityViewController(activityItems: [jpgImage], applicationActivities: [])
// add the following line if using doing universal and need iPad
vc.popoverPresentationController?.barButtonItem = navigationItem.rightBarButtonItem
present(vc, animated: true)
}
}
}
Hope this helps anyone else that's been struggling sharing an image since iOS11.