Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS - Send Image to Instagram - DocumentInteraction

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

like image 870
Martin Marconcini Avatar asked Mar 31 '16 23:03

Martin Marconcini


1 Answers

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)
            }

        }
}
like image 87
Zuhair Hussain Avatar answered Oct 14 '22 06:10

Zuhair Hussain