1

我只是想将 Facebook 分享整合到我的应用程序中,我的应用程序的最终产品是一个视频,因此上传并与朋友分享最终编译是我的想法,我正在使用本地社交框架和 ACAccount 框架,让我的应用程序工作Facebook 似乎有一个奇怪的逻辑,我需要在我请求 publish_stream 权限之前请求读取权限,并且他们不能立即(在某处读取),我的问题是我应该在哪里以及如何在我的代码中实现这个逻辑,继承人我的代码

    ACAccountStore* accountStore = [[ACAccountStore alloc] init];
NSDictionary *options = @{
                          ACFacebookAppIdKey: @"My app ID",
                          ACFacebookPermissionsKey: @[@"user_birthday"],
                          ACFacebookAudienceKey: ACFacebookAudienceOnlyMe
                          };
ACAccountType *facebookAccountType = [accountStore
                                      accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierFacebook];
[accountStore requestAccessToAccountsWithType:facebookAccountType options:options completion:^(BOOL granted, NSError *error) {

    if (granted) {

        NSArray *accounts = [accountStore
                             accountsWithAccountType:facebookAccountType];
         ACAccount * facebookAccount = [accounts lastObject];



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

        NSData *videoData = [NSData dataWithContentsOfFile:[_fURL absoluteString]];
        NSLog(@"%@",[_fURL absoluteString]);
        NSLog(@"video size = %d", [videoData length]);
        NSDictionary *params = @{
                                 @"title": @"Me being silly",
                                 @"description": @"Me testing the video upload to Facebook with the new system."
                                 };

        NSURL *requestURL = [NSURL URLWithString:@"https://graph.facebook.com/me/videos"];
        SLRequest *request = [SLRequest requestForServiceType:SLServiceTypeFacebook
                                                requestMethod:SLRequestMethodPOST
                                                          URL:requestURL
                                                   parameters:params];

        [request addMultipartData:videoData
                         withName:@"source"
                             type:@"video/quicktime"
                         filename:[_fURL absoluteString]];
        request.account = facebookAccount;
        [request performRequestWithHandler:^(NSData *data, NSHTTPURLResponse *response,NSError * error){
            NSString *responseString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];

            NSLog(@"response = %@", responseString);
        }];

是的,我缺少发布流权限,但我似乎无法弄清楚我应该把它放在哪里,整个代码都在用户按下按钮时调用的一种方法下

4

1 回答 1

0

这很简单。那是我的实现:

- (void)requestAccessToFacebookAccountWithCompletionHandler:(QCFacebookCommunicatorCompletionHandler)completionHandler {
    [[self accountStore] requestAccessToAccountsWithType:[self accountType] options:[self facebookAccountAccessRequestOptionsForReadStreamPermission] completion:^(BOOL accessForReadingGranted, NSError *error) {
        if (accessForReadingGranted) {
            [[self accountStore] requestAccessToAccountsWithType:[self accountType] options:[self facebookAccountAccessRequestOptionsForPublishStreamPermission] completion:^(BOOL accessForWrittingGranted, NSError *error) {
                if (accessForWrittingGranted) {
                    completionHandler(YES, nil);
                } else {
                    completionHandler(NO, [error userInfo]);
                }
            }];
        } else {
            completionHandler(NO, [error userInfo]);
        }
    }];
}

我只是使用辅助方法来返回带有选项的字典,其他的东西与你的类似。

然后,您可以粘贴负责视频上传的代码而不是 completionHandler 调用,或者您可以使用此实现,并且仅当在 completionHandler中成功 == YES时才上传视频。

希望能帮助到你!

于 2013-06-05T18:59:38.063 回答