我正在努力让从我的应用程序到 Instagram 的分享变得简单。我想要的是进入下面屏幕截图所示的屏幕。我已经尝试过 instagram-stories://share deeplink 并且我已经阅读了所有这些文档:https://developers.facebook.com/docs/instagram/sharing-to-stories/ 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)
}
})
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)