App crashing with exception: -[NSConcreteMutableData _fastCharacterContents] - ios

I am trying to share data with facebook. I am able to share data except image stored locally in iPhone memory. But when I try to share image stored locally with parameter #"source" my app gets terminated with error "[NSConcreteMutableData _fastCharacterContents]".
For sharing a local image I am converting it into NSData as required by #"source" parameter.
NSData *data = [NSData dataWithContentsOfURL:localUrl];
__block ACAccount *facebookAccount = nil;
ACAccountType *facebookAccountType = [self.accountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierFacebook];
// Specify App ID and permissions
NSDictionary *options = #{
ACFacebookAppIdKey: #"xxxxxxxxxxxxxx",
ACFacebookPermissionsKey: #[#"email"],
ACFacebookAudienceKey: ACFacebookAudienceEveryone
}; // basic read permissions
[self.accountStore requestAccessToAccountsWithType:facebookAccountType
options:options completion:^(BOOL granted, NSError *e)
{
if (granted) {
// Now that you have publish permissions execute the request
NSDictionary *options2 = #{
ACFacebookAppIdKey: #"xxxxxxxxxxxxxx",
ACFacebookPermissionsKey: #[#"publish_stream", #"publish_actions"],
ACFacebookAudienceKey: ACFacebookAudienceFriends
};
[self.accountStore requestAccessToAccountsWithType:facebookAccountType options:options2 completion:^(BOOL granted, NSError *error) {
if (granted) {
NSArray *accounts = [self.accountStore accountsWithAccountType:facebookAccountType];
facebookAccount = [accounts lastObject];
/*NSDictionary *parameters = #{#"message": #"This is a test",
#"name": #"Sharing Tutorial",
#"caption": #"Build great social apps and get more installs.",
#"description": #"Allow your users to share stories on Facebook from your app using the iOS SDK.",
#"link": #"https://developers.facebook.com/docs/ios/share/",
#"source":data};*/
NSMutableDictionary *parameters = [NSMutableDictionary dictionaryWithObjectsAndKeys:
#"My hat image", #"message", data, #"source", nil];
NSURL *feedURL = [NSURL URLWithString:#"https://graph.facebook.com/me/photos"];
SLRequest *feedRequest = [SLRequest
requestForServiceType:SLServiceTypeFacebook
requestMethod:SLRequestMethodPOST
URL:feedURL
parameters:parameters];
NSLog(#"AnythingHere?");
[feedRequest setAccount:facebookAccount];
[feedRequest performRequestWithHandler:^(NSData *responseData,
NSHTTPURLResponse *urlResponse, NSError *error)
{
// Handle response
NSLog(#"%#%#", error,urlResponse);
NSDictionary *response = [NSJSONSerialization JSONObjectWithData:responseData options:kNilOptions error:nil];
NSLog(#"Facebook Response : %#",response);
}];
}
else {
NSLog(#"Access denied 2");
NSLog(#"%#", [error description]);
}
}];
} else {
NSLog(#"Error: %#", [e description]);
NSLog(#"Access denied");
}
}];
What can be the reason behind this error?

This is just an educated guess because I've never used the Social framework, but it's too long for a comment.
Judging by this answer I would say that you are using SLRequest wrong. I'm pretty sure that the exception comes from your data object which is interpreted as a NSString (or NSURL).
Maybe you should use addMultipartData:withName:type:filename: to attach your photo to the request.
NSMutableDictionary *parameters = [NSMutableDictionary dictionaryWithObjectsAndKeys:
#"My hat image", #"message", nil];
NSURL *feedURL = [NSURL URLWithString:#"https://graph.facebook.com/me/photos"];
SLRequest *feedRequest = [SLRequest
requestForServiceType:SLServiceTypeFacebook
requestMethod:SLRequestMethodPOST
URL:feedURL
parameters:parameters];
[feedRequest addMultipartData:data
withName:#"source"
type:#"multipart/form-data"
filename:#"Test Image"];
which is basically the same code as another answer

please recheck your parameters formation.
and convert your image in following formate(Base64)
NSData *rep = UIImagePNGRepresentation(img);
NSLog(#"Rep: %#", rep);
NSString *base64 = [rep encodeBase64];
NSLog(#"Base 64 is a %#", NSStringFromClass([base64 class]));
self.somestring = [#"hardcodedstring" stringByAppendingString:base64]; //encodeBase64 encodes it to base64

Related

Upload video to Facebook doesn't work with SLRequest

I was trying to upload the video to Facebook with following code:
- (void)facebok
{
self.accountStore = [[ACAccountStore alloc]init];
ACAccountType *FBaccountType= [self.accountStore accountTypeWithAccountTypeIdentifier:#"com.apple.facebook"];
NSDictionary *options = #{
ACFacebookAppIdKey: #"appid",
ACFacebookPermissionsKey: #[#"publish_stream"],
ACFacebookAudienceKey: ACFacebookAudienceFriends
};
[self.accountStore requestAccessToAccountsWithType:FBaccountType options:options 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);
[self post];
} else {
//Fail gracefully...
NSLog(#"error getting permission %#",e);
}
}];
}
- (void)post
{
NSURL *url = [NSURL URLWithString:#"https://graph.facebook.com/me/videos"];
NSString *filePath = [[NSBundle mainBundle] pathForResource:#"test" ofType:#"mov"];
NSURL *pathURL = [[NSURL alloc]initFileURLWithPath:filePath isDirectory:NO];
NSData *videoData = [NSData dataWithContentsOfFile:filePath];
NSString *status = #"One step closer.";
NSDictionary *params = #{#"title":status, #"description":status};
SLRequest *uploadRequest = [SLRequest requestForServiceType:SLServiceTypeFacebook
requestMethod:SLRequestMethodPOST
URL:url
parameters:params];
[uploadRequest addMultipartData:videoData
withName:#"source"
type:#"video/quicktime"
filename:[pathURL absoluteString]];
uploadRequest.account = self.facebookAccount;
[uploadRequest performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error) {
NSString *responseString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
if(error){
NSLog(#"Error %#", error.localizedDescription);
}else
NSLog(#"%#", responseString);
}];
}
Fist it ask for the permission to post the video. That is fine but after few seconds it gives me this error:
{"error":{"message":"You recently posted something that violates Facebook policies, so you're temporarily blocked from using this feature. For more information, visit the Help Center.\n\nTo keep from getting blocked again, please make sure you've read and understand Facebook's Community Standards.","type":"OAuthException","code":368}}
Am I doing something wrong to post the video on Facebook?

An active access token must be used to query information about the current user error when posting video to facebook

This is my code to post a video to facebook:
ACAccountStore *accountStore = [[ACAccountStore alloc]init];
NSArray * p = [NSArray arrayWithObjects:#"email",#"basic_info", nil];
NSDictionary * dict=[NSDictionary dictionaryWithObjectsAndKeys:#"xxx",ACFacebookAppIdKey,p,ACFacebookPermissionsKey,ACFacebookAudienceEveryone,ACFacebookAudienceKey, nil];
NSDictionary *options = #{
ACFacebookAppIdKey: #"xxx",
ACFacebookPermissionsKey: #[#"publish_actions",#"publish_stream, " ],
ACFacebookAudienceKey: ACFacebookAudienceFriends,
};
ACAccountType *facebookAccountType = [accountStore
accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierFacebook];
[accountStore requestAccessToAccountsWithType:facebookAccountType options:dict completion:^(BOOL granted, NSError *error) {
NSLog(#"blablalba");
//do nothing here.
if (granted) {
[accountStore requestAccessToAccountsWithType:facebookAccountType options:options completion:^(BOOL granted, NSError *error) {
if (granted) {
NSLog(#"posting to fb..");
NSURL *url = [NSURL URLWithString:#"https://graph.facebook.com/me/videos"];
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSURL *videoPathURL = [defaults URLForKey:#"vidURL"];
// NSURL *videoPathURL = [[NSURL alloc]initFileURLWithPath:videoPath isDirectory:NO];
// NSData *videoData = [NSData dataWithContentsOfFile:videoPath];
NSData *videoData = [NSData dataWithContentsOfFile:[videoPathURL path]];
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]];
[request performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error) {
if (error) {
NSLog(#"error: %#", error);
}
NSLog(#"httpresponse: %#", urlResponse);
}];
}
}];
}
}];
But its returning:
"OAuth \"Facebook Platform\" \"invalid_request\" \"An active access token must be used to query information about the current user.\"";
I have no idea what I'm missing. Any help?
You are forgetting to set the facebook account to the request.
When you are granted the access, fetch the account:
NSArray *accounts = [self.accountStore
accountsWithAccountType:fbAccountType];
self.facebookAccount = [accounts lastObject];
and after creating the request, set the account
[request setAccount:self.facebookAccount];
Found that I had to add the token to the params field:
NSString *status = #"One step closer.";
NSDictionary *params = #{#"title":status, #"description":status, #"access_token":accessToken};
Hope it helps others.

Getting error while fetching picture of facebook friends with Graph API in iOS Social Framework

I'm using below code to get facebook friends list, and its working fine. But there is a need of friends profile picture too.
NSString *fbPermissions = #[ #"user_about_me",#"email",#"user_friends"];
NSString *apiIdKey = #"App_Key";
ACAccountStore *accountStore = [[ACAccountStore alloc] init];
id options = #{
ACFacebookAppIdKey: apiIdKey,
ACFacebookPermissionsKey: fbPermissions,
ACFacebookAudienceKey: ACFacebookAudienceFriends,
};
ACAccountType *accountType = [accountStore accountTypeWithAccountTypeIdentifier: ACAccountTypeIdentifierFacebook];
[accountStore requestAccessToAccountsWithType:accountType options:options completion:^(BOOL granted, NSError *error)
{
if (granted == YES)
{
NSArray *arrayOfAccounts = [accountStore accountsWithAccountType:accountType];
ACAccount *tempAccount = [arrayOfAccounts lastObject];
NSString *string = [NSString stringWithFormat:#"https://graph.facebook.com/me/friends"];
NSURL *fbURL = [NSURL URLWithString:string];
SLRequest *fbRequest = [SLRequest requestForServiceType:SLServiceTypeFacebook
requestMethod:SLRequestMethodGET
URL:fbURL
parameters:nil];
[fbRequest setAccount:tempAccount];
[fbRequest performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error) {
if (!error)
{
NSError *jsonError = nil;
NSDictionary *data = [NSJSONSerialization JSONObjectWithData:responseData
options:NSJSONReadingAllowFragments
error:&jsonError];
NSMutableArray *array = [data valueForKey:#"data"];
NSLog(#"Friends array:- %#",array);
}
else
{
[[[UIAlertView alloc]initWithTitle:#"Error"
message:[error description]
delegate:nil
cancelButtonTitle:#"OK"
otherButtonTitles:nil, nil] show];
}
}];
}
}];
To get profile picture I have changed request url with:-
NSString *string = [NSString stringWithFormat:#"https://graph.facebook.com/me/friends?fields=picture,name"];
I got following response :-
{
error = {
code = 2500;
message = "An active access token must be used to query information about the current user.";
type = OAuthException;
};
}
I'm not sure its access token expiration error because to revert back request Url to previous one, code works fine.
Finally it worked with:-
NSString *string = [NSString stringWithFormat:#"https://graph.facebook.com/me/friends"];
NSURL *requestURL = [NSURL URLWithString:#"https://graph.facebook.com/me/friends"];
SLRequest *fbRequest = [SLRequest requestForServiceType:SLServiceTypeFacebook
requestMethod:SLRequestMethodGET URL:requestURL parameters:#{#"fields":#"id,name,picture,first_name,last_name,gender"}];

Can't able to fetch the User Profile photo in ios

I am trying to get the facebook account details using SLRequest in ios.
I got all the details But Can not able to get the Profile Photo link of the User.
My code is as follow:
NSDictionary *options = [[NSDictionary alloc] initWithObjectsAndKeys:
#"xxxxxxxxxxxxxx", ACFacebookAppIdKey,
[NSArray arrayWithObjects:#"email", nil] , ACFacebookPermissionsKey,
nil];
[_accountStore requestAccessToAccountsWithType:facebookTypeAccount
options:options
completion:^(BOOL granted, NSError *error) {
if(granted){
NSArray *accounts = [_accountStore accountsWithAccountType:facebookTypeAccount];
_facebookAccount = [accounts lastObject];
NSLog(#"Success");
[self me];
}else{
// ouch
NSLog(#"Fail");
NSLog(#"Error: %#", error);
}
}];
- (void)me{
NSURL *meurl = [NSURL URLWithString:#"https://graph.facebook.com/me"];
SLRequest *merequest = [SLRequest requestForServiceType:SLServiceTypeFacebook
requestMethod:SLRequestMethodGET
URL:meurl
parameters:nil];
merequest.account = _facebookAccount;
[merequest performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error) {
NSString *meDataString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
NSLog(#"%#", meDataString);
NSJSONSerialization *js=[NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingAllowFragments error:nil];
NSLog(#"%#",js);
}];
}
Help me to solve this.
You can get the user's ID from the returned response, then use the below to get the profile picture:
http://graph.facebook.com/User_ID/picture
I use SDWebImage for images:
NSString *urlstr = #"http://graph.facebook.com/100001393655684/picture";
[imageView setImageWithURL:[NSURL URLWithString:urlstr]
placeholderImage:nil];
For Twitter, please make sure you authenticate your request, check this link for more details
Note: You may need to update TWRequest to SLRequest if you aren't deploying for iOS5

Getting OauthException when uploading a video to facebook

I am trying to upload a video on an iPhone to the user's facebook timeline using Social Framework. I used the snippet given in this answer. Here is my code:
- (void)performActivity {
NSLog(#"sharing on facebook.");
ACAccountStore *accountStore = [[ACAccountStore alloc] init];
ACAccountType *facebookAccountType = [accountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierFacebook];
NSDictionary *options = #{ACFacebookAppIdKey : #"***", ACFacebookPermissionsKey : #[#"email"], ACFacebookAudienceKey:ACFacebookAudienceFriends};
[accountStore requestAccessToAccountsWithType:facebookAccountType options:options completion:^(BOOL granted, NSError *error) {
if (granted) {
NSDictionary *options = #{ACFacebookAppIdKey : #"***", ACFacebookPermissionsKey : #[#"publish_stream"], ACFacebookAudienceKey:ACFacebookAudienceFriends};
[accountStore requestAccessToAccountsWithType:facebookAccountType options:options completion:^(BOOL granted, NSError *error) {
if (granted) {
NSLog(#"Publishing");
ACAccount *fbAccount = [accountStore accountsWithAccountType:facebookAccountType][0];
NSURL *videourl = [NSURL URLWithString:#"https://graph.facebook.com/me/videos"];
NSURL *pathURL = [NSURL fileURLWithPath:[NSString stringWithFormat:#"%#session.mov", NSTemporaryDirectory()]];
NSData *videoData = [NSData dataWithContentsOfFile:[NSString stringWithFormat:#"%#session.mov", NSTemporaryDirectory()]];
NSDictionary *params = #{
#"title": #"Nebuu",
#"description": #"mebuu"
};
SLRequest *uploadRequest = [SLRequest requestForServiceType:SLServiceTypeFacebook
requestMethod:SLRequestMethodPOST
URL:videourl
parameters:params];
[uploadRequest addMultipartData:videoData
withName:#"source"
type:#"video/quicktime"
filename:#"Nebuu Video"];
uploadRequest.account = fbAccount;
[uploadRequest performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error) {
NSString *responseString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
if(error){
NSLog(#"Error %#", error.localizedDescription);
}else
NSLog(#"%#", responseString);
}];
}}];
}}];
}
As you can see, I first requested read permission(email) then, requested publish permission if the first request is granted. The code works fine, all the permissions are granted and the app begins uploading video(I can see this in my network monitoring tool). However, after video is uploaded I get this error:
{
"error": {
"message": "An unexpected error has occurred. Please retry your request later.",
"type": "OAuthException"
}
}
What might be the problem here? I read some other SO posts and seems it is possible thet the error is originated from Facebook's side. Any suggestions?

Resources