gpt4 book ai didi

ios - 使用 FBSDK 分享视频时如何显示对话框?

转载 作者:行者123 更新时间:2023-11-28 10:41:29 29 4
gpt4 key购买 nike

分享静态照片时,我可以让我的应用程序切换到 Facebook 来分享该图像。它似乎与视频不一样。

这分享一张照片就好了。它移动到 Facebook 应用程序以确认帖子:

let sharePhoto = FBSDKSharePhoto()
sharePhoto.image = photo

let content = FBSDKSharePhotoContent()
content.photos = [sharePhoto]

let shareDialog: FBSDKShareDialog = FBSDKShareDialog()
shareDialog.shareContent = content
shareDialog.mode = .native
shareDialog.show()

同样,我在分享视频时不能这样做!没有对话,没有切换到 Facebook 应用程序,也没有发布视频:

let shareVdo: FBSDKShareVideo = FBSDKShareVideo()
shareVdo.videoURL = self.fileURL
let vdoContent = FBSDKShareVideoContent()
vdoContent.video = shareVdo

let shareDialog: FBSDKShareDialog = FBSDKShareDialog()
shareDialog.shareContent = vdoContent
shareDialog.mode = .native
shareDialog.show()

这将分享我的视频,但没有对话,或者先转到 Facebook 应用程序!

let shareVdo: FBSDKShareVideo = FBSDKShareVideo()
shareVdo.videoURL = self.fileURL
let vdoContent = FBSDKShareVideoContent()
vdoContent.video = shareVdo
FBSDKShareAPI.share(with: vdoContent, delegate:self)

根据文档,我可能需要将我的 fileURL 转换为 Assets URL。我不清楚是否应该使用 FBSDKShareAPI:

let shareVdo: FBSDKShareVideo = FBSDKShareVideo()
let asset = AVAsset(url: self.fileURL)
let assetURL = self.getAssetUrl(asset:asset)
shareVdo.videoURL = assetURL
let vdoContent = FBSDKShareVideoContent()
vdoContent.video = shareVdo
//FBSDKShareAPI.share(with: vdoContent, delegate:self)

let shareDialog: FBSDKShareDialog = FBSDKShareDialog()
shareDialog.shareContent = vdoContent
shareDialog.mode = .native
shareDialog.show()

如果我取消注释 FBSDKShareAPI.share 函数调用,我会在我的控制台中看到“TIC Read Status”,它最终会发布到 Facebook,但不会显示 native 共享对话框。 (基本上,它在不向用户提供任何视觉反馈的情况下无形地分享到 Facebook)。我希望它移动到 Facebook 应用程序,内容由用户确认,就像在我的应用程序中共享照片一样。

还有一个尝试是使用带有初始化参数“videoURL”和“previewPhoto”的 FBSDKShareVideo。我确保视频小于 12 兆字节(在我的例子中是 4.4 MB),sharePhoto 和那个 fileURL 都是有效的。共享对话框不起作用,这意味着它不会转移到 native Facebook 应用程序中。 Facebook 开发人员指南使用 imagePickerController 显示它,这可能意味着 SDK 需要来自您的相机胶卷的视频。

let photo = self.uiImages[0]

let sharePhoto = FBSDKSharePhoto()
sharePhoto.image = photo

let filePath = self.fileURL

// get size of video in bytes
do {
var fileSize : UInt64
let attr = try FileManager.default.attributesOfItem(atPath: (filePath?.path)!)
fileSize = attr[FileAttributeKey.size] as! UInt64

print(fileSize)

} catch {
print("Error: \(error)")
}

let shareVideo = FBSDKShareVideo(videoURL: self.fileURL, previewPhoto: sharePhoto)

let content = FBSDKShareVideoContent()
content.video = shareVideo

let shareDialog: FBSDKShareDialog = FBSDKShareDialog()

shareDialog.shareContent = content

shareDialog.mode = .native
shareDialog.show()

最佳答案

这是我如何让它工作的。 FB 需要一种特定的 URL 格式。

if let lastAsset = fetchResult.firstObject {
let localID = lastAsset.localIdentifier
let assetID = localID.replacingOccurrences(of: "/.*", with: "", options: NSString.CompareOptions.regularExpression, range: nil)
let ext = "mp4"
let assetURLStr = "assets-library://asset/asset.\(ext)?id=\(assetID)&ext=\(ext)"
let video = FBSDKShareVideo(videoURL: URL(string: assetURLStr))
let content = FBSDKShareVideoContent()
content.video = video
let dialog = FBSDKShareDialog()
dialog.delegate = self
dialog.shareContent = content
dialog.shouldFailOnDataError = true
dialog.mode = .automatic
dialog.fromViewController = self

dialog.show()
}

这使用了 ShareDialog() 而不是 FBSDKShareDialog() 的另一种方法

let fetchOptions = PHFetchOptions()
fetchOptions.sortDescriptors = [NSSortDescriptor(key: "creationDate", ascending: false)]
let fetchResult = PHAsset.fetchAssets(with: .video, options: fetchOptions)
if let lastAsset = fetchResult.firstObject {
let localID = lastAsset.localIdentifier
let assetID = localID.replacingOccurrences(of: "/.*", with: "", options: NSString.CompareOptions.regularExpression, range: nil)
let ext = "mp4"
let assetURLStr = "assets-library://asset/asset.\(ext)?id=\(assetID)&ext=\(ext)"
let video = Video(url: URL(string: assetURLStr)!)
let content = VideoShareContent(video: video)
let dialog = ShareDialog(content: content)
dialog.failsOnInvalidData = true
dialog.mode = .automatic
dialog.presentingViewController = self
do {
try dialog.show()
} catch {
print(error)
}
}

关于ios - 使用 FBSDK 分享视频时如何显示对话框?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50300335/

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