Daniel Wijono
Daniel Wijono

Reputation: 86

Share image instagram using UIActivityViewController swift 3/4 have alert controller error

I have search in stackoverflow using swift 3/4 to share image instagram using UIActivityViewController. But when I want to share image then click instagram logo, then there is alert that says "We're sorry, but something went wrong. Please try again. Is there anyway to solve this ?

            let image = UIImage(named: "instaIcon.png")
            let objectsToShare: [AnyObject] = [ image! ]
            let activityViewController = UIActivityViewController(activityItems: objectsToShare, applicationActivities: nil)
            activityViewController.popoverPresentationController?.sourceView = self.view

            activityViewController.excludedActivityTypes = [ UIActivityType.airDrop, UIActivityType.postToFacebook, UIActivityType.addToReadingList, UIActivityType.assignToContact, UIActivityType.copyToPasteboard,
            UIActivityType.mail, UIActivityType.markupAsPDF, UIActivityType.message, UIActivityType.openInIBooks,
            UIActivityType.postToFacebook, UIActivityType.postToFlickr, UIActivityType.postToTencentWeibo,
            UIActivityType.postToTwitter, UIActivityType.postToVimeo, UIActivityType.postToWeibo]

            self.present(activityViewController, animated: true, completion: nil)

Upvotes: 1

Views: 813

Answers (1)

Carlos Henrique
Carlos Henrique

Reputation: 25

so for our future devs, I had the same issue when trying to share a post or story using the UIActivityViewController and an UIImage it always returned to me Something went wrong when I tapped on the story or post button from the UIActivityViewController.

So the solution was to share the image URL instead of the UIImage, basically, just save it to a temporary folder and then share the URL and you will be fine.

guard let imageData = self.jpegData(compressionQuality: 0.5) else { return nil }
         
let tempDir = FileManager.default.temporaryDirectory
let url = tempDir.appendingPathComponent("anyName.jpg")
            
do {
   try imageData.write(to: url)
   vc.present(UIActivityViewController(activityItems: [url], applicationActivities: nil))
} catch {
    return nil
}

Upvotes: 2

Related Questions