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"];
Related
I am using the following code to upload an image from my iOS to Facebook.
NSMutableDictionary* params = [[NSMutableDictionary alloc] init];
[params setObject:#"MY TEST MSG..." forKey:#"message"];
[params setObject:UIImagePNGRepresentation(self.editImageView.image) forKey:#"picture"];
[[FBSDKGraphRequest alloc] initWithGraphPath:#"me/photos" parameters:params HTTPMethod:#"POST"] startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection, id result, NSError *error) {
if (!error) {
NSLog(#"uploaded");
} else {
NSLog(#"Graph API Error: %#", [error description]);
}
}];
My app got stuck for couple of minutes and show the error message.
Can any one please help me?
NSMutableDictionary *variables = [NSMutableDictionary dictionaryWithCapacity:3];
UIImage *picture = FilterImage;
FbGraphFile *graph_file = [[FbGraphFile alloc] initWithImage:picture];
[variables setObject:graph_file forKey:#"file"];
[variables setObject:[NSString stringWithFormat:#"Hello testin"] forKey:#"message"];
[fbGraph doGraphPost:#"me/photos" withPostVars:variables];
NSLog(#"Now log into Facebook and look at your profile & photo albums...");
When I post photo in faceboook one by one through ios. it creates a photo album,I want to post photo separately, don't create the album or thumbnails. upload single post at a time. I am doing R & D more than two days.
please provide a better solution.
Thanks,
UIImage *postImag = [UIImage ImageWithName:#"Hello.png"];
NSString *message = #"Hello Test Message";
NSMutableDictionary *params = [[NSMutableDictionary alloc] initWithCapacity:0];
NSData *imageData = UIImagePNGRepresentation(postImag);
[params setObject:message forKey:#"message"];
if (imageData ) {
[params setObject:imageData forKey:#"source"];
}
/* make the API call */
[FBRequestConnection startWithGraphPath:#"me/photos" parameters:params
HTTPMethod:#"POST"
completionHandler:^(
FBRequestConnection *connection,
id result,
NSError *error
) {
if (error) {
NSLog(#"Error: %#",[error localizedDescription]);
} else {
NSLog(#"Successfully shared.");
}
completionAction();
}];
}
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?
i am new in ios , i want to write something on my friend post . i am try this code using graph
api this is not working please help me
NSMutableDictionary* params = [NSMutableDictionary dictionary];
[params setObject:#"Some text" forKey:#"user_message_prompt"];
[params setObject:#"another text" forKey:#"action_links"];
[params setObject:#"Yet another text" forKey:#"attachment"];
[params setObject:#"SOME FACEBOOK ID" forKey:#"target_id"];
FBRequest *request = [[FBRequest alloc] initWithSession:FBSession.activeSession
graphPath:#"https://graph.facebook.com/100000329153640/feed"
parameters:params
HTTPMethod:#"POST"];
NSLog(#"%#",request);
in this NSlog-- request line return this
graphPath: https://graph.facebook.com/100000329153640/feed, HTTPMethod: POST, parameters: {
"action_links" = "another text";
attachment = "Yet another text";
"migration_bundle" = "fbsdk:20131212";
"target_id" = "SOME FACEBOOK ID";
"user_message_prompt" = "Some text";
}>
please give me solution
please share your valuable knowledge ... i am waiting
Thankyou
Try this one,
NSMutableDictionary *params = [NSMutableDictionary new];
[params setObject:#"Some text" forKey:#"user_message_prompt"];
[params setObject:#"another text" forKey:#"action_links"];
[params setObject:#"Yet another text" forKey:#"attachment"];
[params setObject:#"SOME FACEBOOK ID" forKey:#"target_id"];
[FBWebDialogs presentFeedDialogModallyWithSession:nil
parameters:params
handler:^(FBWebDialogResult result, NSURL *resultURL, NSError *error) {
NSLog(#" RESULT : %u",result);
}
];
Refer Developer Guide for facebook
Facebook Developer guide
The first thing you need to do is request publish permissions. I wrote this helper method to do it:
void runBlockWithPublishPermissions(void (^block)(FBSession *))
{
FBSession *session = [FBSession activeSession];
NSString *publishPermission = #"publish_actions";
// If we already have publish permissions, then submit.
if (([session isOpen] && FB_ISSESSIONOPENWITHSTATE(session.state)) &&
[session.permissions containsObject:publishPermission])
{
block(session);
}
// Else request for the permissions, then submit.
else
{
[session requestNewPublishPermissions:#[publishPermission]
defaultAudience:FBSessionDefaultAudienceFriends
completionHandler:^(FBSession *session, NSError *error) {
if (!error)
{
block(session);
}
else
{
NSLog(#"Failed to request publish permissions: %#", error);
}
}];
}
}
You would then use your code in the following way:
void (^submit)(FBSession *) = ^(FBSession *session){
NSMutableDictionary* params = [NSMutableDictionary dictionary];
[params setObject:#"Some text" forKey:#"user_message_prompt"];
[params setObject:#"another text" forKey:#"action_links"];
[params setObject:#"Yet another text" forKey:#"attachment"];
[params setObject:#"SOME FACEBOOK ID" forKey:#"target_id"];
FBRequest *request = [[FBRequest alloc] initWithSession:session
graphPath:#"https://graph.facebook.com/100000329153640/feed"
parameters:params
HTTPMethod:#"POST"];
[request startWithCompletionHandler:nil];
};
runBlockWithPublishPermissions(submit);
I'm using this code to read the last post with location of all my facebook friends:
(FacebookFriend is an object I've created mySelf and contains also the friend's id and _facebookFriends contains all my facebook friends)
_postsWithLocationList = [[NSMutableArray alloc] init];
for (FacebookFriend *currentFriend in _facebookFriends) {
FBRequestConnection *connection = [[FBRequestConnection alloc] init];
NSString *path = [NSString stringWithFormat:#"%#/posts?limit=1&with=location",currentFriend.id];
FBRequest *postsWithLocationRequest = [FBRequest requestWithGraphPath:path parameters:nil HTTPMethod:#"GET"];
[connection addRequest:postsWithLocationRequest completionHandler:^(FBRequestConnection *connection, NSDictionary *result, NSError *error) {
NSDictionary *postData = [result objectForKey:#"data"];
if (postData.count != 0) //some people don't have any post with position
{
// how to extract "place" object?
}
}];
[connection start];
}
and then I would like to store every location in my NSMutable array _postsWithLocationList.
Once I've extracted *postData I printed it's content and it looked like a dictionary (where "place" is a key) exactly as in the Facebook Graph API Explorer.
But when I printed
NSLog(#"%i", postData.count);
I saw that the length of "postData" was always 1, so I think that it's a single object that contains all the instances of the post. I looked at https://developers.facebook.com but I don't understand the type of this object and how to extract it's content.