I'm having an issue with sharing an image to Instagram via the iOS share sheet. I need to share the image through a URL to a local file, since I want the image to be named a certain way when shared, and that's the only way I found I could do that. The code is:
//This saves the image into the local temp directory and returns the file url
saveImageForSharing(image: image!) {(tempUrl) in
if tempUrl != nil {
let objectsToShare = [tempUrl!]
let activityVC = UIActivityViewController(activityItems: objectsToShare, applicationActivities: nil)
self.present(activityVC, animated: true, completion: nil)
}
}
When the UIActivityViewController comes up you can share the image successfully to ALL the services available (including Facebook, email, Twitter etc), EXCEPT Instagram. When sharing to Instagram, the following error pops up:
Setting a completion handler for the UIActivityViewController's completionWithItemsHandler, the 'success' parameter is returned as false, but the error object is nil, so there's no information to be had about what went wrong.
When sharing a UIImage directly, there's no problem. Also when saving to a different URL or when changing the file name.
Any ideas what could be happening here, or how I can share an image successfully while also setting the item's name?
Edit: I've also encountered this problem when trying to share a video through its url. The video is stored simply in the Photos library, and I can't get Instagram to accept it, even though it shared perfectly well when sharing from the Photos app! Also, I'm getting a message in the output console that says "Failed to ssue sandbox token for URL:". Any advice?
Thanks!
Give this a try and see if it resolves your issue.
class ShareItem: NSObject, UIActivityItemSource {
private let url: URL
private let title: String
init(url: URL, title: String) {
self.url = url
self.title = title
}
func activityViewControllerPlaceholderItem(_: UIActivityViewController) -> Any {
url
}
func activityViewController(_: UIActivityViewController, itemForActivityType _: UIActivity.ActivityType?) -> Any? {
url
}
func activityViewController(_: UIActivityViewController, subjectForActivityType _: UIActivity.ActivityType?) -> String {
title
}
func activityViewController(_: UIActivityViewController, dataTypeIdentifierForActivityType _: UIActivity.ActivityType?) -> String {
(try? url.resourceValues(forKeys: [.typeIdentifierKey]).typeIdentifier) ?? ""
}
func activityViewController(_: UIActivityViewController, thumbnailImageForActivityType _: UIActivity.ActivityType?, suggestedSize _: CGSize) -> UIImage? {
UIImage(contentsOfFile: url.absoluteString)
}
func activityViewControllerLinkMetadata(_: UIActivityViewController) -> LPLinkMetadata? {
let metadata = LPLinkMetadata()
metadata.originalURL = url
metadata.url = url
metadata.title = title
return metadata
}
}
You would then pass an instance of ShareItem
to the UIActivityViewController
instead of your tempUrl.
// This saves the image into the local temp directory and returns the file url
saveImageForSharing(image: image!) { tempUrl in
guard let url = tempUrl else {
return
}
let activityVC = UIActivityViewController(
activityItems: [
ShareItem(url: url, title: "Your Title")
],
applicationActivities: nil
)
self.present(
activityVC,
animated: true,
completion: nil
)
}
Hopefully this helps you.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With