簡體   English   中英

在 iOS Swift 上發布到 Instagram 屏幕

[英]Post To Instagram screen on iOS Swift

我正在嘗試使從我的應用程序共享到 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)
                }
            }

您需要做的是使用以下網址打開 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)
    }
})
  1. 假設您的image是“png”
  2. 假設您在 info.plist 中允許“instagram-stories”(關鍵LSApplicationQueriesSchemes

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM