upload Video to Facebook using iOS6 Social Framework - facebook

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'

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]];

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

Related

how to invite facebook friend in ios7?

am new in ios development
when i click "find your facebook friend" button then invitaion goes to this my salected friend
how can i code for this.am using facebook sdk framework.
NSLog(#"ID=%#",[_IDArray lastObject]);
NSLog(#"Event ID:%#",_eventID);
NSData *data = [_eventID dataUsingEncoding:NSUTF8StringEncoding];
id json = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
NSLog(#"Event ID:%#",[json objectForKey:#"id"]);
NSURL *meurl = [NSURL URLWithString:[NSString stringWithFormat:#"https://graph.facebook.com/%#/invited/%#",[json objectForKey:#"id"],[_IDArray lastObject]]];
//#"https://graph.facebook.com/EVENT_ID/invited?users=USER_ID1,USER_ID2,USER_ID3"
SLRequest *merequest = [SLRequest requestForServiceType:SLServiceTypeFacebook
requestMethod:SLRequestMethodPOST
URL:meurl
parameters:nil];
merequest.account = _facebookAccount;
[merequest performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error) {
NSString *meDataString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
NSLog(#"Data=%#",meDataString);
}];
}
}
but i want to use facebook sdk
please help out
Facebook have shared the way to send request and invitation as:Facebook tutorial friend requests

is it possible to tag a post/photo using ios social framework?

In my app I need to tag my photo/posts. Is this possible using social framework or Should I use Facebook-sdk for this?. Also can I get friends list from social framework? any help to achieve this?
Tagging a photo or post can only be done using the FacebookSDK for iOS while getting your friends list is possible using both the SocialFramework and the FacebookSDK for iOS.
You can use this code to get your friends list using the SocialFramework.
- (void)getFbFriends
{
ACAccountStore *store = [[ACAccountStore alloc] init];
ACAccountType *facebookTypeAccount = [store accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierFacebook];
NSDictionary *options = #{
ACFacebookAppIdKey: facebookAppId,
ACFacebookPermissionsKey: #[#"email"],
ACFacebookAudienceKey: ACFacebookAudienceFriends
};
[store requestAccessToAccountsWithType:facebookTypeAccount
options:options
completion:^(BOOL granted, NSError *error){
if(granted)
{
NSArray *accounts = [store accountsWithAccountType:facebookTypeAccount];
ACAccount *facebookAccount = [accounts lastObject];
NSURL *meurl = [NSURL URLWithString:#"https://graph.facebook.com/me"];
SLRequest *merequest = [SLRequest requestForServiceType:SLServiceTypeFacebook
requestMethod:SLRequestMethodGET
URL:meurl
parameters:nil];
merequest.account = facebookAccount;
NSURL *requestURL = [NSURL URLWithString:#"https://graph.facebook.com/me/friends"];
SLRequest *request = [SLRequest requestForServiceType:SLServiceTypeFacebook
requestMethod:SLRequestMethodGET
URL:requestURL parameters:#{#"fields":#"id,name,picture,first_name,last_name,gender"}];
request.account = facebookAccount;
[request performRequestWithHandler:^(NSData *data,
NSHTTPURLResponse *response,
NSError *error) {
if(!error)
{
NSDictionary *list =[NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];
if([list objectForKey:#"error"]!=nil)
{
// if error occured e.g. Invalid Auth etc.
}
else
{
NSMutableArray *FBFriends = [[NSMutableArray alloc] init];
[FBFriends addObject:[userInfo objectForKey:#"id"]];
NSLog(#"friends %#", list);
}
}
else
{
NSLog(#"Error");
}
}];
}
else
{
NSLog(#"Error grant access");
}
}];
}

how to get read permission before publishing permission for uploading video on Facebook using SLRequest?

I am simply trying to integrate Facebook sharing into my app, my app's end product is a video hence uploading and sharing the final compilation with friends is what i have in mind, I am using native social framework with ACAccount Framework, for my app to work there seems to be weird logic from Facebook that i need to ask for read permission before i ask for publish_stream permission and that they cant be immediately after(read somewhere), my question is where and how should i implement this logic in my code, heres my code
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);
}];
Yes I am missing publish stream permission but i cant seem to figure out where should i put it, this whole code is under one method which is called when user presses a button
It is very simple. That is my implementation of it:
- (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]);
}
}];
}
I just use a helper methods to return dictionaries with options, other stuff is similar to yours.
Then, you can paste your code that is responsible for video uploading instead of the completionHandler calls, or you can use this implementation, and upload video only if the success == YES in completionHandler.
Hope it helps!

I get the Malformed access token for Facebook in ios 6

I want to fetch the facebook friends profile picture.I also give the access_token for it.
I search a lot on net but didn't got any good solution.
my code is as follow:
- (IBAction)advancedButtonPressed:(id)sender {
self.accountStore = [[ACAccountStore alloc]init];
ACAccountType *FBaccountType= [self.accountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierFacebook];
//NSString *key = #"xxxxxx";
NSString *key = #"xxxxxx";
NSDictionary *dictFB = [NSDictionary dictionaryWithObjectsAndKeys:key,ACFacebookAppIdKey,#[#"email"],ACFacebookPermissionsKey, nil];
[self.accountStore requestAccessToAccountsWithType:FBaccountType options:dictFB completion:
^(BOOL granted, NSError *e) {
if (granted) {
NSArray *accounts = [self.accountStore accountsWithAccountType:FBaccountType];
//it will always be the last object with single sign on
self.facebookAccount = [accounts lastObject];
NSLog(#"facebook account =%#",self.facebookAccount);
ACAccountCredential *fbCredential = [self.facebookAccount credential];
accessToken = [fbCredential oauthToken];
NSLog(#"Facebook Access Token: %#", accessToken);
[self get];
} else {
//Fail gracefully...
NSLog(#"error getting permission %#",e);
}
}];
}
-(void)fr
{
NSURL *requestURL = [NSURL URLWithString:#"https://graph.facebook.com/me/friends?fields=id,name,picture,first_name,last_name,gender&access_token=BAAFcRQZCZAUeUBAICacM2q1V5UzRHee6xHGnbEdu19v6CZBIEDJmTwHBKtMZBwtVuFiEeuXqt4yKeSkeI17QZBGaZCeszBfOKdbZAfR4E9RN2nFqHalKPXZBga96WQCU8CSNG4ZCJCK1V7JIbqZAPRgNl6mjjO4Ns1CEaLpbXUYtum1CXbNqTtT82YpVVKoZCmMEBpqHfwYMx1OKu9RZBsjPZA6DlhKA1uGyU32EGJ8QOQZASJVQZDZD"];
SLRequest *request = [SLRequest requestForServiceType:SLServiceTypeFacebook
requestMethod:SLRequestMethodGET
URL:requestURL
parameters:nil];
request.account = self.facebookAccount;
[request performRequestWithHandler:^(NSData *data,
NSHTTPURLResponse *response,
NSError *error) {
if(!error)
{
list =[NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];
NSLog(#"Dictionary contains data: %#", list );
if([list objectForKey:#"error"]!=nil)
{
[self attemptRenewCredentials];
}
dispatch_async(dispatch_get_main_queue(),^{
nameLabel.text = [list objectForKey:#"username"];
});
}
else{
//handle error gracefully
NSLog(#"error from get%#",error);
//attempt to revalidate credentials
}
}];
}
But i always got the error like this:
error = {
code = 190;
message = "Malformed access token BAAFcRQZCZAUeUBAICacM2q1V5UzRHee6xHGnbEdu19v6CZBIEDJmTwHBKtMZBwtVuFiEeuXqt4yKeSkeI17QZBGaZCeszBfOKdbZAfR4E9RN2nFqHalKPXZBga96WQCU8CSNG4ZCJCK1V7JIbqZAPRgNl6mjjO4Ns1CEaLpbXUYtum1CXbNqTtT82YpVVKoZCmMEBpqHfwYMx1OKu9RZBsjPZA6DlhKA1uGyU32EGJ8QOQZASJVQZDZD?access_token=BAAFcRQZCZAUeUBAHBZBSdTL9wWFDOVthxj78bBJlrNEkPiKHdxZA1HaRmO9dtBTJdsZCtZCu2vaE5ByZAdK7Ox30BpIR0KTg0ZC6pi1IOE0KnZCZBSxqbycG0C6Ws5Ct4ferL3G0VU1wIfDypKekFM3zB4r5gAiUREQ66Yuz2Fu2mp5NGqZCBE1p357H41P2lKOAQN9EZAmu4q3SRtjMuVIw4dtdC00l4RhMuDWTOIUQlO54bgZDZD";
type = OAuthException;
};
}
I can't able to solve this. Help me!
If you're using an SLRequest, it will auto append the access token for you, and you also should not have any request params in the URL itself. Instead, you should do something like:
NSURL *requestURL = [NSURL URLWithString:#"https://graph.facebook.com/me/friends"];
SLRequest *request = [SLRequest requestForServiceType:SLServiceTypeFacebook
requestMethod:SLRequestMethodGET
URL:requestURL
parameters:#{#"fields":#"id,name,picture,first_name,last_name,gender"}];
You can also use this instead of using SLRequest.It is also
NSURL *meurl = [NSURL URLWithString:[NSString stringWithFormat:#"https://graph.facebook.com/me?access_token=%#",fbAccessToken]];
NSURLRequest *req = [[NSURLRequest alloc]initWithURL:meurl];
[NSURLConnection sendAsynchronousRequest:req queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
NSString *userDataString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
}];

Share an image on Facebook using App ID

I want to share my image on the facebook using my AppID. So I can see it on facebook as via App. Its working fine till iOS 5. But not in iOS 6.
If I'm using SLComposeViewController, so I can share image but is displays 'via iOS' instead of 'via App'. So I've reviewed code for it and applied, but it's not sharing the image. Please check the code for any issues and correct it.
facebookaccount = [[ACAccountStore alloc] init];
ACAccountType *facebookaccountType = [facebookaccount accountTypeWithAccountTypeIdentifier: ACAccountTypeIdentifierFacebook];
// Specify App ID and permissions
NSDictionary *options = #{ ACFacebookAppIdKey: #"xxxxxxxxxxxxx", ACFacebookPermissionsKey: #[#"publish_stream"], ACFacebookAudienceKey: ACFacebookAudienceFriends };
[facebookaccount requestAccessToAccountsWithType:facebookaccountType options:options completion:^(BOOL granted, NSError *error) {
if (granted) {
NSArray *accountsArray = [facebookaccount accountsWithAccountType:facebookaccountType];
facebookAccount = [[accountsArray lastObject] retain];
NSLog(#" Account :::: %#", facebookAccount);
} else {
NSLog(#" Error :::: %#", error.description);
}}];
NSDictionary *parameters = #{#"message" : #""};
NSString *link = [NSString stringWithFormat:#"%simages/%#",ServerPath,[images objectAtIndex:imageNO]];
NSURL *url = [NSURL URLWithString:link];
NSData *data = [NSData dataWithContentsOfURL:url];
CGFloat compression = 1.0f;
CGFloat maxCompression = 0.0f;
int maxFileSize = 150*224;
UIImage *img1 = [[UIImage alloc] initWithData:data];
NSData *imageData = UIImageJPEGRepresentation(img1, compression);
while ([imageData length] > maxFileSize && compression > maxCompression){
compression -= 0.1;
imageData = UIImageJPEGRepresentation(img1, compression);
}
UIImage *img = [[UIImage alloc] initWithData:imageData];
NSLog(#"Image ::: %#", img);
NSData *myImageData = UIImagePNGRepresentation(img);
NSLog(#" Image Data ::: %#", myImageData);
NSURL *requestURL = [NSURL URLWithString:#"https://graph.facebook.com/me"];
SLRequest *facebookRequest = [SLRequest requestForServiceType:SLServiceTypeFacebook requestMethod:SLRequestMethodPOST URL:requestURL parameters:parameters];
[facebookRequest addMultipartData:myImageData withName:#"source" type:#"multipart/form-data" filename:nil];
NSLog(#"Request Account :::: %#", facebookAccount);
facebookRequest.account = facebookAccount;
[facebookRequest performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error) {
NSLog(#"Done with Facebook.......");
if (!error) {
NSDictionary *list = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];
NSLog(#" Dictionary ::: %#", list);
} else {
NSLog(#" Error :::: %#", error.description);
}}];
I'm getting null values in the dictionary log.
What can be the issue? Thank you.
use below code rather than SLComposeViewController use SLRequest
NSDictionary *parameters = #{#"message": sendmessage};
SLRequest *facebookRequest = [SLRequest requestForServiceType:SLServiceTypeFacebook
requestMethod:SLRequestMethodPOST
URL:[NSURL URLWithString:#"https://graph.facebook.com/me/photos"]
parameters:parameters];
[facebookRequest addMultipartData: myImageData
withName:#"source"
type:#"multipart/form-data"
filename:#"TestImage"];
facebookRequest.account = facebookAccount;
[facebookRequest performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error)
{
// Log the result
}];
i hope this will also help you
-(void)facebookPost:(NSString *)text{
ACAccountStore *facebookaccount1 = [[ACAccountStore alloc] init];
ACAccountType *facebookaccountType = [facebookaccount1 accountTypeWithAccountTypeIdentifier: ACAccountTypeIdentifierFacebook];
// Specify App ID and permissions
NSDictionary *options = #{ ACFacebookAppIdKey: #"xxxxxxxxxxxxxxx", ACFacebookPermissionsKey: #[#"publish_stream"], ACFacebookAudienceKey: ACFacebookAudienceFriends };
[facebookaccount1 requestAccessToAccountsWithType:facebookaccountType options:options completion:^(BOOL granted, NSError *error) {
if(granted) {
NSArray *accountsArray = [facebookaccount1 accountsWithAccountType:facebookaccountType];
if ([accountsArray count] > 0)
{
NSDictionary *parameters = #{#"message": text};
SLRequest *facebookRequest = [SLRequest requestForServiceType:SLServiceTypeFacebook
requestMethod:SLRequestMethodPOST
URL:[NSURL URLWithString:#"https://graph.facebook.com/me/photos"]
parameters:parameters];
NSString *link = [NSString stringWithFormat:#"%simages/%#",ServerPath,[images objectAtIndex:imageNO]]; // image link
NSURL *url = [NSURL URLWithString:link];
NSData *data = [NSData dataWithContentsOfURL:url];
// code for compress image you can delete this code if you dont want to compress image
CGFloat compression = 1.0f;
CGFloat maxCompression = 0.0f;
int maxFileSize = 150*224;
UIImage *img1 = [[UIImage alloc] initWithData:data];
NSData *imageData = UIImageJPEGRepresentation(img1, compression);
while ([imageData length] > maxFileSize && compression > maxCompression)
{
compression -= 0.1;
imageData = UIImageJPEGRepresentation(img1, compression);
}
// imageData is your image
[facebookRequest addMultipartData: imageData
withName:#"source"
type:#"multipart/form-data"
filename:#"TestImage"];
ACAccount *facebookAccount1 = [accountsArray objectAtIndex:0];
[facebookRequest setAccount:facebookAccount1];
[facebookRequest performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error)
{
// Log the result
}];
}
}
}];
}