gpt4 book ai didi

ios - 在 iOS Swift 上发布到 Instagram 屏幕

转载 作者:可可西里 更新时间:2023-11-01 01:07:25 26 4
gpt4 key购买 nike

我正在努力简化从我的应用程序到 Instagram 的分享。我想要的是进入下面屏幕截图中描述的屏幕。我试过 instagram-stories://share deeplink 并且我通读了所有这些文档:https://developers.facebook.com/docs/instagram/sharing-to-stories/

但是,无论我做什么,当 url scheme Action 触发时,它会直接将图像分享到故事中。我在这里缺少什么?

enter image description here

这是我的代码摘录:

           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
由于某种原因,这个钩子(Hook)没有在文档中的任何地方列出🤷‍♂️

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

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)
}
})

关于ios - 在 iOS Swift 上发布到 Instagram 屏幕,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55211886/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com