我的应用程序可以选择使用graph API共享在线视频到Facebook,并且可以使用到2018年8月。我刚刚发现publish_action被弃用了,我们不能使用graph API在Facebook上共享视频。 因此,我尝试了另一种解决方案:将视频下载到本地,将其复制到资产中,然后使用旧的FBSDKShareDialog进行共享(在iOS 7波纹管上设置url,在iOS> = 11上设置资产),但是出现了Facebook上的信息没有视频,只有文字。 然后,我使用新的ShareDialog类对其进行了尝试,因为它仅适用于视频URL,所以在将其复制到资产后,我无法获得其资产URL,但无法获得其文件URL,因此在日志中出现以下错误:
"Error while sharing video content Error Domain=com.facebook.sdk.share Code=2 "(null)" UserInfo={com.facebook.sdk:FBSDKErrorArgumentValueKey=file:///var/mobile/Media/DCIM/105APPLE/IMG_5953.MP4, com.facebook.sdk:FBSDKErrorDeveloperMessageKey=Must refer to an asset file., com.facebook.sdk:FBSDKErrorArgumentNameKey=videoURL}".
这是我的代码。请告诉我要在Facebook上分享视频该怎么做。如果我共享URL,则它仅显示帖子中的URL,而无法显示视频波纹管。 预先感谢!
private func downloadVideoForUploadOnFacebook(urlString: String, videoId: Int, isMyVideo: Bool, successHandler: @escaping () -> Void, failureHandler: @escaping (String) -> Void) {
guard let url = URL(string: urlString) else {delegate.stopActivityIndicator(); return}
let request = Alamofire.request(URLRequest(url: url))
request.responseData {[weak self] (response) in
if let data = response.result.value {
let documentsURL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!
let videoURL = documentsURL.appendingPathComponent("video.mp4")
do {
try data.write(to: videoURL)
self?.showShareDialog(localVideoUrl: videoURL, completion: {
print("success")
successHandler()
}, failure: { (error) in
print(error)
failureHandler(error)
})
} catch {
print("Something went wrong!")
}
}
}.downloadProgress { (progress) in
print(progress)
}
}
private func showShareDialog(localVideoUrl: URL, completion: @escaping () -> Void, failure: @escaping (String) -> Void) {
guard let navigationController = UIApplication.shared.keyWindow?.rootViewController as? UINavigationController, let vc = navigationController.viewControllers.last else {return}
if PHPhotoLibrary.authorizationStatus() == .authorized {
showFacebookShareDialog(localVideoUrl: localVideoUrl, vc: vc, completion: completion, failure: failure)
} else {
PHPhotoLibrary.requestAuthorization({[weak self] (status) in
switch status {
case .authorized:
self?.showFacebookShareDialog(localVideoUrl: localVideoUrl, vc: vc, completion: completion, failure: failure)
default:
print(status)
failure("Authorization error")
}
})
}
}
private func showFacebookShareDialog(localVideoUrl: URL, vc: UIViewController, completion: @escaping () -> Void, failure: @escaping (String) -> Void) {
var placeHolder : PHObjectPlaceholder?
do {
try PHPhotoLibrary.shared().performChangesAndWait {
let creationRequest = PHAssetChangeRequest.creationRequestForAssetFromVideo(atFileURL: localVideoUrl )
placeHolder = creationRequest?.placeholderForCreatedAsset
}
}
catch {
print(error)
failure(error.localizedDescription)
}
let result = PHAsset.fetchAssets(withLocalIdentifiers: [placeHolder!.localIdentifier], options: nil)
if let phAsset = result.firstObject {
getURL(ofPhotoWith: phAsset, completionHandler: {[weak self] (url) in
guard let url = url else {return}
self?.showNewDialog(localVideoUrl: url, vc: vc, completion: completion, failure: failure)
// if #available(iOS 11.0, *) {
// self?.showOldDialog(asset: phAsset, vc: vc)
// } else {
// self?.showOldDialog(localVideoUrl: url, vc: vc)
// }
})
} else {
failure("Error at assets")
}
}
func showOldDialog(localVideoUrl: URL, vc: UIViewController) {
let video = FBSDKShareVideo()
video.videoURL = localVideoUrl
let content = FBSDKShareVideoContent()
content.video = video
let shareDialog: FBSDKShareDialog = FBSDKShareDialog()
shareDialog.shareContent = content
shareDialog.fromViewController = vc
DispatchQueue.main.async {
shareDialog.show()
}
}
func showOldDialog(asset: PHAsset, vc: UIViewController) {
let video = FBSDKShareVideo()
video.videoAsset = asset
let content = FBSDKShareVideoContent()
content.video = video
let shareDialog: FBSDKShareDialog = FBSDKShareDialog()
shareDialog.shareContent = content
shareDialog.fromViewController = vc
DispatchQueue.main.async {
shareDialog.show()
}
}
func showNewDialog(localVideoUrl: URL, vc: UIViewController, completion: @escaping () -> Void, failure: @escaping (String) -> Void) {
let video = FacebookShare.Video(url: localVideoUrl)
let content = VideoShareContent(video: video)
let shareDialog = ShareDialog(content: content)
shareDialog.mode = .native
shareDialog.failsOnInvalidData = true
shareDialog.presentingViewController = vc
shareDialog.completion = { result in
switch result {
case .success:
print("Share succeeded")
completion()
case .failed:
print(result)
failure("Error while sharing on facebook")
case .cancelled:
print("Share cancelled")
}
}
DispatchQueue.main.async {
do {
try shareDialog.show()
} catch {
print(error)
failure("Error while sharing video content \(error)")
}
}
}