如何在Facebook上上传/分享video?

我正在制作一个testing应用程序,我想在Facebook上发布video。 我正在使用最新的Facebook的SDK。 但我无法在Facebook上发布。

我的代码如下。

NSDictionary *parameters = [NSDictionary dictionaryWithObject:videoData forKey:@"CareAppDemo.mov"]; FBRequest *request = [FBRequest requestWithGraphPath:@"me/videos" parameters:parameters HTTPMethod:@"POST"]; [request startWithCompletionHandler:^(FBRequestConnection *connection, id result, NSError *error) { NSLog(@"result: %@, error: %@", result, error); }]; 

请帮我通过我的应用程序在Facebook上发布video。

成功testing在FaceBook SDK 3.14.1

build议: .plist文件中有3个属性

设置FacebookAppID,FacebookDisplayName,
URLtypes – > Item 0-> URL Schemes设置为带有fb facebookappId前缀请参阅

 -(void)shareOnFaceBook { //sample_video.mov is the name of file NSString *filePathOfVideo = [[NSBundle mainBundle] pathForResource:@"sample_video" ofType:@"mov"]; NSLog(@"Path Of Video is %@", filePathOfVideo); NSData *videoData = [NSData dataWithContentsOfFile:filePathOfVideo]; //you can use dataWithContentsOfURL if you have a Url of video file //NSData *videoData = [NSData dataWithContentsOfURL:shareURL]; //NSLog(@"data is :%@",videoData); NSMutableDictionary *params = [NSMutableDictionary dictionaryWithObjectsAndKeys: videoData, @"video.mov", @"video/quicktime", @"contentType", @"Video name ", @"name", @"description of Video", @"description", nil]; if (FBSession.activeSession.isOpen) { [FBRequestConnection startWithGraphPath:@"me/videos" parameters:params HTTPMethod:@"POST" completionHandler:^(FBRequestConnection *connection, id result, NSError *error) { if(!error) { NSLog(@"RESULT: %@", result); [self throwAlertWithTitle:@"Success" message:@"Video uploaded"]; } else { NSLog(@"ERROR: %@", error.localizedDescription); [self throwAlertWithTitle:@"Denied" message:@"Try Again"]; } }]; } else { NSArray *permissions = [[NSArray alloc] initWithObjects: @"publish_actions", nil]; // OPEN Session! [FBSession openActiveSessionWithPublishPermissions:permissions defaultAudience:FBSessionDefaultAudienceEveryone allowLoginUI:YES completionHandler:^(FBSession *session, FBSessionState status, NSError *error) { if (error) { NSLog(@"Login fail :%@",error); } else if (FB_ISSESSIONOPENWITHSTATE(status)) { [FBRequestConnection startWithGraphPath:@"me/videos" parameters:params HTTPMethod:@"POST" completionHandler:^(FBRequestConnection *connection, id result, NSError *error) { if(!error) { [self throwAlertWithTitle:@"Success" message:@"Video uploaded"]; NSLog(@"RESULT: %@", result); } else { [self throwAlertWithTitle:@"Denied" message:@"Try Again"]; NSLog(@"ERROR: %@", error.localizedDescription); } }]; } }]; } } 

我GOT错误在第一次的应用程序运行:

  The operation couldn't be completed. (com.facebook.sdk error 5.) 

当facebook被inited时发生。 下一次我打开我的应用程序,它工作正常,它总是第一次。 尝试了应用程序中的所有内容,但似乎在Facebook SDK一侧。

几个原因看到com.facebook.sdk error 5

  • 会议是不开放的。 validation。
  • Facebook已经检测到您正在发送垃圾邮件。 更改video名称。
  • Facebook使用SDK定义了一个限制。 尝试不同的应用程序。
  • 错误的发布权限。 发布publish_actions
  • 还有很多….

您需要先下载FacebookSDK,然后将以下框架添加到您的项目中

 FacebookSDK.framework, FBSDKLoginKit.framework, FBSDKShareKit.framework, Bolts.framework,FBSDKCoreKit.framework 

导入它们,然后编写下面的代码

 if(![FBSDKAccessToken currentAccessToken]) { FBSDKLoginManager *login1 = [[FBSDKLoginManager alloc]init]; [login1 logInWithPublishPermissions:@[@"publish_actions"] handler:^(FBSDKLoginManagerLoginResult *result, NSError *error) { FBSDKShareVideo *video = [[FBSDKShareVideo alloc] init]; video.videoURL = videoAssetURL; FBSDKShareVideoContent *content = [[FBSDKShareVideoContent alloc] init]; content.video = video; [FBSDKShareDialog showFromViewController:self withContent:content delegate:nil]; }]; } else { FBSDKShareVideo *video = [[FBSDKShareVideo alloc] init]; video.videoURL = videoAssetURL; FBSDKShareVideoContent *content = [[FBSDKShareVideoContent alloc] init]; content.video = video; [FBSDKShareDialog showFromViewController:self withContent:content delegate:nil]; } 

videourlvideoURL必须是资源url。 您可以从UIImagePickerController获取video资源URL。

或录制video,你可以采取如下

 ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init]; [library writeVideoAtPathToSavedPhotosAlbum:[NSURL URLWithString:[[NSHomeDirectory() stringByAppendingPathComponent:@"Library/Caches"] stringByAppendingFormat:@"/current.mov"]] completionBlock:^(NSURL *assetURL, NSError *error) { videoAssetURL =assetURL; }]; 

