I share text and an image using the UIActivityViewController
. The image is from activityImage
which is an UIImage()
. The custom text is from activityText()
which is returned from an UIActivityItemSource
protocol.
The trouble is, Instagram is only visible in the UIActivityViewController
under certain conditions.
To summerize the tests below:
Instagram is only visible in the
UIActivityViewController
foractivityItems: [activityImage]
andactivityItems: [activityImage, NSNull()]
Instagram is hidden for
activityItems: [activityImage, activityText()]
even when the returnedUIActivityItemSource
protocol value is""
,nil
,NSNull()
.
The issue is that the UIActivityItemSource
protocol needs to be called so that different sharing platforms can be managed separately.
Questions:
How can I share to Instagram when calling the
UIActivityItemSource
protocol?What are the alternatives to
""
,nil
andNSNull()
that I could try when calling theUIActivityItemSource
protocol?
Code:
class activityText: NSObject, UIActivityItemSource {
@objc func activityViewControllerPlaceholderItem(activityViewController: UIActivityViewController) -> AnyObject {
return ""
}
@objc func activityViewController(activityViewController: UIActivityViewController, itemForActivityType activityType: String) -> AnyObject? {
switch activityType {
case UIActivityTypeMessage:
return "Special text when sharing to Messages."
default:
return ""
}
}
}
func shareEverwhere() {
var activityImage: UIImage()
let activity = UIActivityViewController(activityItems: [activityImage, activityText()], applicationActivities: nil)
self.presentViewController(activity, animated: true, completion: nil)
}
Tests:
Instagram is visible in the UIActivityViewController
let activity = UIActivityViewController(activityItems: [activityImage], applicationActivities: nil)
or
let activity = UIActivityViewController(activityItems: [activityImage, NSNull()], applicationActivities: nil)
Instagram is hidden in the UIActivityViewController
let activity = UIActivityViewController(activityItems: [activityImage, ""], applicationActivities: nil)
or
let activity = UIActivityViewController(activityItems: [activityImage, activityText()], applicationActivities: nil)
or
let activity = UIActivityViewController(activityItems: [activityImage, activityText()], applicationActivities: nil)
class activityText: NSObject, UIActivityItemSource {
@objc func activityViewControllerPlaceholderItem(activityViewController: UIActivityViewController) -> AnyObject {
return ""
}
@objc func activityViewController(activityViewController: UIActivityViewController, itemForActivityType activityType: String) -> AnyObject? {
return NSNull()
}
}
or
let activity = UIActivityViewController(activityItems: [activityImage, activityText()], applicationActivities: nil)
class activityText: NSObject, UIActivityItemSource {
@objc func activityViewControllerPlaceholderItem(activityViewController: UIActivityViewController) -> AnyObject {
return ""
}
@objc func activityViewController(activityViewController: UIActivityViewController, itemForActivityType activityType: String) -> AnyObject? {
return nil
}
}
or
let activity = UIActivityViewController(activityItems: [activityImage, activityText()], applicationActivities: nil)
class activityText: NSObject, UIActivityItemSource {
@objc func activityViewControllerPlaceholderItem(activityViewController: UIActivityViewController) -> AnyObject {
return ""
}
@objc func activityViewController(activityViewController: UIActivityViewController, itemForActivityType activityType: String) -> AnyObject? {
return ""
}
}
The question was asked and answered at: Share image with hashtag via UIActivityViewController (Twitter, Facebook, Instagram)
And the class that implements the return of the string (or not) is:
The key, as pointed by the original answer is to return an NSObject (the class extends NSObject). Then in the item selection, just test instagram to return nil and otherwise return your string.