UIDocumentInteractionController不能与Instagram

时间:2017-10-10 17:41:28

标签: ios swift instagram uidocumentinteraction

我需要与Instagram共享一个图像文件,根据Instagram文档:https://www.instagram.com/developer/mobile-sharing/iphone-hooks/,我可以使用文档交互来实现这一点,但它只是不起作用。这是我的代码:

        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
        }

我在这段代码中尝试了很多东西(见评论),但结果是一样的:在菜单中点击instagram后,没有任何反应。

我认为它可能是由我的应用程序中的其他代码引起的,所以我创建了一个空白的应用程序(使用一个简单的UIViewController)并将此代码放入。它不起作用。

我已经阅读了与UIDocumentInteractionController相关的几乎所有答案。

这是否发生在你身上?这是iOS 11的问题(刚升级我的手机),还是Instagram停止支持文档交互?

1 个答案:

答案 0 :(得分:2)

我今天也遇到了同样的问题,我能够通过在视图控制器中将UIDocumentInteractionController声明为类级别的属性来解决此问题,以防止它过早地被释放。