iOS - Send Image to Instagram - DocumentInteraction
Asked Answered
S

1

9

Would it be possible to share a picture to Instagram bypassing the Action Share Sheet?

Please Note that I am aware of the UIDocumentInteractionController and the hooks and in fact it works fine. With their sample code you get a Copy to Instagram option (either unique if you use the exclusive UTI or a big list of apps which can handle a JPG/PNG, alongside with Instagram).

This works fine, but I'd like to know if there's a way to execute the action "Copy to Instagram" without the need to present the UIDocumentInteractionController menu in iOS 9+.

For the record this is a simplified version of the code that works perfectly. Assuming you have a valid NSURL…

        guard let data: NSData = NSData(contentsOfURL: url), 
                  image = UIImage(data: data) else {
            return
        }

        let imageData = UIImageJPEGRepresentation(image, 100)
        let captionString = "caption"
        let writePath = (NSTemporaryDirectory() as NSString).stringByAppendingPathComponent("instagram.ig")

        guard let _ = imageData?.writeToFile(writePath, atomically: true) else {
            return
        }

        let fileURL = NSURL(fileURLWithPath: writePath)
        self.documentController = UIDocumentInteractionController(URL: fileURL)
        self.documentController.delegate = self
        self.documentController.UTI = "com.instagram.photo"
        self.documentController.annotation = NSDictionary(object: captionString, forKey: "InstagramCaption")
        self.documentController.presentOpenInMenuFromRect(viewController.view.frame, inView: viewController.view, animated: true)

The problem is that this will present an "Action Sheet" and I want to avoid doing that if possible, I want to use instagram.ige (or whatever the name it was to make it exclusive) and skip this ActionSheet.

Is that possible?

UPDATE: I haven't found a solution for this, but seems like Instagram finally is adding/added extensions: "Instagram recently added sharing extensions functionality to its iOS app. Now, you can share photos from third-party apps directly to Instagram" source: http://www.macworld.com/article/3080038/data-center-cloud/new-instagram-feature-for-ios-makes-it-easier-to-share-photos-from-other-apps.html

Sedgewake answered 31/3, 2016 at 23:58 Comment(2)
Did you find a solution for this? It seems even after the latest update from Instagram, you still need a share sheet before opening Instagram rather than doing it directly.Kondon
I didn’t, but I also didn’t try again to be honest. I believe (if I don’t incorrectly remember) that the client didn’t care about it and decided to deal with Instagram separately. Lame.Sedgewake
D
3

UPDATED Swift 4.2 CODE

import Photos

    func postImageToInstagram(image: UIImage) {
        UIImageWriteToSavedPhotosAlbum(
            image,
            self,
            #selector(self.image(image:didFinishSavingWithError:contextInfo:)),
            nil
        )
    }

    @objc func image(image: UIImage, didFinishSavingWithError error: NSError?, contextInfo: UnsafeRawPointer) {
        if let err = error {
            print(err) // TODO: handle error
            return
        }

        let fetchOptions = PHFetchOptions()
        fetchOptions.sortDescriptors = [NSSortDescriptor(key: "creationDate", ascending: false)]
        let fetchResult = PHAsset.fetchAssets(with: .image, options: fetchOptions)
        if let lastAsset = fetchResult.firstObject {
            let localIdentifier = lastAsset.localIdentifier
            let u = "instagram://library?AssetPath=" + localIdentifier
            let url = URL(string: u)!
            if UIApplication.shared.canOpenURL(url) {
                UIApplication.shared.open(url, options: [:], completionHandler: nil)
            } 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)
            }
        }
    }

Original Answer

import Photos
...

func postImageToInstagram(image: UIImage) {
        UIImageWriteToSavedPhotosAlbum(image, self, #selector(SocialShare.image(_:didFinishSavingWithError:contextInfo:)), nil)
}
func image(image: UIImage, didFinishSavingWithError error: NSError?, contextInfo:UnsafePointer<Void>) {
        if error != nil {
            print(error)
        }

        let fetchOptions = PHFetchOptions()
        fetchOptions.sortDescriptors = [NSSortDescriptor(key: "creationDate", ascending: false)]
        let fetchResult = PHAsset.fetchAssetsWithMediaType(.Image, options: fetchOptions)
        if let lastAsset = fetchResult.firstObject as? PHAsset {
            let localIdentifier = lastAsset.localIdentifier
            let u = "instagram://library?LocalIdentifier=" + localIdentifier
            let url = NSURL(string: u)!
            if UIApplication.sharedApplication().canOpenURL(url) {
                UIApplication.sharedApplication().openURL(NSURL(string: u)!)
            } else {
                let alertController = UIAlertController(title: "Error", message: "Instagram is not installed", preferredStyle: .Alert)
                alertController.addAction(UIAlertAction(title: "OK", style: .Default, handler: nil))
                self.presentViewController(alertController, animated: true, completion: nil)
            }

        }
}
Drum answered 24/7, 2016 at 9:35 Comment(2)
To make this work, need to add the following in plist : <key>LSApplicationQueriesSchemes</key> <array> <string>instagram</string> </array>Readymade
Is there a way to do this without saving to photo album?Kondon

© 2022 - 2024 — McMap. All rights reserved.