2

如何与 Instagram 应用共享 UIImage?

我知道,这个话题已经讨论过很多次了。但由于 SWIFT 的演进,我希望能找到更好的解决方案。我找到了两种不同的方法,都可以在我的设备上使用:

1.) 第一种方法: UIApplication.shared.open()

func backgroundImage(_ backgroundImage: Data, attributionURL: String) {
    // Verify app can open custom URL scheme, open if able
    guard let urlScheme = URL(string: "instagram-stories://share"),
        UIApplication.shared.canOpenURL(urlScheme) else {
            // Handle older app versions or app not installed case
            return
    }

    let pasteboardItems = [["com.instagram.sharedSticker.backgroundImage": backgroundImage,
                            "com.instagram.sharedSticker.contentURL": attributionURL]]
    let pasteboardOptions: [UIPasteboard.OptionsKey: Any] = [.expirationDate: Date().addingTimeInterval(60 * 5)]

    UIPasteboard.general.setItems(pasteboardItems, options: pasteboardOptions)

    UIApplication.shared.open(urlScheme)
}

这会直接在 Instagram 应用中打开我的图像。到目前为止一切顺利,但通过这种方式,只能在Instagram 故事中分享。

2.) 第二种方法: UIDocumentInteractionController()

private let kInstagramURL = "instagram://"
private let kUTI = "com.instagram.exclusivegram"
private let kfileNameExtension = "instagram.igo"

[...]

if UIApplication.shared.canOpenURL(instagramURL! as URL) {

        let jpgPath = (NSTemporaryDirectory() as NSString).appendingPathComponent(kfileNameExtension)

        do {
            try UIImageJPEGRepresentation(imageInstagram, 0.75)?.write(to: URL(fileURLWithPath: jpgPath), options: .atomic)
        } catch {
            print(error)
        }
        let rect = CGRect.zero
        let fileURL = NSURL.fileURL(withPath: jpgPath)

        documentInteractionController.url = fileURL
        documentInteractionController.delegate = self
        documentInteractionController.uti = kUTI

        // adding caption for the image
        documentInteractionController.annotation = ["InstagramCaption": instagramCaption]
        documentInteractionController.presentOpenInMenu(from: rect, in: view, animated: true)
        //documentInteractionController.presentOptionsMenu(from: rect, in: view, animated: true)
}

这将打开 UIDocumentInteractionController 弹出窗口,用户可以在其中选择他想要共享的应用程序。不幸的是,许多应用程序可以处理“instagram.igo”: Popup 的屏幕截图

我现在的问题:

  • 我想让用户选择,如果他想在提要或故事中分享,因此解决方案 1 是没有选择的(只有故事可能)
  • UIInteractionController 打开带有太多应用程序的愚蠢弹出窗口。

我现在的问题: 有一个解决方案可以直接打开应用程序,不弹出!但是否也可以直接到达 Instagram-Selection-Screen 而不弹出? Instagram 选择屏幕

Insta Developer Hooks中没有帮助

4

0 回答 0