我们可以使用 Facebook SDK 将文档目录视频上传到 Facebook
Posted
技术标签:
【中文标题】我们可以使用 Facebook SDK 将文档目录视频上传到 Facebook【英文标题】:Could we upload the documents directory video to facebook using Facebook SDK 【发布时间】:2015-05-26 11:07:15 【问题描述】:我在使用 Facebook SDK 将视频上传到 Facebook 时遇到问题。我正在尝试上传从 SavedPhotos 中选择的视频工作正常。但是当我试图从我的文档目录上传视频时,它会说下面的错误。 我知道我们可以上传具有资产 url 的视频。有没有其他方法可以将文档目录视频上传到facebook???
错误是
2015-05-26 16:30:02.369 graphtwentysixth[3025:1413799] FB: ERROR=Error Domain=com.facebook.sdk.share Code=2 "The operation couldn’t be completed. (com.facebook.sdk.share error 2.)" UserInfo=0x156b0f90 com.facebook.sdk:FBSDKErrorArgumentValueKey=file:///private/var/mobile/Containers/Bundle/Application/48DA75B3-63BA-400A-AC92-BE6B4A2B954B/graphtwentysixth.app/demo-video-high-quality.mov, com.facebook.sdk:FBSDKErrorArgumentNameKey=videoURL, com.facebook.sdk:FBSDKErrorDeveloperMessageKey=Invalid value for videoURL: file:///private/var/mobile/Containers/Bundle/Application/48DA75B3-63BA-400A-AC92-BE6B4A2B954B/graphtwentysixth.app/demo-video-high-quality.mov
代码
NSURL *videoURL = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"demo-video-high-quality" ofType:@"mov"]];
FBSDKShareVideo *video = [[FBSDKShareVideo alloc] init];
video.videoURL = videoURL;
FBSDKShareVideoContent *content1 = [[FBSDKShareVideoContent alloc] init];
content1.video = video;
[FBSDKShareAPI shareWithContent:content1 delegate:self];
感谢您宝贵的时间
【问题讨论】:
您是否仔细检查了此文件是否在路径上退出,是否file:///private/var/mobile/Containers/Bundle/Application/48DA75B3-63BA-400A-AC92-BE6B4A2B954B/graphtwentysixth.app/demo-video-high-quality.mov
是的,我已经检查过了。它是存在的
你能做一件事吗?只需 NSLog 视频路径进入你的项目,让我知道两者是否相同。
你检查过那个网址吗?
你能把你的代码贴在这里吗?
【参考方案1】:
无法从文档目录上传视频。您可以通过将视频设为资产并将资产的 url 提供给 Facebook 并在完成处理程序调用时从图库中删除该视频资产来实现这一点。这是一个技巧,但不是一个好的解决方案,因为当您将视频作为厨房资产时,它将在 savedPhotos 中可见。
【讨论】:
感谢您的想法,但从上一小时开始,我试图将视频 url 更改为资产 url。你能帮帮我吗?? 是的,您可以使用图形 API 从您的目录上传视频,但您需要图形 API 的权限才能在用户的墙上发布视频 您不能直接将视频网址制作为资产您的资产,您需要将该视频复制到您的资产库中,而不是将该资产网址传递给 Facebook 你甚至不能使用graph API来上传文档目录中保存的视频。请参阅此处并搜索标题为“共享照片和视频”的部分:developers.facebook.com/docs/ios/graph 看来资产问题没有办法解决。【参考方案2】:就像 Shoaib 所说,您需要先将视频制作成资产。请务必在您的课程中加入#import <AssetsLibrary/AssetsLibrary.h>
。
ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
ALAssetsLibraryWriteVideoCompletionBlock videoWriteCompletionBlock = ^(NSURL *newURL, NSError *error)
if (error)
NSLog( @"Error writing image with metadata to Photo Library: %@", error );
else
NSLog(@"Wrote image with metadata to Photo Library %@", newURL.absoluteString);
FBSDKShareVideo* video = [FBSDKShareVideo videoWithVideoURL:newURL];
FBSDKShareVideoContent* content = [[FBSDKShareVideoContent alloc] init];
content.video = video;
[FBSDKShareAPI shareWithContent:content delegate:self];
;
NSURL *videoURL = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"demo-video-high-quality" ofType:@"mov"]];
if ([library videoAtPathIsCompatibleWithSavedPhotosAlbum:videoURL])
[library writeVideoAtPathToSavedPhotosAlbum:videoURL completionBlock:videoWriteCompletionBlock];
(编辑部分灵感来自this post)
【讨论】:
这是生成的新网址!!assets-library://asset/asset.mp4?id=E121FF88-B0AB-4EE9-BD60-E676617BDC54&ext=mp4
Same Error The operation couldn’t be completed. (com.facebook.sdk.share error 2.)
【参考方案3】:
您应该首先将视频保存到您的库中,然后获取正确的 PHAsset 并生成正确的 URL:
guard let schemaUrl = URL(string: "fb://") else
return //be safe
if UIApplication.shared.canOpenURL(schemaUrl)
phphotoLibrary.requestAuthorization( [weak self]
(newStatus) in
guard let strongSelf = self else
return
if newStatus == PHAuthorizationStatus.authorized
strongSelf.saveVideoToCameraRoll(url: assetURL, completion: (result, phAsset) in
phAsset?.getURL(completionHandler: (url) in
if let url = url
dispatchAsyncOnMainQueue
let video = FBSDKShareVideo()
video.videoURL = url
let content = FBSDKShareVideoContent()
content.video = video
dialog.shareContent = content
dialog.show()
)
)
else
//unauthorized
)
else
//facebookAppNotInstalled
...
func saveVideoToCameraRoll(url: URL, completion:@escaping (Bool, PHAsset?) -> ())
PHPhotoLibrary.shared().performChanges(
PHAssetChangeRequest.creationRequestForAssetFromVideo(atFileURL: url)
) saved, error in
if saved
let fetchOptions = PHFetchOptions()
fetchOptions.sortDescriptors = [NSSortDescriptor(key: "creationDate", ascending: false)]
fetchOptions.fetchLimit = 1
let fetchResult = PHAsset.fetchAssets(with: .video, options: fetchOptions).firstObject
completion(true, fetchResult)
else
completion(false, nil)
...
extension PHAsset
func getURL(completionHandler : @escaping ((_ responseURL : URL?) -> Void))
if self.mediaType == .video
let options: PHVideoRequestOptions = PHVideoRequestOptions()
options.version = .original
let nameParts = self.localIdentifier.components(separatedBy: "/")
if nameParts.count > 0
let assetFormatString = "assets-library://asset/asset.MP4?id=%@&ext=MP4"
let name = nameParts[0]
let urlString = String(format: assetFormatString, name)
if let url = URL(string: urlString)
completionHandler(url)
else
completionHandler(nil)
【讨论】:
【参考方案4】:你试试下面的代码可能对你有帮助。
- (void)upload
if (FBSession.activeSession.isOpen)
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"demo-video-high-quality" ofType:@"mov"];
NSURL *pathURL = [[NSURL alloc]initFileURLWithPath:filePath isDirectory:NO];
NSData *videoData = [NSData dataWithContentsOfFile:filePath];
NSDictionary *videoObject = @
@"title": @"FB SDK 3.1",
@"description": @"hello there !",
[pathURL absoluteString]: videoData
;
FBRequest *uploadRequest = [FBRequest requestWithGraphPath:@"me/videos"
parameters:videoObject
HTTPMethod:@"POST"];
[uploadRequest startWithCompletionHandler:^(FBRequestConnection *connection, id result, NSError *error)
if (!error)
NSLog(@"Done: %@", result);
else
NSLog(@"Error: %@", error.localizedDescription);
];
【讨论】:
这是一个无关紧要的答案,因为它使用的是旧的 Facebook SDK。以上是关于我们可以使用 Facebook SDK 将文档目录视频上传到 Facebook的主要内容,如果未能解决你的问题,请参考以下文章