【问题标题】:Post To Instagram screen on iOS Swift在 iOS Swift 上发布到 Instagram 屏幕
【发布时间】:2019-03-17 21:01:50
【问题描述】:

我正在努力让从我的应用分享到 Instagram 变得容易。我想要的是进入下面屏幕截图中描述的屏幕。我已经尝试过 instagram-stories://share deeplink 并且我已经阅读了所有这些文档:https://developers.facebook.com/docs/instagram/sharing-to-stories/

但是,无论我做什么,当 url 方案动作触发时,它都会直接将图像分享到故事中。我在这里错过了什么?

这是我的代码摘录:

           if let image = image {
                guard let urlScheme = URL(string: "instagram-stories://share"),
                    let imageData = image.pngData() else {
                    return
                }

                if UIApplication.shared.canOpenURL(urlScheme) {
                    let pasterboardItems = [["com.instagram.sharedSticker.backgroundImage": imageData]]
                    let pasterboardOptions = [UIPasteboard.OptionsKey.expirationDate: Date().addingTimeInterval(60*5)]

                    UIPasteboard.general.setItems(pasterboardItems, options: pasterboardOptions)

                    UIApplication.shared.open(urlScheme, options: [:], completionHandler: nil)
                }
            }

【问题讨论】:

    标签: ios swift facebook instagram


    【解决方案1】:

    您需要做的是使用以下网址打开 Instagram 应用程序:
    instagram://library?LocalIdentifier= 并作为参数传递 PHAsset.localIdentifier
    由于某种原因,这个钩子没有在文档中的任何地方列出?‍♂️

    但为了接收您的图片/视频的本地标识符,您必须先将图片/视频保存到用户的照片库。 所以最终的代码应该是这样的

    let videoFileUrl: URL = URL(fileURLWithPath: "path/to/my/video")!
    var localId: String?
    PHPhotoLibrary.shared().performChanges({
        let request = PHAssetChangeRequest.creationRequestForAssetFromVideo(atFileURL: videoFileUrl)
        localId = request?.placeholderForCreatedAsset?.localIdentifier
    }, completionHandler: { success, error in
        // completion handler is called on an arbitrary thread
        // but since you (most likely) will perform some UI stuff
        // you better move everything to the main thread.
        DispatchQueue.main.async {
            guard error == nil else {
                // handle error
                return
            }
            guard let localId = localId else {
                // highly unlikely that it'll be nil,
                // but you should handle this error just in case
                return
            }
    
            let url = URL(string: "instagram://library?LocalIdentifier=\(localId)")!
            guard UIApplication.shared.canOpenURL(url) else {
                // handle this error
                return
            }
            UIApplication.shared.open(url, options: [:], completionHandler: nil)
        }
    })
    

    【讨论】:

    • App Store 应用程序中使用 Instagram 保存功能的 Apple 块。可以不保存就提供代码吗?
    • @ucelme 我正在使用相同的方法将我的应用程序分享到 Instagram,如果遇到任何解决方案,请告诉我。我还没有在应用商店上架。
    【解决方案2】:
    1. 假设您的image 是“png”
    2. 假设您允许 info.plist 中的“instagram-stories”(键 LSApplicationQueriesSchemes

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-07-02
      • 1970-01-01
      • 2017-08-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多