有关更多详细信息,可以使用https://developers.facebook.com/docs/sharing/ios

获取发布权限

 NSArray* permissions = [[NSArray alloc] initWithObjects: @"publish_stream", nil]; [facebook authorize:permissions delegate:self]; [permissions release]; 

尝试这个

 - (void)fbDidLogin { NSString *filePath = [[NSBundle mainBundle] pathForResource:@"sample" ofType:@"mov"]; NSData *videoData = [NSData dataWithContentsOfFile:filePath]; NSMutableDictionary *params = [NSMutableDictionary dictionaryWithObjectsAndKeys: videoData, @"video.mov", @"video/quicktime", @"contentType", @"Video Test Title", @"title", @"Video Test Description", @"description", nil]; [facebook requestWithGraphPath:@"me/videos" andParams:params andHttpMethod:@"POST" andDelegate:self]; } 

在Facebook上上传video的最佳示例检查它

从下面的链接下载这只是iPhone

在这里输入图像说明

这是旧的线程,但对于所有未来的读者来说,这里是如何使用当前最新的Facebook SDK( v3.24.0 - September 10th 2015 )做到这一点。

 - (IBAction)bntShareOnFacebookAction:(id)sender { if ([[FBSDKAccessToken currentAccessToken] hasGranted:@"publish_actions"]) { [self shareVideoOnFacebook]; } else { FBSDKLoginManager *loginManager = [[FBSDKLoginManager alloc] init]; [loginManager logOut]; //very important line for login to work [loginManager logInWithPublishPermissions:@[@"publish_actions"] handler:^(FBSDKLoginManagerLoginResult *result, NSError *error) { if(!error) { [self shareVideoOnFacebook]; } }]; } } - (void) shareVideoOnFacebook { NSString *videoPath = @"/Documents/.../movie.mov"; NSData *videoData = [NSData dataWithContentsOfURL:[NSURL fileURLWithPath:videoPath]]; NSMutableDictionary *params = [NSMutableDictionary dictionaryWithCapacity:3L]; [params setObject:videoData forKey:@"video_filename.MOV"]; [params setObject:@"Title for this post." forKey:@"title"]; [params setObject:@"Description for this post." forKey:@"description"]; [[[FBSDKGraphRequest alloc] initWithGraphPath:@"/me/videos" parameters:params HTTPMethod:@"POST"] startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection, id result, NSError *error) { if (!error) { //video posted } }]; } 

如果您要上传/分享video,则必须传递所选项目的原始版本的资产库URL。

 URL eg assets-library://asset/asset.MOV?id=18BC70A0-208A-4F03-A207-7D57C8863425&ext=MOV 

如果你正在使用

 - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info 

那么你必须通过

 NSURL *url = [info objectForKey:UIImagePickerControllerReferenceURL]; 

如果您使用文档目录path,那么首先您需要将video保存到库中。 您必须创buildURL链接资源库URL。

 [[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{ [PHAssetChangeRequest creationRequestForAssetFromVideoAtFileURL:videoURL]; } completionHandler:^(BOOL success, NSError * _Nullable error) { if (success) { // Fetch Last saved video. PHFetchOptions *fetchOptions = [[PHFetchOptions alloc] init]; fetchOptions.sortDescriptors = @[[NSSortDescriptor sortDescriptorWithKey:@"creationDate" ascending:false]]; // Get video url. PHAsset *fetchResult = [PHAsset fetchAssetsWithMediaType:PHAssetMediaTypeVideo options:fetchOptions].firstObject; NSString *assetID = [fetchResult.localIdentifier substringToIndex:(fetchResult.localIdentifier.length - 7)]; NSURL *assetURL = [NSURL URLWithString:[NSString stringWithFormat:@"assets-library://asset/asset.MOV?id=%@&ext=MOV", assetID]]; // Share Video. FBSDKShareVideo *video = [[FBSDKShareVideo alloc] init]; video.videoURL = assetURL; FBSDKShareVideoContent *content = [[FBSDKShareVideoContent alloc] init]; content.video = video; FBSDKShareDialog *shareDialog = [[FBSDKShareDialog alloc] init]; shareDialog.shareContent = content; shareDialog.delegate = (id)self; shareDialog.fromViewController = self; NSError * error = nil; BOOL validation = [shareDialog validateWithError:&error]; if (validation) { dispatch_async(dispatch_get_main_queue(), ^{ [shareDialog show]; }); } else { NSLog(@"%@", error.localizedDescription); } } }];