how to post text to friend wall in Facebook in iphone sdk? - iphone

i want to post text to friend wall but there is some problem by using this code...
-(IBAction)PostToBuddyWall
{
NSMutableDictionary *postVariablesDictionary = [[NSMutableDictionary alloc] init];
[postVariablesDictionary setObject:#"LOL" forKey:#"name"];
[postVariablesDictionary setObject:#"helllo" forKey:#"message"];
Facebook *fb = [((AppDelegate*)[[UIApplication sharedApplication] delegate]) fbInstance];
[fb requestWithGraphPath:[NSString stringWithFormat:#"%#/feed",self.fbFriendsInvited] andParams:postVariablesDictionary andHttpMethod:#"POST" andDelegate:nil];
NSLog(#"message: %#",postVariablesDictionary);
[postVariablesDictionary release];
UIAlertView *facebookAlter=[[UIAlertView alloc] initWithTitle:#"Message" message:#"Posted successfully on facebook" delegate:self cancelButtonTitle:#"OK" otherButtonTitles: nil, nil];
[facebookAlter show];
[facebookAlter release];
[self dismissViewControllerAnimated:YES completion:nil];
}
please give the suggestions if u have...
thanks !!

Try this :
NSMutableDictionary *postVariablesDictionary = [[NSMutableDictionary alloc] init];
[postVariablesDictionary setObject:#"LOL" forKey:#"name"];
[postVariablesDictionary setObject:#"helllo" forKey:#"message"];
//Post to friend's wall.
[FBRequestConnection startWithGraphPath:[NSString stringWithFormat:#"%#/feed", self.fbFriendsInvited] parameters: postVariablesDictionary HTTPMethod:#"POST"
completionHandler:^(FBRequestConnection *connection, id result, NSError *error)
{
NSLog(#"%#",result);
}];

Related

Facebook return com.facebook.sdk error 5 during Friend Wall Post

In my App I used Facebook sdk 3.1 and using that I try to do wall Post.
Using This Code Snippet I can successfully wall post Image and Message on Friend Wall and on my own wall.I just change me/photos and friendId/photos.
-(IBAction)postPhoto:(id)sender
{
UIImage *image=[UIImage imageNamed:#"baby.jpg"];
NSArray *frndArray=[[NSArray alloc] initWithObjects:#"Friend ID", nil];
[self postImageOnFriendsWall:image FriendsArray:frndArray];
}
-(void)postImageOnFriendsWall:(UIImage*)image FriendsArray:(NSArray*)friends
{
AppDelegate *appDelegate = [[UIApplication sharedApplication]delegate];
// if we don't have permission to announce, let's first address that
if ([appDelegate.session.permissions indexOfObject:#"publish_actions"] == NSNotFound)
{
NSArray *permissions =[NSArray arrayWithObjects:#"publish_actions",#"publish_stream",#"manage_friendlists", nil];
[FBSession setActiveSession:appDelegate.session];
[[FBSession activeSession] reauthorizeWithPublishPermissions:permissions defaultAudience:FBSessionDefaultAudienceFriends completionHandler:^(FBSession *session, NSError *error)
{
if (!error)
{
// have the permission
[self postImageOnFriendsWall:image FriendsArray:friends];
}
else
{
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:#"Error" message:error.localizedDescription delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alertView show];
}
}];
}
else
{
//post image
for (id<FBGraphUser> user in friends)
{
NSString* szMessage = #"Check out!";
NSString *userID = user;
NSLog(#"trying to post image of %# wall",userID);
NSMutableDictionary *postVariablesDictionary = [[NSMutableDictionary alloc] init];
[postVariablesDictionary setObject:UIImagePNGRepresentation(image) forKey:#"picture"];
[postVariablesDictionary setObject:szMessage forKey:#"message"];
[FBRequestConnection startWithGraphPath:[NSString stringWithFormat:#"%#/photos",userID] parameters:postVariablesDictionary HTTPMethod:#"POST" completionHandler:^(FBRequestConnection *connection, id result, NSError *error)
{
if (error)
{
//showing an alert for failure
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:#"Facebook"
message:error.localizedDescription delegate:nil cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[alertView show];
}
else
{
//showing an alert for success
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:#"Facebook"
message:#"Shared the photo successfully" delegate:nil cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[alertView show];
}
}];
}
}
}
For Message wall post I used me/feed and its work but for Friend friendID/feed will give me Error: HTTP status code: 403 and In alert box i got com.facebook.sdk error 5.
So Please guide me m I doing anything wrong?, or What I have to do for wall post.
Here is solution,
I update my FB SDK 3.1 to 3.2 then import
#import <FacebookSDK/FacebookSDK.h>
then update
[[FBSession activeSession] requestNewPublishPermissions:permissions defaultAudience:FBSessionDefaultAudienceFriends
completionHandler:^(FBSession *session, NSError *error) {}];
in place of
[[FBSession activeSession] reauthorizeWithPublishPermissions:permissions defaultAudience:FBSessionDefaultAudienceFriends completionHandler:^(FBSession *session, NSError *error){}];
and update
NSMutableDictionary *params = [NSMutableDictionary dictionaryWithObjectsAndKeys:
#"FRIEND ID", #"to",
#"text change", #"caption",
#"this is test message on ur wall", #"description",
#"https://website.com/share", #"link",
#"http://website.com/iossdk_logo.png", #"picture",
nil];
[FBWebDialogs presentFeedDialogModallyWithSession:nil
parameters:params
handler:
^(FBWebDialogResult result, NSURL *resultURL, NSError *error)
{}];
in place of
[FBRequestConnection startWithGraphPath:[NSString stringWithFormat:#"%#/photos",userID] parameters:postVariablesDictionary HTTPMethod:#"POST" completionHandler:^(FBRequestConnection *connection, id result, NSError *error){}];
and i got Dialog box and after click on share Successfully post on FRIEND's Wall.
Hope this snippet help someone else.
I am not sure. Try this one:
me?fields=friends.fields(feed)

iOS Facebook SDK 3.2 Image Publish w/Description Open Graph

No matter what I try I cannot get my description to show up on Facebook. I used the debugger and all is well with the generated link and the URL is populated with the appropriate data. The picture is uploaded fine, but the description is not there. Is there something I need to setup in Facebook settings for the app?
Here is the relevant code:
- (id<FBPost>)outfitObjectForOutfit{
id<FBPost> result = (id<FBPost>)[FBGraphObject graphObject];
NSString *format =
#"http://mysite.mobi/app/fbOpen.php?"
#"og:title=%#&og:description=%%22%#%%22&"
#"og:caption=%%22%#%%22&"
#"og:image=http://mysite.mobi/images/transBlank.png&"
#"body=%#";
result.url = [NSString stringWithFormat:format,
#"New Post Title",fldDescription.text,fldDescription.text,fldDescription.text];
return result;
}
And the portion that publishes to FB:
- (void)postOpenGraphActionWithPhotoURL:(NSString*)photoURL
{
id<FBPost> outfitObject = [self outfitObjectForOutfit];
id<FBPostOutfit> action =
(id<FBPostOutfit>)[FBGraphObject graphObject];
action.outfit=outfitObject;
if (photoURL) {
NSMutableDictionary *image = [[NSMutableDictionary alloc] init];
[image setObject:photoURL forKey:#"url"];
NSMutableArray *images = [[NSMutableArray alloc] init];
[images addObject:image];
action.image = images;
}
[FBSettings setLoggingBehavior:[NSSet
setWithObjects:FBLoggingBehaviorFBRequests,
FBLoggingBehaviorFBURLConnections,
nil]];
NSLog(#"%#",action);
NSMutableDictionary *params = [NSMutableDictionary dictionary];
[params setObject:fldDescription.text forKey:#"message"];
[FBRequestConnection startForPostWithGraphPath:#"me/appnamespace:action"
graphObject:action
completionHandler:
^(FBRequestConnection *connection, id result, NSError *error) {
NSString *alertText;
if (!error) {
alertText = [NSString stringWithFormat:
#"Posted Open Graph action, id: %#",
[result objectForKey:#"id"]];
} else {
alertText = [NSString stringWithFormat:
#"error: domain = %#, code = %d",
error.domain, error.code];
NSLog(#"%#",error);
}
[[[UIAlertView alloc] initWithTitle:#"Result"
message:alertText
delegate:nil
cancelButtonTitle:#"Thanks!"
otherButtonTitles:nil]
show];
}
];
}
I figured out my problem. I was confusing what the Facebook examples did (activity publishing) with publishing to a user's wall. This was all that I had to do to get it to work:
NSMutableDictionary* params = [[NSMutableDictionary alloc] init];
[params setObject:fldDescription.text forKey:#"message"];
[params setObject:UIImagePNGRepresentation(userImage) forKey:#"picture"];
[FBRequestConnection startWithGraphPath:#"me/photos"
parameters:params
HTTPMethod:#"POST"
completionHandler:^(FBRequestConnection *connection,
id result,
NSError *error)
{
if (error) {
[[[UIAlertView alloc] initWithTitle:#"Result"
message:#"Sorry there was an error posting to Facebook."
delegate:nil
cancelButtonTitle:#"OK"
otherButtonTitles:nil]
show];
}
}];
I should also mention that I had to go into the opengraph settings for my app and allow user messages and user generated photos.

How to post uiimage instead of image url from fbconnect feed dialog?

I need to post uiimage (screenshot taken from app) instead of url to post in facebook from facebook dialog along with user option to enter text. how it can be achieved. some help is appreciated.
You could try something like this to post the photo part. I'm sure the text is just another field.
#try {
// make sure your permissions are set
NSArray *permissions = [[NSArray alloc] initWithObjects:
#"publish_stream",
nil];
[FBSession openActiveSessionWithPublishPermissions:permissions
defaultAudience:FBSessionDefaultAudienceOnlyMe
allowLoginUI:NO
completionHandler:^(FBSession *session, FBSessionState state, NSError *error){
NSLog(#"completion handler.");
}];
[FBRequestConnection startForUploadPhoto:imageToUpload
completionHandler:^(FBRequestConnection *connection, id result, NSError *error) {
if (!error) {
UIAlertView *tmp = [[UIAlertView alloc]
initWithTitle:#"Success"
message:#"Photo Uploaded!"
delegate:self
cancelButtonTitle:nil
otherButtonTitles:#"Ok", nil];
[tmp show];
} else {
UIAlertView *tmp = [[UIAlertView alloc]
initWithTitle:#"Error"
message:#"Error uploading image to Facebook."
delegate:self
cancelButtonTitle:nil
otherButtonTitles:#"Ok", nil];
[tmp show];
}
}];
}
#catch (NSException *exception) {
NSLog(#"-----> !! ERROR UPLOADING IMAGE TO FACEBOOK !!");
}
to post image in facebook through fbconnect few steps need to follow with the fbconnect api.
need to login in facebook then it check about the access key and the expiry date.
if those are not nil, there it go to post the image i.e
NSData *imageData = UIImagePNGRepresentation(cellImageView.image);
NSMutableDictionary *params = [[NSMutableDictionary alloc]initWithObjectsAndKeys:#"app name",#"message",imageData,#"source", nil];
[facebook requestWithGraphPath:#"me/photos" andParams:params andHttpMethod:#"POST" andDelegate:self];
if not it go for the access key and expiry key and then to the image post code as written above.
for more download fbconnect api and look at the login and have a look on the code with brakepoints.
for this user need the login delegate and fb request delegate.
also don't forget to initialize facebook obj with app id and to set its delegate self.

How to resolve property array not found on object of type "AppDelegate" error

I'm trying to post to a user's friend's FB wall with the following code. I'm stuck on this line: ** NSString *post=[[delegate.array objectAtIndex:tag.tag] objectForKey:#"id"];
**
with the error message: "property array not found on object of type "AppDelegate"
AppDelegate *delegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
//Call the request method
[[delegate facebook] requestWithGraphPath:#"me/friends" andDelegate:self];
// get the array of friends
NSArray *data = [result objectForKey:#"data"];
// Check that the user has friends
if ([data count] > 0) {
NSMutableArray *array = [NSMutableArray array];
for (int i = 0; i < data.count; i++){
id object = [data objectAtIndex:i];
[array addObject:[object objectForKey:#"name"]];
}
NSLog(#"list of friends %#", array);
NSString *Message = [NSString stringWithFormat:#"-posted via iPhone App"];
NSMutableDictionary *params1 = [NSMutableDictionary dictionaryWithObjectsAndKeys:
Message, #"message", nil];
NSString *post=[[delegate.array objectAtIndex:tag.tag] objectForKey:#"id"];
[[delegate facebook] requestWithGraphPath:[NSString stringWithFormat:#"/%#/feed",post] andParams:params1 andHttpMethod:#"POST" andDelegate:self];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Message!" message:#"Message Sent" delegate:self cancelButtonTitle:#"OK" otherButtonTitles: nil];
[alert show];
[alert release];
Last 3 days i realize you got problem with get facebook friend in iphone. so i make a demo for you . hope you enjoy this.:)
Fetch Facebook friend here

How to post images in facebook in iphone app?

could anyone pls help me to post fotos on facebook using objective c for iphone app.i tried it but its getting terminated when i check with iphone.its working properly in simulator.following is my code i used to post in fb.i use graph api for developing my app.
-(void)fbGraphCallback:(id)sender {
if ((fbGraph.accessToken == nil) || ([fbGraph.accessToken length] == 0)) {
NSLog(#"You pressed the 'cancel' or 'Dont Allow' button, you are NOT logged into Facebook...I require you to be logged in & approve access before you can do anything useful....");
//restart the authentication process.....
[fbGraph authenticateUserWithCallbackObject:self
andSelector:#selector(fbGraphCallback:)
andExtendedPermissions:#"user_photos,user_videos,publish_stream,offline_access,user_checkins,friends_checkins"];
[self.view addSubview:viewshare];
}
else {
NSMutableDictionary *variables = [NSMutableDictionary dictionaryWithCapacity:2];
//imgPicture is my image view name
NSLog(#"the Data::%#\n",imgPicture.image);
FbGraphFile *graph_file = [[FbGraphFile alloc] initWithImage:imgPicture.image];
[variables setObject:graph_file forKey:#"file"];
[variables setObject:[NSString stringWithFormat:#"%#", txtComment.text] forKey:#"message"];
FbGraphResponse *fb_graph_response = [fbGraph doGraphPost:#"me/photos" withPostVars:variables];
NSLog(#"postPictureButtonPressed: %#", fb_graph_response.htmlResponse);
NSLog(#"Now log into Facebook and look at your profile & photo albums...");
txtComment.text=#" ";
[txtComment setHidden:YES];
[lblcmt setHidden:YES];
UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:nil message:#"Successfully posted...Now log into Facebook & look at your profile" delegate:nil cancelButtonTitle:#"Ok" otherButtonTitles:nil];
[alertView show];
[alertView release];
}
[fbGraph release];
}
First I check if facebook(the facebook object) has a valid session:
if (![facebook_ isSessionValid]) {
permissions_ = [[NSArray arrayWithObjects:#"read_stream", #"publish_stream", #"offline_access",nil] retain];
[facebook_ authorize:permissions_];
}
When I can guaranty that i'm logged into facebook I post the image like this:
NSMutableDictionary *params = [NSMutableDictionary dictionaryWithObjectsAndKeys:
image, #"picture",
message, #"message",
nil];
[facebook_ requestWithGraphPath:#"me/photos"
andParams:params
andHttpMethod:#"POST"
andDelegate:self];
Finally I check this methods in for being sure if the image post was succesful or not:
-(void)request:(FBRequest *)request didFailWithError:(NSError *)error {
//Error
}
-(void)request:(FBRequest *)request didLoad:(id)result {
//Succes
}