iOS - Posting image and text to FB User's wall - ios

I'm creating iOS app using latest Facebook framework 3.1
I've seen lots of content regarding posting image and text to facebook user's wall. I'm using the following code.
NSDictionary* dict = #{
#"link" : #"https://developers.facebook.com/ios",
#"picture" : [UIImage imageNamed:#"Default"],
#"message":#"Your temp message here",
#"name" : #"MyApp",
#"caption" : #"TestPost",
#"description" : #"Integrating Facebook in ios"
};
[FBRequestConnection startWithGraphPath:#"me/feed" parameters:dict HTTPMethod:#"POST" completionHandler:^(FBRequestConnection *connection, id result, NSError *error) {
NSString *alertText;
if (error)
{
NSLog(#"%#",[NSString stringWithFormat: #"error: domain = %#, code = %d", error.domain, error.code]);
alertText = #"Failed to post to Facebook, try again";
} else
{
alertText = #"Posted successfully to Facebook";
}
[[[UIAlertView alloc] initWithTitle:#"Facebook Result" message:alertText delegate:nil cancelButtonTitle:#"OK!" otherButtonTitles:nil] show];
}];
If I'm providing the picture as "URL" its posting fine. Else its throwing error. I want to post an image that is within the application bundle. Can some on tell me where I'm coding wrong?

UIImage* image = [UIImage imageNamed:#"nature.jpg"];
NSMutableDictionary* params = [[NSMutableDictionary alloc] init];
[params setObject:#"My First message" forKey:#"message"];
[params setObject:image forKey:#"picture"];
[FBRequestConnection startWithGraphPath:#"me/photos" parameters:dict HTTPMethod:#"POST" completionHandler:^(FBRequestConnection *connection, id result, NSError *error) {
NSString *alertText;
if (error)
{
NSLog(#"%#",[NSString stringWithFormat: #"error: domain = %#, code = %d", error.domain, error.code]);
alertText = #"Failed to post to Facebook, try again";
} else
{
alertText = #"Posted successfully to Facebook";
}
[[[UIAlertView alloc] initWithTitle:#"Facebook Result" message:alertText delegate:nil cancelButtonTitle:#"OK!" otherButtonTitles:nil] show];
}];

Related

Fb share dialog, can i simply post a picture with a description?

I'm trying to create a simple sharing function for my ios app. I wanna take advantage of the new sharing dialog (advantages like , tagging friends, add places,ecc..). What i wanna share is a photo ,a link to itunes app download, a description that comes from the ios app. I have tried the sharedialog, something like this:
NSURL *url=[[NSURL alloc] initWithString:#"https://itunes.apple.com/it/app/myapplication"];
[FBDialogs presentShareDialogWithLink:url name:#"My app name" caption:#"" description:#"prefilled descriptiontest" picture:[NSURL URLWithString:#"http://www.example.com/image.jpg"] clientState:nil handler:^(FBAppCall *call, NSDictionary *results, NSError *error) {
if(error) {
// An error occurred, we need to handle the error
// See: https://developers.facebook.com/docs/ios/errors
UIAlertView *av = [[[UIAlertView alloc]
initWithTitle:#"Sorry :("
message:#"There's been a problem publishing your message. Please try again!"
delegate:self
cancelButtonTitle:#"Close"
otherButtonTitles:nil] autorelease];
[av show];
} else {
// Success
UIAlertView *av = [[[UIAlertView alloc]
initWithTitle:#"Published!"
message:#"Your post has been successfully published."
delegate:self
cancelButtonTitle:#"Close"
otherButtonTitles:nil] autorelease];
[av show];
}
}];
It works but i see my prefilled description only in share dialog, when i see the shared content on facebook i only see the description taken from the linked page. So i have tried this other solution, the photodialog :
UIImage *Image=[UIImage imageNamed:#"myphoto.jpg"];
// Open the image picker and set this class as the delegate
FBPhotoParams *params = [[FBPhotoParams alloc] init];
// Note that params.photos can be an array of images. In this example
// we only use a single image, wrapped in an array.
params.photos = #[Image];
[FBDialogs presentShareDialogWithPhotoParams:params
clientState:nil
handler:^(FBAppCall *call,
NSDictionary *results,
NSError *error) {
if (error) {
NSLog(#"Error: %#",
error.description);
} else {
NSLog(#"Success!");
}
}];
In this way i can post a photo on my wall but the description parameter seems to be onlyread and i can't set any prefilled text. How i can do? There's a way to force the visualization of my description text in the sharelink dialog? Or there's a way to set a text in the photodialog? Or there another solution ?
The only solution seems to be to use only the web fallback:
NSMutableDictionary *parameter = [NSMutableDictionary dictionaryWithObjectsAndKeys:
name, #"name",
author, #"caption",
linkShare, #"link",
userImage, #"picture",
nil];
[FBWebDialogs presentFeedDialogModallyWithSession:nil
parameters:parameter
handler:^(FBWebDialogResult result, NSURL *resultURL, NSError *error) {
if (error)
{
NSLog(#"Error publishing story: %#", error.description);
}
else
{
if (result == FBWebDialogResultDialogNotCompleted)
{
NSLog(#"User cancelled.");
}
else
{
NSDictionary *urlParams = [self parseURLParams:[resultURL query]];
NSLog(#"User login");
if (![urlParams valueForKey:#"post_id"])
{
NSLog(#"User cancelled post.");
}
else
{
NSString *result = [NSString stringWithFormat: #"Posted story, id: %#", [urlParams valueForKey:#"post_id"]];
NSLog(#"result %#", result);
}
}
}
}];

Facebook share dialog does not post story with local image

I am trying to post a story using the share dialog with the code:
[FBDialogs presentShareDialogWithOpenGraphAction:action
actionType:#"myApp:myActionType"
previewPropertyName:#"myObjectType"
handler:^(FBAppCall *call, NSDictionary *results, NSError *error)
{
if(error)
{
NSLog(#"Facebook: error publishing story: %#", error.description);
}
else
{
NSLog(#"Facebook: publishing story: result %#", results);
}
}];
I get the share dialog (showing the image preview) and press Post, then I get the progress bar but it doesn't progress, and then I get switched back to my app. Not only that, the handler does not get called. I am trying to post a story with a locally generated image, like so:
id<FBOpenGraphAction> action = (id<FBOpenGraphAction>)[FBGraphObject graphObject];
NSArray* image = #[ #{#"url": [UIImage imageNamed:#"image-poof-1.png"], #"user_generated": #"true"} ];
[action setObject:image forKey:#"image"];
id<FBGraphObject> object = [FBGraphObject openGraphObjectForPost];
[object setObject:#"myApp:myObjectType" forKey:#"type"];
[object setObject:#"my title" forKey:#"title"];
[object setObject:#"my description" forKey:#"description"];
[action setObject:object forKey:#"myObjectType"];
I tried posting using a link to an image on the web, and that worked.
EDIT: I've followed this facebook code example
I think property url is for image url only try post file:
NSMutableDictionary *variables = [NSMutableDictionary dictionaryWithCapacity:2];
UIImage *picture = [UIImage imageNamed:#"75x75.png"];
FbGraphFile *graph_file = [[FbGraphFile alloc] initWithImage:picture];
[variables setObject:graph_file forKey:#"file"];
[variables setObject:#"this is a test message: postPictureButtonPressed" forKey:#"message"];
//the fbGraph object is smart enough to recognize the binary image data inside the FbGraphFile
//object and treat that is such.....
FbGraphResponse *fb_graph_response = [fbGraph doGraphPost:#"117795728310/photos" withPostVars:variables];
Readmore
Document
Sample app
I have not experience of share dialog to post local image but I have posted local image to facebook without share dialog. And it is successfully posted with image and message. My code for that is
NSString *message=#"your message";
UIImage *sendPic = [UIImage imageNamed:#"image.png"];
NSMutableDictionary* params = [[NSMutableDictionary alloc] init];
[params setObject:message forKey:#"message"];
[params setObject:UIImagePNGRepresentation(sendPic) forKey:#"picture"];
//fbShreBtn.enabled = NO; //for not allowing multiple hits
[FBSession setActiveSession:[self appDelegate].session];
[FBRequestConnection startWithGraphPath:#"me/photos"
parameters:params
HTTPMethod:#"POST"
completionHandler:^(FBRequestConnection *connection,
id result,
NSError *error)
{
NSLog(#"fb result : %#",result);
if (error)
{
//showing an alert for failure
NSLog(#"error : %#",error);
UIAlertView *alertView = [[UIAlertView alloc]
initWithTitle:#"Post Failed"
message:error.localizedDescription
delegate:nil
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[alertView show];
}
else
{
//showing an alert for success
UIAlertView *alertView = [[UIAlertView alloc]
initWithTitle:#"Post success"
message:#"Shared successfully"
delegate:nil
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[alertView show];
}
//fbShreBtn.enabled = YES;
}];
It might be useful for you. Thanks.

Post to Facebook multiple photos with status message in single wall post

I am having trouble posting like this image. A common caption for all images & multiple images. At least, tell me if it is possible or not ?
I have tried to loop through all images & succeed to post all images as shown in the image but not getting the caption. (In image it's : "TEST: PLEASE IGNORE FRNDS."). How can i do it ?
My code is posting successfully but the problem is that each photo is a different post. I want all photos in one single post.
Here's what i tried:
NSMutableArray *arrayOfImages = [self getSelectedImagesArray];
for (int i=0; i<arrayOfImages.count; i++)
{
NSMutableDictionary* params = [[NSMutableDictionary alloc] init];
[params setObject:#"LOL !! THIS IS IMAGE" forKey:#"message"];
//[params setObject:#"LOL !! APP NAME" forKey:#"name"];
[params setObject:#"LOL !! THIS IS IMAGE CAPTION" forKey:#"caption"];
[params setObject:#"LOL !! THIS IS IMAGE description" forKey:#"description"];
[params setObject:UIImageJPEGRepresentation([arrayOfImages objectAtIndex:i], 0.5) forKey:#"picture"];
[FBRequestConnection startWithGraphPath:#"me/photos"
parameters:params
HTTPMethod:#"POST"
completionHandler:^(FBRequestConnection *connection,
id result,
NSError *error)
{
NSString *alertText;
if (error)
{
//showing an alert for failure
[[FBSession activeSession] openWithBehavior:FBSessionLoginBehaviorForcingWebView completionHandler:^(FBSession *session, FBSessionState status, NSError *error)
{
[self shareAdvertOnFacebook];
}];
alertText = [NSString stringWithFormat: #"error: domain = %#, code = %d", error.domain, error.code];
}
else
{
//showing an alert for success
alertText = [NSString stringWithFormat: #"Posted action, id: %#", result[#"id"]];
[[[UIAlertView alloc] initWithTitle:#"Result" message:#"Advert is posted to Facebook successfully." delegate:self cancelButtonTitle:#"OK!" otherButtonTitles:nil] show];
}
}];
}
First make sure you have publish_stream permission to create a new album.
Then to create a new album, make a Graph API call to https://graph.facebook.com/me/albums with parameters for the access token, the album name, and the album description.
If the album is created successfully the new album ID will be returned.
Then make a call to https://graph.facebook.com/NEW_ALBUM_ID/photos with the access token and other photo parameters to upload your photo to the new album.
By default, if you do not specify any album and just push photos to the me/photos endpoint your photos will end up in an album automatically created for your application.
This code worked for me, just you have to add your code to "post all images"
- (void) postImageToFB:(UIImage*)image
{
NSData* imageData = UIImageJPEGRepresentation(image, 90);
NSString *text=#"Purchased a book From Library Store,\nBook Name-";
text=[text stringByAppendingString:lbl_bk_name.text];
text=[text stringByAppendingString:#"\nCategory-"];
text=[text stringByAppendingString:lbl_bk_category.text];
text=[text stringByAppendingString:#"\nFeeling Great!!"];
NSMutableDictionary * params = [NSMutableDictionary dictionaryWithObjectsAndKeys:
text, #"message",
imageData, #"source",
nil];
[FBRequestConnection startWithGraphPath:#"me/photos"
parameters:params
HTTPMethod:#"POST"
completionHandler:^(FBRequestConnection *connection, id result, NSError *error) {
if (error) {
UIAlertView *alert=[[UIAlertView alloc] initWithTitle:#"Facebook" message:#"Problem in sharing in Facebook. Try Later!" delegate:self cancelButtonTitle:#"OK"otherButtonTitles:nil, nil];
[alert show];
} else {
UIAlertView *alert=[[UIAlertView alloc] initWithTitle:#"Facebook" message:#"Shared Successfully." delegate:self cancelButtonTitle:#"OK"otherButtonTitles:nil, nil];
[alert show];
}
}];
}

Post to Friends Wall and un-authorize error

I am trying to post to friend wall and it wouldn't work. I can post to my wall though.
- (void)publishStory
{
NSString *graph=#"me/feed";
if([self.friendId length]!=0){
graph=[NSString stringWithFormat:#"%#/feed", self.friendId];
}
self.postParams = [#{
#"link" : self.link,
#"picture" : self.imageUrl,
#"name" :self.name,
#"caption" :self.caption,
#"description" : self.postMessageTextView.text
} mutableCopy];
[FBRequestConnection
startWithGraphPath:graph
parameters:self.postParams
HTTPMethod:#"POST"
completionHandler:^(FBRequestConnection *connection,
id result,
NSError *error) {
NSString *alertText;
if (error) {
NSLog(#"Error publish %#", error.localizedDescription);
alertText = [NSString stringWithFormat:
#"error: domain = %#, code = %d",
error.domain, error.code];
} else {
alertText = #"Posted to Facebook. Thank you";
}
// Show the result in an alert
[[[UIAlertView alloc] initWithTitle:#"Result"
message:alertText
delegate:self
cancelButtonTitle:#"OK!"
otherButtonTitles:nil]
show];
}];
}
This is the error.
body = {
error = {
code = 200;
message = "(#200) The user hasn't authorized the application to perform this action";
type = OAuthException;
};
};
code = 403;

presentFeedDialogModallywithSession does not post message to a friend

Iam using the following code to post a message on my friend's facebook wall. This code posts message to the logged in users wall but not to the friend's wall
Iam also giving the "to" value and i.e., friends facebook id (116623456) and App_id but same problem persists.
Please provide a good direction on this.
- (void)facebookViewControllerDoneWasPressed:(id)sender
{
NSLog(#"DonePressed Called");
NSString* fid;
NSString* fbUserName;
NSString *message = [NSString stringWithFormat:#"You have been selected as a health coach(Multiple Users1), You will be receiving daily and weekly reports from here on!!!!"];
NSLog(#"Before For");
// NSString *SelectedFriends = nil;
for (id<FBGraphUser> user in _friendPickerController.selection)
{
fid = user.id;
fbUserName = user.name;
NSLog(#"User Name =%#, USer id =%#",fbUserName, fid);
}
NSLog(#"After For");
NSLog(#"%#",fid);
NSMutableDictionary *params =
[NSMutableDictionary dictionaryWithObjectsAndKeys:
#"4444444444444444",#"app_id",
fid,#"to",
nil];
// Invoke the dialog
[FBWebDialogs presentFeedDialogModallyWithSession:nil
parameters:params
handler:
^(FBWebDialogResult result, NSURL *resultURL, NSError *error) {
if (error) {
// Error launching the dialog or publishing a story.
NSLog(#"Error publishing story.");
} else {
if (result == FBWebDialogResultDialogNotCompleted) {
// User clicked the "x" icon
NSLog(#"User canceled story publishing.");
} else {
// Handle the publish feed callback
NSDictionary *urlParams = [self parseURLParams:[resultURL query]];
if (![urlParams valueForKey:#"post_id"]) {
// User clicked the Cancel button
NSLog(#"User canceled story publishing.");
} else {
// User clicked the Share button
// Show the result in an alert
[[[UIAlertView alloc] initWithTitle:#"Result"
message:#"Message Posted Successfully"
delegate:self
cancelButtonTitle:#"OK!"
otherButtonTitles:nil]
show];
}
}
}
}];
}
- (void)facebookViewControllerDoneWasPressed:(id)sender {
NSMutableString *text = [[NSMutableString alloc] init];
// we pick up the users from the selection, and create a string that we use to update the text view
// at the bottom of the display; note that self.selection is a property inherited from our base class
for (id<FBGraphUser> user in self.friendPickerController.selection) {
if ([text length]) {
[text appendString:#", "];
}
[text appendString:user.name];
NSString *fid=user.id;
NSString *fbUserName=user.name;
NSLog(#"");
NSMutableDictionary* params = [[NSMutableDictionary alloc] initWithObjectsAndKeys:#"Test miLineup!", #"message", #"Iphone Apps", #"name", nil];
NSLog(#"\nparams=%#\n", params);
//Post to friend's wall.
[FBRequestConnection startWithGraphPath:[NSString stringWithFormat:#"%#/feed", fid] parameters:params HTTPMethod:#"POST"
completionHandler:^(FBRequestConnection *connection, id result, NSError *error)
{
//Tell the user that it worked.
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:#"Shared"
message:[NSString stringWithFormat:#"Invited %#! error=%#", fbUserName, error]
delegate:nil
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[alertView show];
}
];
//Close the friend picker.
//[self dismissModalViewControllerAnimated:YES];
}

Resources