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

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

Related

I want to share an image that is within the application bundle on facebook without url using FBWebDialogs?

NSMutableDictionary *params = [NSMutableDictionary dictionaryWithObjectsAndKeys: #"The mechanic has done a fine job improving my vehicle…", #"name", #"Click below to download this awesome app for FREE and challenge me", #"description",iTunesLink, #"link", img, #"picture", nil];
//step 2
// 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
//NSString *msg = [NSString stringWithFormat: #"Posted story, id: %#", [urlParams valueForKey:#"post_id"]];
// Show the result in an alert
isSharedButtonTapped = YES;
self.btnFacebookShare.userInteractionEnabled = NO;
[[[UIAlertView alloc] initWithTitle:#"Success!" message:#"Successfully shared to Facebook" 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?
`NSMutableDictionary *params = [NSMutableDictionary dictionaryWithObjectsAndKeys:
#"Sharing Tutorial", #"name",
#"Build great social apps and get more installs.", #"caption",
#"Allow your users to share stories on Facebook from your app using the iOS SDK.", #"description",
#"https://developers.facebook.com/docs/ios/share/", #"link",
#"http://i.imgur.com/g3Qc1HN.png", #"picture",
nil];`
[FBDialogs presentShareDialogWithPhotoParams:params
clientState:nil
handler:^(FBAppCall *call,
NSDictionary *results,
NSError *error) {
if (error) {
} else {
//share successdialog
}
}];
You can use something like this
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
UIImage *img = [info objectForKey:UIImagePickerControllerOriginalImage];
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 = #[img];
[FBDialogs presentShareDialogWithPhotoParams:params
clientState:nil
handler:^(FBAppCall *call,
NSDictionary *results,
NSError *error) {
if (error) {
NSLog(#"Error: %#",
error.description);
} else {
NSLog(#"Success!");
}
}];
}
For more details you can check here.
However this will add the shared image to Photos which would not be the way you want it to be. It will create a folder with the name of the app and the image will be added under this.

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.

Share app through facebook is not working

Hi in my Application i have option of sharing my application through Facebook. So I have added the Facebook framework to my project and I have added the bundle id into my Facebook and got the App id but the problem is I'm not Unable to share the App its showing like this.
An error occurred. Please try again later
In my Stimulator is showing like this i don't why it's showing like that. If my device have Facebook App its working fine but if it doesn't have the Facebook App its showing error like this. Please tell how to resolve this issue.
- (IBAction)share:(id)sender {
FBLinkShareParams *params = [[FBLinkShareParams alloc] init];
params.link = [NSURL URLWithString:#"url/"];
// If the Facebook app is installed and we can present the share dialog
if ([FBDialogs canPresentShareDialogWithParams:params]) {
// Present share dialog
[FBDialogs presentShareDialogWithLink:params.link
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
NSLog(#"Error publishing story: %#", error.description);
} else {
// Success
NSLog(#"result %#", results);
}
}];
// If the Facebook app is NOT installed and we can't present the share dialog
} else {
// FALLBACK: publish just a link using the Feed dialog
// Put together the dialog parameters
// Put together the dialog parameters
NSMutableDictionary *params = [NSMutableDictionary dictionaryWithObjectsAndKeys:
#"My appname", #"name",
#"test share.", #"caption",
#"sample.", #"description",
#"myrul", #"link",
nil];
// Show the feed dialog
[FBWebDialogs presentFeedDialogModallyWithSession:nil
parameters:params
handler:^(FBWebDialogResult result, NSURL *resultURL, NSError *error) {
if (error) {
// An error occurred, we need to handle the error
// See: https://developers.facebook.com/docs/ios/errors
NSLog(#"Error publishing story: %#", error.description);
} else {
if (result == FBWebDialogResultDialogNotCompleted) {
// User canceled.
NSLog(#"User cancelled.");
} else {
// Handle the publish feed callback
NSDictionary *urlParams = [self parseURLParams:[resultURL query]];
if (![urlParams valueForKey:#"post_id"]) {
// User canceled.
NSLog(#"User cancelled.");
} else {
// User clicked the Share button
NSString *result = [NSString stringWithFormat: #"Posted story, id: %#", [urlParams valueForKey:#"post_id"]];
NSLog(#"result %#", result);
}
}
}
}];
}
}
- (NSDictionary*)parseURLParams:(NSString *)query {
NSArray *pairs = [query componentsSeparatedByString:#"&"];
NSMutableDictionary *params = [[NSMutableDictionary alloc] init];
for (NSString *pair in pairs) {
NSArray *kv = [pair componentsSeparatedByString:#"="];
NSString *val =
[kv[1] stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
params[kv[0]] = val;
}
return params;
}
I have used the above code for its not working please help me how to resolve this issue.
Thanks.
Use this code for Share App.
-(void)facebook
{
if([SLComposeViewController isAvailableForServiceType:SLServiceTypeFacebook]) {
SLComposeViewController *controller = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeFacebook];
SLComposeViewControllerCompletionHandler myBlock = ^(SLComposeViewControllerResult result){
if (result == SLComposeViewControllerResultCancelled) {
NSLog(#"Cancelled");
}
else
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Congratulations!" message:#"Successfully posted to facebook Wall." delegate:nil cancelButtonTitle:#"Ok" otherButtonTitles:nil, nil];
[alert show];
[Flurry logEvent:#"Share screen - Successfully posted on Facebook"];
}
[controller dismissViewControllerAnimated:YES completion:Nil];
};
controller.completionHandler =myBlock;
NSString *str=[NSString stringWithFormat:#" I am using App for my iPhone. You too can download it from here"];
[controller setInitialText:str];
[self presentViewController:controller animated:YES completion:Nil];
}
else{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Sorry!"message:#"Please add Facebook account in settings menu" delegate:self cancelButtonTitle:Nil otherButtonTitles:#"Ok", nil];
alert.tag=100;
[alert show];
}
}
For use of SLComposeViewController,you have to add Social Framework. It fetch your facebook account from setting and you can share your app link through SLComposeViewController. and
if you want to add images or URL to in than :
[composeController addImage:[UIImage imageNamed:#"image.png"]];
[composeController addURL: [NSURL URLWithString:#"apple.com"]];

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

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