Posting a map to Facebook using Facebook SDK from iOS - ios

I am posting an image to facebook through FBRequestConnection.
How can I post a map to FB by giving a latitude and longitude. (Also attaching some text with this)
Please advise.
if (FBSession.activeSession.isOpen) {
NSMutableDictionary* params = [[NSMutableDictionary alloc] init];
[params setObject:#"Posting from padivattom" forKey:#"message"];
[params setObject:UIImagePNGRepresentation(image) forKey:#"picture"];
[FBRequestConnection startWithGraphPath:#"me/photos"
parameters:params
HTTPMethod:#"POST"
completionHandler:^(FBRequestConnection *connection,
id result,
NSError *error)
{
if (error)
{
//showing an alert for failure
}
else
{
//showing an alert for success
}
}];

I used the method:
[FBWebDialogs presentFeedDialogModallyWithSession:
parameters:
handler:
to post message and map to facebook. (Its post to news feed).
But you can try to add to your dictionary: [params setObject:[NSString stringWithFormat:#"https://maps.google.com/?q=%#,%#",latitude,longitude] forKey:#"link"];
(If it doesn't work, you can find the Feed Dialog sample on facebook developers page).
I hope it helps

Related

error message fbcdn.net urls are not allowed

I'm trying to share a post with this code:
// Put together the dialog parameters
NSMutableDictionary *params = [[NSMutableDictionary alloc]init];
[params setValue:[Campaign sharedInstance].name forKey:#"name" ];
[params setValue:#"desription" forKey:#"description"];
[params setValue:picture.facebookPicture.link forKey:#"link"];
[params setValue:picture.facebookPicture.source forKey:#"picture"];
// Show the feed dialog
[FBWebDialogs presentFeedDialogModallyWithSession:nil parameters:params handler:^(FBWebDialogResult result, NSURL *resultURL, NSError *error) {
if(error) {
[basicViewController toastMessage:[#"Error sharing on Facebook: " stringByAppendingString:error.localizedDescription]];
}}];
I'm getting an error because the source of the picture is from facebook.
What can i do to add the wanted facebook picture and still using the fbwebdialogs?

formatting a url to appear as string in a facebook post

I am trying to post a link formatted as a string on facebook. Please see the relevant code below:
NSMutableDictionary* params = [[NSMutableDictionary alloc] init];
NSString *modifiedTitle = [NSString stringWithFormat:#"<a href= https://www.google.com> My App </a>"];
[params setObject:modifiedTitle forKey:#"message"];
[params setObject:UIImagePNGRepresentation(picture) forKey:#"picture"];
[FBRequestConnection startWithGraphPath:#"me/photos"
parameters:params
HTTPMethod:#"POST"
completionHandler:^(FBRequestConnection *connection,
id result,
NSError *error)
if (error)
{
//showing an alert for failure
NSLog(#"ERROR in posting image to Facebook");
}
else
{
//showing an alert for success
NSLog(#"Result after posting image successfully = %#",result);
}
// button.enabled = YES;
}];
The message appeared as:
<a href= https://www.google.com> My App </a>
instead of
My App
For one thing, ensure you're posting an immmutable dictionary into the startWithGraph... method by using the copy method:
NSDictionary* paramsImmutable = params.copy;
[FBRequestConnection startWithGraphPath:#"me/photos"
parameters:paramsImmutable
HTTPMethod:#"POST"
completionHandler:^(FBRequestConnection *connection,
id result,
NSError *error)
That way you won't have any changes to worry about on that object during the method's lifetime.
Next, is your params including any sort of quotes around the url? I.e.
NSString *modifiedTitle = #"<a href='https://www.google.com'>My App</a>";
That's just a start.

iOS Facebook SDK startWithGraphPath:parameters post to just SOME friends programmatically

In Facebook, you can post something and share it with just some friends.
How can this be done programmatically on iOS using Facebook SDK?
My App has an "Emergency Button" that sends: a) User Location on a Map (picture); b) An emergency text (message); and c) The post is only shared with the friends the user has chosen in the config. section (privacy).
- (void)sendMessage {
//Just an Example: Send an Emergency Post with: message, picture, just to SOME friends.
//This action may not need the User to actually press a button.
//Privacy parameter obtained from:
//https://developers.facebook.com/docs/graph-api/reference/v2.0/post/
NSMutableDictionary *privacy = [[NSMutableDictionary alloc]initWithObjectsAndKeys:
#"100000278095294,100000278095528", #"allow", //Friends Id separeted with commas.
#"", #"deny",
#"CUSTOM", #"value", //Privacy Custom Value
nil];
NSMutableDictionary* params = [[NSMutableDictionary alloc] init];
[params setObject:self.emergencyText forKey:#"message"];
[params setObject:self.locationMap forKey:#"picture"];
[params setObject:privacy forKey:#"privacy"];
[FBRequestConnection startWithGraphPath:#"me/feed"
parameters:params
HTTPMethod:#"POST"
completionHandler:^(FBRequestConnection *connection,
id result,
NSError *error) {
if (error) {
NSLog(#"Error");
NSLog(#"Error descriptcion %#",error.description);
}else{
NSLog(#"Success");
}
}];
}
This code in not running. I get an error from Facebook.
If I comment this line:
// [params setObject:privacy forKey:#"privacy"];
Then it runs fine and I see the post in FB, but it is a public post.
I need to post: message, picture, just to some friends.
Any solution using startWithGraphPath or using any other command is welcome!
You must format the privacy as a JSON string and then assign that json string to the params NSMutableDictionary.
For Example:
NSMutableDictionary *privacy = [[NSMutableDictionary alloc]initWithObjectsAndKeys:
#"100000278095294,100000278095528", #"allow", //Friends Id separeted with commas.
#"", #"deny",
#"CUSTOM", #"value", //Privacy Custom Value
nil];
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:privacy
options:NSJSONWritingPrettyPrinted
error:&error];
NSString *jsonString;
if (! jsonData) {
NSLog(#"Got an error: %#", error2);
} else {
jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
}
NSMutableDictionary* params = [[NSMutableDictionary alloc] init];
[params setObject:#"to my friend" forKey:#"message"];
[params setObject:self.locationMap forKey:#"picture"];
[params setObject:jsonString forKey:#"privacy"];

post on friends wall in Facebook using FBWebDialogs in ios

I have followed this link to post on friends wall but its not posting actually instead it just lists the no of friends and allows for multiple selection of friends, there is no option for posting on friends wall.
Not getting satisfactory answer please help me how to post on friends wall using FBWebDialogs, as other ways using Graph api to post on users wall is disabled by facebook.
Any help would be much appreciated. Thanks.
Finally I got the answer of my own question, how easier it was !! but it took my couple of days reaching out there.
To post on your friends wall first of all you need is a permission publish_actions.
And here is the simple code :
NSMutableDictionary *params = [NSMutableDictionary new];
[params setObject:#"YOUR DESC" forKey:#"description"];
[params setObject:#"YOUR LINK" forKey:#"link"];
[params setObject:#"YOUR CAPTION" forKey:#"caption"];
[params setObject:#"FRIEND_ID" forKey:#"to"];
[FBWebDialogs presentFeedDialogModallyWithSession:nil
parameters:params
handler:^(FBWebDialogResult result, NSURL *resultURL, NSError *error) {
NSLog(#" RESULT : %u",result);
}
];
Make sure you have added Facebook SDK in you bundle
and have imported necessary class.
That's it..
Look on this,
NSDictionary *params = #{#"name" :#"Your name",
#"caption" : #"",
#"description" :#"Your description"],
#"picture" : #"Your Image link",
#"link" : #"Your link",
};
// Invoke the dialog
[FBWebDialogs presentFeedDialogModallyWithSession:nil
parameters:params
handler:
^(FBWebDialogResult result, NSURL *resultURL, NSError *error) {
if (error) {
NSLog(#"Error publishing story.");
} else {
if (result == FBWebDialogResultDialogNotCompleted) {
NSLog(#"User canceled story publishing.");
} else {
NSLog(#"Story published.");
}
}}];

IOS Facebook SDK 3 upload image with message

I am using the following code to upload an image to Facebook wall :
UIImage *img = [UIImage imageNamed:#"logoits.png"];
[FBRequestConnection startForUploadPhoto:img
completionHandler:^(FBRequestConnection *connection, id result, NSError *error) {
NSLog(#"Facebook posting %#",result);
NSLog(#"Facebook posting %#",error);
}];
It works as expected but I would like to add a message or title to this uploaded photo, and looking at the documentation of FBRequestConnection there is no example of that. http://developers.facebook.com/docs/sdk-reference/iossdk/3.0/class/FBRequestConnection/
How do I upload an image with message?
In the Facebook SDK 3.0 only a image can be post using the given methods, and for other actions you need to use graph a pi's.
So for post a image with message you need to use graph-api. Here is the line of codes which lets you to post a image with message on users timeline.
NSMutableDictionary* params = [[NSMutableDictionary alloc] init];
[params setObject:#"your custom message" forKey:#"message"];
[params setObject:UIImagePNGRepresentation(_image) forKey:#"picture"];
_shareToFbBtn.enabled = NO; //for not allowing multiple hits
[FBRequestConnection startWithGraphPath:#"me/photos"
parameters:params
HTTPMethod:#"POST"
completionHandler:^(FBRequestConnection *connection,
id result,
NSError *error)
{
if (error)
{
//showing an alert for failure
[self alertWithTitle:#"Facebook" message:#"Unable to share the photo please try later."];
}
else
{
//showing an alert for success
[UIUtils alertWithTitle:#"Facebook" message:#"Shared the photo successfully"];
}
_shareToFbBtn.enabled = YES;
}];
and also make sure _shareToFbBtn is enabled only after login is success.
linesvishy's solution works great and it's also worth to note that if you were using Facebook SDK 3.1 and you used method
FBRequest *request = [FBRequest requestForUploadPhoto:_imageToShare];
You can just use the following lines to replace it:
NSMutableDictionary* params = [[NSMutableDictionary alloc] init];
[params setObject:_textToShare forKey:#"message"];
[params setObject:_imageToShare forKey:#"picture"];
FBRequest *request = [FBRequest requestWithGraphPath:#"me/photos" parameters:params HTTPMethod:#"POST"];
Hope it helps! Happy Coding!
Here is an example that posts and image and a note:
http://developers.facebook.com/docs/tutorials/ios-sdk-tutorial/publish-open-graph-story/

Resources