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

How do you show a dialog when sharing a video with the FBSDK?

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

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

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 应用程序,也没有 post 视频:

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)

根据文档,我可能需要将我的文件URL 转换为资产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" 打印在我的控制台中,它最终 post 发送到 Facebook,但这样做时没有显示本机共享对话框. (基本上,它在不向用户提供任何视觉反馈的情况下无形地分享到 Facebook)。我希望它移动到 Facebook 应用程序,内容由用户确认,就像在我的应用程序中共享照片一样。

还有一个尝试是使用带有初始化参数 "videoURL" 和 "previewPhoto" 的 FBSDKShareVideo。我确保视频小于 12 兆字节(在我的例子中是 4.4 MB),sharePhoto 和那个文件URL 都是有效的。共享对话框不起作用,这意味着它不会转移到本机 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)
  }
}