使用facebook sdk 3.1在ios 6.0上将视频上传到Facebook

ara*_*ide 5 api facebook objective-c ios ios6

我的一个应用是将视频上传到Facebook帐户.我在网上查了一下,但发现大多数解决方案都是旧的或已删除.有没有更新的解决方案?

欢迎任何评论

Dan*_*iel 20

上下文

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

因此,请确保在尝试上传视频之前,您应该已经请求了基本信息(例如电子邮件),然后,一旦您拥有此信息,您就可以请求写入权限.上传视频所需的许可是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);
    }];
}
Run Code Online (Sandbox Code Playgroud)

这里重要的是要注意视频数据不会进入参数字典,必须SLRequest使用该addMultipartData:withName:type:filename:方法将其添加到对象中.

另请注意,添加视频数据时,文件名非常重要.这里我只是使用文件的完整路径.

使用Facebook SDK 3.1库

如果您必须支持iOS 6之前的iOS版本,或者您希望出于任何其他原因使用Facebook SDK 3.1,则上传视频会略有不同.

您必须使用包含视频详细信息的FBRequest对象NSDictionary.我推荐使用的方法是requestWithGraphPath:parameters:HTTPMethod:,虽然您应该能够使用其他一些方法来创建请求对象,但我已经使用了这种方法.

以下代码适用于Facebook SDK 3.1上传视频:

- (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);
        }];
    }
}
Run Code Online (Sandbox Code Playgroud)

在这里,正如您所看到的,我们将视频数据添加到parameters字典中,与以前的解决方案不同,它titledescription(和2个可选参数)一起存在.另请注意,这里没有密钥source,正如Facebook文档所指定的那样.密钥的名称是视频的文件名.我不知道为什么这不应该source,但使用source会导致com.facebook.sdk错误5.

我提到的我在Facebook上提交的错误,您可以在此链接上看到此报告- 除非我在解释文档时出错.请尝试该bug并报告是否可以重现它.谢谢 !