在与FBSDK共享视频时如何显示对话框?

时间:2018-05-11 21:13:34

标签: ios swift video fbsdk fbsdksharekit

在分享静态照片时,我可以让我的应用切换到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转换为资产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读取状态"打印在我的控制台中,它最终发布到Facebook,但这样做没有显示本机共享对话框。 (基本上它无形地与Facebook共享,没有任何对用户的视觉反馈)。我希望它转移到Facebook应用程序,其内容将由用户确认,就像在我的应用程序中共享照片的方式一样。

另一种尝试是将FBSDKShareVideo与初始化器参数一起使用" videoURL"和" previewPhoto"。我确保视频不到12兆字节(在我的情况下是4.4 MB),sharePhoto和fileURL都是有效的。共享对话框不起作用,这意味着它不会转移到本机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()

1 个答案:

答案 0 :(得分:2)

这就是我如何使其工作的方式。 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)
  }
}