How to share image in instagram?Swift How to share image in instagram?Swift swift swift

How to share image in instagram?Swift


if you don't want to use UIDocumentInteractionController

SWIFT 5 update

import Photos...func postImageToInstagram(image: UIImage) {    UIImageWriteToSavedPhotosAlbum(image, self, #selector(image(_:didFinishSavingWithError:contextInfo:)), nil)}@objc func image(_ image: UIImage, didFinishSavingWithError error: Error?, contextInfo: UnsafeRawPointer) {        if error != nil {            print(error)        }        let fetchOptions = PHFetchOptions()        fetchOptions.sortDescriptors = [NSSortDescriptor(key: "creationDate", ascending: false)]        let fetchResult = PHAsset.fetchAssets(with: .image, options: fetchOptions)        if let lastAsset = fetchResult.firstObject as? PHAsset {            let url = URL(string: "instagram://library?LocalIdentifier=\(lastAsset.localIdentifier)")!            if UIApplication.shared.canOpenURL(url) {                UIApplication.shared.open(url)            } else {                let alertController = UIAlertController(title: "Error", message: "Instagram is not installed", preferredStyle: .alert)                alertController.addAction(UIAlertAction(title: "OK", style: .default, handler: nil))                self.present(alertController, animated: true, completion: nil)            }        }}


    class viewController: UIViewController, UIDocumentInteractionControllerDelegate {    var yourImage: UIImage?    var documentController: UIDocumentInteractionController!    func shareToInstagram() {     let instagramURL = NSURL(string: "instagram://app")            if (UIApplication.sharedApplication().canOpenURL(instagramURL!)) {                let imageData = UIImageJPEGRepresentation(yourImage!, 100)                let captionString = "caption"           let writePath = (NSTemporaryDirectory() as NSString).stringByAppendingPathComponent("instagram.igo")           if imageData?.writeToFile(writePath, atomically: true) == false {                    return                } else {   let fileURL = NSURL(fileURLWithPath: writePath)                    self.documentController = UIDocumentInteractionController(URL: fileURL)                    self.documentController.delegate = self                    self.documentController.UTI = "com.instagram.exlusivegram"                    self.documentController.annotation = NSDictionary(object: captionString, forKey: "InstagramCaption")                          self.documentController.presentOpenInMenuFromRect(self.view.frame, inView: self.view, animated: true)                }            } else {                print(" Instagram isn't installed ")            }        }     }    }

Now this still wont work with iOS 9, so you will have to go to your apps info.plist, add "LSApplicationQueriesSchemes" type: Array, and add the URL Scheme in this case "instagram".


Swift 3.0 Version :

 @IBAction func shareInstagram(_ sender: Any) {        DispatchQueue.main.async {            //Share To Instagram:            let instagramURL = URL(string: "instagram://app")            if UIApplication.shared.canOpenURL(instagramURL!) {                let imageData = UIImageJPEGRepresentation(image, 100)                let writePath = (NSTemporaryDirectory() as NSString).appendingPathComponent("instagram.igo")                do {                    try imageData?.write(to: URL(fileURLWithPath: writePath), options: .atomic)                } catch {                    print(error)                }                let fileURL = URL(fileURLWithPath: writePath)                self.documentController = UIDocumentInteractionController(url: fileURL)                self.documentController.delegate = self                self.documentController.uti = "com.instagram.exlusivegram"                if UIDevice.current.userInterfaceIdiom == .phone {                    self.documentController.presentOpenInMenu(from: self.view.bounds, in: self.view, animated: true)                } else {                    self.documentController.presentOpenInMenu(from: self.IGBarButton, animated: true)                }            } else {                print(" Instagram is not installed ")            }        }    }