1

I need to share an image file to Instagram, and according to Instagram doc: https://www.instagram.com/developer/mobile-sharing/iphone-hooks/, I can use Document Interaction to implement this, but it just doesn't work. Here is my code:

        let img = UIImage(named: "test.jpg")
        let kInstagramURL = URL(string: "instagram://")
        let kUTI = "com.instagram.exclusivegram" // testing "com.instagram.photo" too
        let instagramCaption = "TESTING"
        let kfileNameExtension = "instagram.igo" // testing "jpg"/"ig" too

        if UIApplication.shared.canOpenURL(kInstagramURL!) {
            let urls = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)
            if urls.count == 0 {
                print("Not found")
                return
            }
            let url = urls[0].appendingPathComponent(kfileNameExtension)
            print("URL", url)
            // OUPUT: file:///var/mobile/Containers/Data/Application/F14B18CB-5CD4-47AD-8257-E2288C23181E/Documents/instagram.igo
            do {
                try UIImageJPEGRepresentation(img!, 1.0)?.write(to: url, options: .atomic)
                print("WRITEN", url) // Written successfully, no error
            } catch {
                //TODO: issue error on UI
                print("WRITE ERROR: \(error)")
            }

            let documentInteractionController = UIDocumentInteractionController(url: url)
            var rect = CGRect.zero
            let view = self.view

            documentInteractionController.delegate = DocInteractionResponder() // tried nil, same result
            documentInteractionController.uti = kUTI


            // adding caption for the image
            documentInteractionController.annotation = ["InstagramCaption": instagramCaption]
            documentInteractionController.presentOpenInMenu(from: rect, in: view!, animated: true)
        }
        else {
            //TODO: warn from dialog
        }

I have tried many things within this code snippet (see comments), but the result is the same: after I tap instagram in the menu, nothing happens.

I thought it may be caused by other code in my app, so I create a blank app (with one simple UIViewController) and put this code in. It doesn't work.

I have read almost all answers on SO related to UIDocumentInteractionController.

Does this happen to you? Is this the problem with iOS 11 (just upgraded my phone), or maybe Instagram stop supporting Document Interaction?

NeoWang
  • 17,361
  • 24
  • 78
  • 126
  • 1
    Not related to your question but worths mentioning. From the docs **To check whether a collection is empty, use its isEmpty property instead of comparing count to zero. Unless the collection guarantees random-access performance, calculating count can be an O(n) operation. Complexity: O(1) if the collection conforms to RandomAccessCollection; otherwise, O(n), where n is the length of the collection.** – Leo Dabus Oct 10 '17 at 17:46
  • Here is an example on how to share any file type to any app (including Instagram of iOS 11.0.2) https://stackoverflow.com/a/46457518/2303865 – Leo Dabus Oct 10 '17 at 17:49
  • @LeoDabus Thanks for the tip. But the code in your answer doesn't work for me. Strange... – NeoWang Oct 10 '17 at 18:25
  • The code doesn’t work is vague. I have never tested to use it to share to a single App but it does show Instagram for me here in my App – Leo Dabus Oct 10 '17 at 18:26
  • Yes, Instagram shows in the menu. But tapping it doesn't trigger an app switch, I have tested Whatsapp, too. – NeoWang Oct 10 '17 at 18:28
  • I've tested here sharing to Instagram does work but it actually doesn't open the App. – Leo Dabus Oct 10 '17 at 18:46
  • I have the same issue. – David van Dugteren Mar 13 '18 at 23:27
  • Did you find any solution? – Coder ACJHP May 08 '20 at 13:40
  • 1
    @CoderACJHP Nope – NeoWang May 17 '20 at 05:54

1 Answers1

5

I just came across the same issue today, and I was able to solve it by declaring the UIDocumentInteractionController as a class-level property in the view controller, to prevent it from getting deallocated too soon.

taykay08
  • 341
  • 4
  • 9