Upload video to Facebook using iOS6 Social Framework

I want to publish a video file to facebook. I used to use Facebook iOS SDK3.0 and it works. However, there is a problem for 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 } }]; 

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

+4
source share
2 answers

Here are my studies: First, the video data cannot be part of the parameter list, as this will cause SLRequest to be invalid and this will fail.

Video data should flow into sections with several parts of the request.

Now you need to associate the parameters with multi-part data, and this is the hard part. Therefore, to create this link, you must use the source attribute.

The source requires a URL in a string format, which sets it in the parameters and sets the same value in the file name field in a multi-page 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]]; 
+5
source

I am working on the same issue. I think your error from ARC and NSData *videoData is removed before returning from performRequestWithHandler .

0
source

Source: https://habr.com/ru/post/1437735/


All Articles