使用iOS6社交框架将视频上传到Facebook [英] upload Video to Facebook using iOS6 Social Framework

查看:156
本文介绍了使用iOS6社交框架将视频上传到Facebook的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将视频文件发布到Facebook。以前我使用的是Facebook iOS SDK3.0,它可以工作。但是,对于iOS6 Social Framework,有问题。

I want to publish a video file to facebook. Previously I used the Facebook iOS SDK3.0 and it works. However, for iOS6 Social Framework, there is problem.

 __block ACAccount * facebookAccount;
    ACAccountStore* accountStore = [[ACAccountStore alloc] init];
    NSDictionary *options = @{
    ACFacebookAppIdKey: @"MY APP ID",
    ACFacebookPermissionsKey: @[@"publish_actions", ], 
    @"ACFacebookAudienceKey": ACFacebookAudienceFriends
    };
    ACAccountType *facebookAccountType = [accountStore
                                          accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierFacebook];
    [accountStore requestAccessToAccountsWithType:facebookAccountType options:options completion:^(BOOL granted, NSError *error) {

        if (granted) {
            NSArray *accounts = [accountStore
                                 accountsWithAccountType:facebookAccountType];
            facebookAccount = [accounts lastObject];



            NSLog(@"access to facebook account ok %@", facebookAccount.username);

            NSData *videoData = [NSData dataWithContentsOfFile:[self videoFileFullPath]];
            NSLog(@"video size = %d", [videoData length]);
            NSDictionary *params = [NSDictionary dictionaryWithObjectsAndKeys:
                                     videoData, @"video.mov",
                                    @"video/quicktime", @"contentType" ,
                                    @"Video title", @"title",
                                    @"Video description", @"description",nil];

            NSURL *requestURL = [NSURL URLWithString:@"https://graph.facebook.com/me/videos"];
            SLRequest *request = [SLRequest requestForServiceType:SLServiceTypeFacebook
                                                                         requestMethod:SLRequestMethodPOST
                                                                                   URL:requestURL
                                                                            parameters:params];
            request.account = facebookAccount;
            [request performRequestWithHandler:^(NSData *data,                                                                  NSHTTPURLResponse *response,NSError * error){
                NSLog(@"response = %@", response);
                NSLog(@"error = %@", [error localizedDescription]);

            }];


        } else {
            NSLog(@"access to facebook is not granted");
            // extra handling here if necesary

        }

    }];




由于未捕获的异常终止应用程序
'NSInvalidArgumentException'原因:' - [NSConcreteData
_fastCharacterContents]:无法识别的选择器发送到实例0x2097ead0'

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSConcreteData _fastCharacterContents]: unrecognized selector sent to instance 0x2097ead0'


推荐答案

这是我的研究:
首先,视频数据不能是参数列表的一部分,因为它会使SLRequest无效,这是您遇到的崩溃。

Here is my research: First, the video data cannot be part of the parameter list, since it will render the SLRequest invalid and that is the crash you are experiencing.

视频数据必须在请求的多部分中。

The video data must go in the multi part section of the request.

现在,需要将参数与多部分数据相关联,这是棘手的一部分。所以有必要使用source属性来创建该链接。

Now,there is a need to associate the parameters with the multipart data and that is the tricky part. So it is necessary to use the source attribute to make that link.

源需要一个字符串格式的URL,在参数中设置它,并在文件名中设置相同的值

Source requires a URL in a string format set it in the parameters and set the same value in the filename field in the multipart request.

应该这样做。

NSURL *url = [NSURL URLWithString:@"https://graph.facebook.com/me/videos"];

NSURL *videoPathURL = [[NSURL alloc]initFileURLWithPath:videoPath isDirectory:NO];
NSData *videoData = [NSData dataWithContentsOfFile:videoPath];

NSString *status = @"One step closer.";
NSDictionary *params = @{@"title":status, @"description":status};

SLRequest *request = [SLRequest requestForServiceType:SLServiceTypeFacebook
                                        requestMethod:SLRequestMethodPOST 
                                                  URL:url 
                                           parameters:params];

[request addMultipartData:videoData
                 withName:@"source"
                     type:@"video/quicktime" 
                 filename:[videoPathURL absoluteString]];

这篇关于使用iOS6社交框架将视频上传到Facebook的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