Question
What changes to the code below must be made to ensure Messenger plays nicely with
UIActivityViewController
and shares both the image and text, or at the very least, the image?
Background
I am using UIActivityViewController
to share text and images from my app and send them to email, messages and other sharing apps. UIActivityViewController
is great and works in a simple and standard way with most apps… but, I’m having issues with Messenger (Facebook Messenger) which doesn’t want to cooperate.
In the below code, I tap a UIButton
which takes a snapshot image of the screen, turns it into a .png, and then sends the prepared image (imageShare
) along with my prepared text (textShare
) to the UIActivityViewController
. This simple method allows my app to successfully share to email, messages and many other sharing apps except Messenger.
(Side note, Facebook is only able to share the prepared image (imageShare
) but not the text.)
Issues
These are the problems when trying to share with UIActivityViewController
to Messenger :
When sharing
activityItems: [textShare, imageShare]
- Messenger only sends the shared text.
When sharing
activityItems: [textShare]
- The option to share to Messenger is not even available in
UIActivityViewController
.
- The option to share to Messenger is not even available in
When sharing
activityItems: [imageShare]
- An error is displayed: “Couldn't Load Content. There was a problem while loading your content. Please try again.”
Code
@IBAction func myButton(sender: UIButton) {
// Take snapshot of screen
let imageSnapshot: UIImage!
UIGraphicsBeginImageContextWithOptions(self.view.frame.size, false, 0)
self.view.drawViewHierarchyInRect(CGRect(x: 0, y: 0, width: self.view.bounds.width, height: self.view.bounds.height), afterScreenUpdates: false)
imageSnapshot = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
// Prepare text to share
let textShare: String!
textShare = "This is my original text."
// Prepare image to share
let imageShare: NSData
imageShare = UIImagePNGRepresentation(imageSnapshot)!
// Share text and image
let activity = UIActivityViewController(activityItems: [textShare, imageShare], applicationActivities: nil)
if UIDevice.currentDevice().userInterfaceIdiom == .Phone {
self.presentViewController(activity, animated: true, completion: nil)
}
}
Image
iOS presenting the UIActivityViewController
with available applications.