使用 iOS6 社交框架将视频上传到 Facebook

2024-02-25

我想将视频文件发布到 Facebook。之前我使用过Facebook iOS SDK3.0,它可以工作。然而,对于iOS6 Social Framework,存在问题。

 __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'


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

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

现在,需要将参数与多部分数据关联起来,这是棘手的部分。因此有必要使用 source 属性来建立该链接。

源需要字符串格式的 URL,将其设置在参数中,并在多部分请求的文件名字段中设置相同的值。

应该可以做到这一点。

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]];
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

使用 iOS6 社交框架将视频上传到 Facebook 的相关文章

随机推荐