ios iphone hooks open instagram directly without showing UIDocumentInteractionController
Asked Answered
S

3

8

I'm integrating sharing images through instagram in my app. I've read their documentation.

It seems that I need to use the iOS UIDocumentInteractionController to allow this (I understand that it allows access to files in my app's sandbox).

Digging deeper, I came across this library which makes things very straightforward.

The problem I have with it is that it presents the action sheet (which only has one button - Instagram...) How can I use the instagram hooks , with the UIDocumentInteractionController without showing the action sheet. I came across this question which is pretty much the same, but its outdated with no answer.

Scratchboard answered 20/5, 2013 at 10:45 Comment(0)
D
5

I've searched for this myself, I don't think it's possible to use the UIDocumentInteractionController without showing an action sheet, nor does it seem possible to share an image with instagram without using a UIDocumentInteractionController.

Which leads to the unavoidable action sheet.

I understand why they designed it like that (you won't leave an app unknowingly as a user) but it leads to annoying UI design in many cases.

Dazzle answered 29/5, 2013 at 15:18 Comment(2)
Check Shots app. They do it without UIDocumentInteractionController. I wonder how..Swahili
The app "10" shares without an action sheet. That's what got me searching on this topic.Byers
N
7

I use a very simple share-open Instagram flow.

  • I save image locally to photos
  • Then I open Instagram with following URL: instagram://library?AssetPath=assets-library

This opens Instagram directly into photos library. Because photo was saved few moments ago, the new photo is visible as the first photo in library.

Nudism answered 13/1, 2016 at 11:41 Comment(1)
man.. that's brilliant ! I thought that was a way to open photo library instead of camera but this is even betten for sharing single photos. Well.. if you got an URI to open photolibrary i take it as well ^^Zhang
D
5

I've searched for this myself, I don't think it's possible to use the UIDocumentInteractionController without showing an action sheet, nor does it seem possible to share an image with instagram without using a UIDocumentInteractionController.

Which leads to the unavoidable action sheet.

I understand why they designed it like that (you won't leave an app unknowingly as a user) but it leads to annoying UI design in many cases.

Dazzle answered 29/5, 2013 at 15:18 Comment(2)
Check Shots app. They do it without UIDocumentInteractionController. I wonder how..Swahili
The app "10" shares without an action sheet. That's what got me searching on this topic.Byers
I
4

It looks like this method is NOT mentioned in the Instagram documentation. However, I just confirmed that @sabiland's answer still works in Swift 4.2 iOS 12. Here is some sample code:

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

    let urlString = "instagram://library?AssetPath=assets-library"

    let url = URL(string: urlString)!
    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 code source

You also have to make sure you info.plist has the instagram query scheme.

In order for your app to use Instagram's custom URL scheme, you mush whitelist the scheme by adding instagram:// to the LSApplicationQueriesSchemes key in your app's Info.plist.

Source

info.plist

Itinerate answered 20/3, 2019 at 0:4 Comment(1)
Just switched my app to using this method after the old way of using Instagram hooks via the share sheet stopped working properly in iOS 13. This is a much cleaner and nicer method too!Output

© 2022 - 2024 — McMap. All rights reserved.