4

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

        }

    }];

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

Rohit Vipin Mathews
  • 11,629
  • 15
  • 57
  • 112
alex
  • 53
  • 1
  • 4

2 Answers2

5

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.

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.

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.

That should do it.

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]];
Daniel
  • 23,129
  • 12
  • 109
  • 154
Luis
  • 66
  • 2
  • You are right. It should not be part of the parameters. I have tried your suggestions and it works. – alex Oct 09 '12 at 06:35
  • I changed a few things in your code snippet, the MultipartData needs it's name to match Facebook's requirements, this should be `source`. Also the parameters had an irrelevant "name" property, should be `title` and `description` which are both optional. – Daniel Oct 12 '12 at 20:14
  • Is it possible to create this using the SDK? – Iulian Onofrei Nov 12 '15 at 12:45
0

I'm working the same issue. I think your error is from ARC and NSData *videoData gets deleted before the return from performRequestWithHandler.

Andrei Chevozerov
  • 1,029
  • 1
  • 10
  • 24
Kevin
  • 1
  • So you are able to upload successfully ? ok lemme try to a way to retain video data then. – alex Oct 08 '12 at 05:59
  • I tried shifting the videoData outside to be global and still encountered the same error. – alex Oct 09 '12 at 06:25