简体   繁体   English

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

[英]upload video to facebook using facebook sdk 3.1 on ios 6.0

One of my app is to upload video to facebook account. 我的一个应用是将视频上传到Facebook帐户。 I checked on web, but found that most of solution are old or removed. 我在网上查了一下,但发现大多数解决方案都是旧的或已删除。 Is there any updated solution? 有没有更新的解决方案?

Welcome any comment 欢迎任何评论

Context 上下文

Before you can publish onto Facebook, you must get publish (write) permissions, using either native integration or the Facebook SDK, the rule is you must first acquire read permissions before write permissions. 在您发布到Facebook之前,您必须获得发布(写入)权限,使用本机集成或Facebook SDK,规则是您必须首先获得写入权限之前的读取权限。

Thus, make sure that before you attempt to upload a video, you should have requested basic info (email for example), then, once you have this, you can request write permissions. 因此,请确保在尝试上传视频之前,您应该已经请求了基本信息(例如电子邮件),然后,一旦您拥有此信息,您就可以请求写入权限。 The permission necessary for uploading videos is publish_stream . 上传视频所需的权限是publish_stream

Using iOS 6 native Facebook integration 使用iOS 6本机Facebook集成

Using the native iOS 6 Facebook integration, you should use the requestForServiceType:requestMethod:URL:parameters: method of SLRequest , as follows: 使用本机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);
    }];
}

Here it's important to note that the video data does not go into the parameters dictionary, it must be added to the SLRequest object using the addMultipartData:withName:type:filename: method. 这里需要注意的是视频数据不会进入参数字典,必须使用addMultipartData:withName:type:filename:方法将其添加到SLRequest对象中。

Also note that the filename is very important when adding the videos data. 另请注意,添加视频数据时,文件名非常重要。 Here I am just using the full path of the file. 这里我只是使用文件的完整路径。

Using Facebook SDK 3.1 library 使用Facebook SDK 3.1库

If you must support iOS versions earlier then iOS 6 or you wish to use the Facebook SDK 3.1 for any other reason, uploading a video is a little different. 如果您必须支持iOS 6之前的iOS版本,或者您希望出于任何其他原因使用Facebook SDK 3.1,则上传视频会略有不同。

You must use a FBRequest object and a NSDictionary that contains the videos details. 您必须使用FBRequest对象和包含视频详细信息的NSDictionary The method I recommend using is requestWithGraphPath:parameters:HTTPMethod: , I've used this method out of preference although you should be able to use some of the other methods to create your request object. 我推荐使用的方法是requestWithGraphPath:parameters:HTTPMethod:我已经使用了这个方法,但你应该能够使用其他一些方法来创建你的请求对象。

The following code works with Facebook SDK 3.1 to upload a video: 以下代码适用于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);
        }];
    }
}

Here as you can see, we add the videos data into the parameters dictionary unlike the previous solution, it's there along with title and description (which are 2 optional parameters). 在这里,您可以看到,我们将视频数据添加到parameters字典中,与之前的解决方案不同,它与titledescription (两个可选参数)一起存在。 Also note that here there is no key source , as specified by the Facebook documentation. 另请注意,这里没有Facebook文档中指定的密钥source The key's name is the filename of the video. 密钥的名称是视频的文件名。 I don't know why this shouldn't be source , but using source results in a com.facebook.sdk error 5 . 我不知道为什么这不应该是source ,但使用source会导致com.facebook.sdk错误5

The bug I mentioned I filed with Facebook, you can see this report on this link - unless I'm mistaken in my interpretation of the documentation. 我提到的我在Facebook上提交的错误,您可以在此链接上看到此报告 - 除非我在解释文档时出错。 Please try out that bug and report if you can reproduce it. 请尝试该bug并报告是否可以重现它。 Thanks ! 谢谢 !

publish_stream不足以上传(读取),您需要请求“ video_upload ”权限。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM