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

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.

Related

Get users email from Fackbook

I have just made a new app and I have it all set up with Facebook login, and I have been approved for the email permission, but I seem to not to be getting the users email, I think I might be missing something in my code here is what I have:
// Set permissions required from the facebook user account
NSArray *permissionsArray = #[ #"user_about_me", #"user_relationships", #"user_likes", #"user_location", #"email", #"basic_info", #"user_activities", #"user_interests", #"user_birthday"];
// Login PFUser using facebook
[PFFacebookUtils logInWithPermissions:permissionsArray block:^(PFUser *user, NSError *error) {
if (!user) {
if (!error) {
NSLog(#"Uh oh. The user cancelled the Facebook login.");
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Log In Error" message:#"Uh oh. The user cancelled the Facebook login." delegate:nil cancelButtonTitle:nil otherButtonTitles:#"Dismiss", nil];
[alert show];
} else {
NSLog(#"Uh oh. An error occurred: %#", error);
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Log In Error" message:[error description] delegate:nil cancelButtonTitle:nil otherButtonTitles:#"Dismiss", nil];
[alert show];
}
} else if (user.isNew) {
user[#"school"] = #"";
NSLog(#"User with facebook signed up and logged in!");
}
else {
NSLog(#"User with facebook logged in!");
}
}
}];
If you could show of tell me what I am missing/ why I am not seeing my users email. I would really appreciate it. By the way I am using Parse.com as a backend.
logInWithPermissions: method doesn't save the email attribute by default when it creates a new user. If you want to save the email of a Facebook user, after verifying that the user is already authenticated, you need to call startForMeWithCompletionHandler: method of FBRequestConnection class. Your code should look like this in the last part:
NSLog(#"User with facebook logged in!");
[FBRequestConnection startForMeWithCompletionHandler:^(FBRequestConnection *connection, id result, NSError *error) {
if (!error) {
[[PFUser currentUser] setObject:[result objectForKey:#"email"]
forKey:#"email"];
[[PFUser currentUser] saveInBackground];
}
}];

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

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

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)

Image Post on Facebook?

In my application. I successfully post a image on my Facebook wall (image type JPEG Type). It's work fine in Simulator and and Device also.
But the problem is that When I Capture Image from my iPhone or select an image from library which is captured by camera. It is not Posted on Facebook Wall.
on other hand when i choose any jpeg image (downloaded by internet) from my iPhone library. It post on wall.
In my opinion there is image format problem. Please Guide me
Thanks
My code is here.
-(IBAction)clickButtonClicked
{
// For link post
NSString *Message = [NSString stringWithFormat:#"Testing Iphone App : \"http://www.google.com\" "];
DataClass *obj=[DataClass getInstance];
UIImage *image=[[UIImage alloc]init ];
image= obj.imgFinalImage;
NSMutableDictionary *params1 = [[NSMutableDictionary alloc]init];
[params1 setObject:image forKey:#"source"];
[params1 setObject:Message forKey:#"message"];
NSString *post=#"/me/photos";
[[appDelegate facebook] requestWithGraphPath:post andParams:params1 andHttpMethod:#"POST" andDelegate:self];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Message!" message:#"Invitation Send Sucessfully" delegate:self cancelButtonTitle:#"OK" otherButtonTitles: nil];
[alert show];
[alert release];
}
My guess is that you pass the image in the wrong property:
NSMutableDictionary *params = [NSDictionary dictionaryWithObjectsAndKeys:
sobj.imgFinalImage, #"picture",
Message, #"caption",
nil];
[[appDelegate facebook] requestWithMethodName:#"photos.upload"
andParams:params
andHttpMethod:#"POST"
andDelegate:self];
Then make sure you implement the delegate of Facbook request to check if the image is uploaded.
With the graph API you can't send a UIImage directly, you need to send the NSData representation of the UIImage:
NSMutableDictionary *params1 = [[NSMutableDictionary alloc]init];
NSData *imageDate = UIImagePNGRepresentation(obj.imgFinalImage);
[params1 imageDate forKey:#"source"];
[params1 setObject:Message forKey:#"message"];
NSString *post=#"/me/photos";
[[appDelegate facebook] requestWithGraphPath:post andParams:params1 andHttpMethod:#"POST" andDelegate:self];

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
}