上传video到facebook使用Facebook的SDK 3.1在iOS 6.0上

我的一个应用程序是上传video到Facebook帐户。 我在网上查了一下,但发现大部分解决scheme都是旧的或删除的。 有没有更新的解决scheme?

欢迎任何评论

上下文

在发布到Facebook之前,您必须使用本机集成或Facebook SDK获得发布(写入)权限,该规则是在写入权限之前,您必须先获取读取权限。

因此,请确保在您尝试上传video之前,您应该已经请求了基本信息(例如电子邮件),然后,一旦拥有此信息,您可以请求写入权限。 上传video所需的权限是publish_stream

使用iOS 6原生Facebook集成

使用本机iOS 6 Facebook集成,您应该使用requestForServiceType:requestMethod:URL:parameters: SLRequest方法,如下所示:

 - (void)upload{ NSURL *videourl = [NSURL URLWithString:@"https://graph.facebook.com/me/videos"]; NSString *filePath = [[NSBundle mainBundle] pathForResource:@"me" ofType:@"mov"]; NSURL *pathURL = [[NSURL alloc]initFileURLWithPath:filePath isDirectory:NO]; NSData *videoData = [NSData dataWithContentsOfFile:filePath]; NSDictionary *params = @{ @"title": @"Me being silly", @"description": @"Me testing the video upload to Facebook with the new system." }; SLRequest *uploadRequest = [SLRequest requestForServiceType:SLServiceTypeFacebook requestMethod:SLRequestMethodPOST URL:videourl parameters:params]; [uploadRequest addMultipartData:videoData withName:@"source" type:@"video/quicktime" filename:[pathURL absoluteString]]; uploadRequest.account = self.facebookAccount; [uploadRequest performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error) { NSString *responseString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding]; if(error){ NSLog(@"Error %@", error.localizedDescription); }else NSLog(@"%@", responseString); }]; } 

这里需要注意的是,video数据不会进入参数字典,它必须使用addMultipartData:withName:type:filename:方法添加到SLRequest对象中。

另外请注意,添加video数据时,文件名非常重要。 这里我只是使用文件的完整path。

使用Facebook SDK 3.1库

如果您之前必须支持iOS版本,那么iOS 6或者您希望使用Facebook SDK 3.1的任何其他原因,则上传video会有所不同。

您必须使用FBRequest对象和包含video详细信息的NSDictionary 。 我推荐使用的方法是requestWithGraphPath:parameters:HTTPMethod:我已经使用了这个方法,虽然你应该可以使用其他一些方法来创build你的请求对象。

以下代码与Facebook SDK 3.1一起上传video:

 - (void)upload{ if (FBSession.activeSession.isOpen) { NSString *filePath = [[NSBundle mainBundle] pathForResource:@"me" 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); }]; } } 

在这里您可以看到,我们将video数据添加到parameters字典中,与之前的解决scheme不同,它与titledescription (这是两个可选参数)一起存在。 另外请注意,这里没有关键的source ,正如Facebook文档所指定的。 密钥的名称是video的文件名。 我不知道为什么这不应该是source ,但使用源结果com.facebook.sdk错误5

我提到的我提交给Facebook的错误,你可以在这个链接上看到这个报告 – 除非我错误地解释了我的文档。 请尝试这个错误,并报告,如果你能重现它。 谢谢 !

publish_stream是不够上传(读取),你需要要求“ video_upload ”权限。

这个网站有很多很棒的API参考资料,特定的一个只是用于Facebook检查,并检查你的语法。

http://developer.coronalabs.com/reference