在iOS Swift上发布到Instagram屏幕 [英] Post To Instagram screen on iOS Swift
本文介绍了在iOS Swift上发布到Instagram屏幕的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在努力让从我的应用程序到Instagram的分享变得容易。我想要的是转到下面屏幕截图上所描绘的屏幕。我试过Instagram-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)
}
}
推荐答案
您需要做的是使用以下URL打开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)
}
})
这篇关于在iOS Swift上发布到Instagram屏幕的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文