posting image on facebook wall xcode ios - ios

i am using facebook sdk 3.0 for posting image on facebook wall.
when I am posting image on Facebook wall it is posting only from developer id not any other IDs.
user login from his facebook account to upload photos on his wall.If user login from developer account then image is posting on wall and from other account it is not posting.
-(void)uploadPhoto {
if (FBSession.activeSession.isOpen) {
[self promptUserWithAccountNameForUploadPhoto];
} else {
[FBSession openActiveSessionWithPermissions:permissions
allowLoginUI:YES
completionHandler:^(FBSession *session,
FBSessionState status,
NSError *error) {
if (error) {
} else
if(FB_ISSESSIONOPENWITHSTATE(status))
{
[self promptUserWithAccountNameForUploadPhoto];
}
}];
}
}
-(void)promptUserWithAccountNameForUploadPhoto {
[[FBRequest requestForMe] startWithCompletionHandler:
^(FBRequestConnection *connection, NSDictionary<FBGraphUser> *user, NSError *error) {
if (!error) {
UIAlertView *tmp = [[UIAlertView alloc]
initWithTitle:#"Upload to FB?"
message:[NSString stringWithFormat:#"Upload to ""%#"" Account?", user.name]
delegate:self
cancelButtonTitle:nil
otherButtonTitles:#"No",#"Yes", nil];
tmp.tag = 100;
[tmp show];
}
}];
}
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
if (buttonIndex==1) {
if (alertView.tag==100) {
[FBRequestConnection startForUploadPhoto:sampleImage
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:#"Some error happened"
delegate:self
cancelButtonTitle:nil
otherButtonTitles:#"Ok", nil];
[tmp show];
}
}];
}
if (alertView.tag==200) {
[FBRequestConnection startForPostStatusUpdate:myStatus.text completionHandler:^(FBRequestConnection *connection, id result, NSError *error) {
if (!error) {
UIAlertView *tmp = [[UIAlertView alloc]
initWithTitle:#"Success"
message:#"Status Posted"
delegate:self
cancelButtonTitle:nil
otherButtonTitles:#"Ok", nil];
[tmp show];
} else {
UIAlertView *tmp = [[UIAlertView alloc]
initWithTitle:#"Error"
message:#"Some error happened"
delegate:self
cancelButtonTitle:nil
otherButtonTitles:#"Ok", nil];
[tmp show];
}
}];
}
}
}

In your facebook application you have a option to set on/off developer mode for your application.
If facebook application is not live you can only post with developer accounts

Related

Facebook: Share to specific people or list from iOS app

If you share the news to Facebook on your computer you have the options to share for "Specific people or list".
I need to share photo from my iOS app only for some list of friends.
Is it possible to share news only for "Specific people or list" from app using Graph API?
For post on user's wall using Social Framework
in ACFacebookAudienceKey, choose one of these
1.ACFacebookAudienceEveryone
2.ACFacebookAudienceFriends
3.ACFacebookAudienceOnlyMe
ACAccountStore *accountStore = [[ACAccountStore alloc] init];
ACAccountType *accountType = [accountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierFacebook];
NSLog(#"0");
[accountStore requestAccessToAccountsWithType:accountType options:#{ACFacebookAppIdKey : #"00000000000", ACFacebookPermissionsKey : #"publish_stream", ACFacebookAudienceKey : ACFacebookAudienceFriends} completion:^(BOOL granted, NSError *error) {
if(granted) {
NSLog(#"1");
NSArray *accountsArray = [accountStore accountsWithAccountType:accountType];
NSLog(#"2");
if ([accountsArray count] > 0) {
NSLog(#"3");
ACAccount *facebookAccount = [accountsArray objectAtIndex:0];
NSLog(#"4");
SLRequest *facebookRequest = [SLRequest requestForServiceType:SLServiceTypeFacebook
requestMethod:SLRequestMethodPOST
URL:[NSURL URLWithString:#"https://graph.facebook.com/me/feed"]
parameters:[NSDictionary dictionaryWithObject:post forKey:#"message"]];
NSLog(#"5");
[facebookRequest setAccount:facebookAccount];
NSLog(#"6");
[facebookRequest performRequestWithHandler:^(NSData* responseData, NSHTTPURLResponse* urlResponse, NSError* error) {
NSLog(#"%#", [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding]);
}];
}
}
}];
For post to friend's wall.
- (IBAction)InviteAction:(id)sender // Button action
{
if (!FBSession.activeSession.isOpen) {
// if the session is closed, then we open it here, and establish a handler for state changes
[FBSession openActiveSessionWithReadPermissions:nil
allowLoginUI:YES
completionHandler:^(FBSession *session,
FBSessionState state,
NSError *error) {
if (error) {
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:#"Invite friends process cancelled"
message:nil
delegate:nil
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[alertView show];
} else if (session.isOpen) {
[self InviteAction:sender];
}
}];
return;
}
if (self.friendPickerController == nil) {
// Create friend picker, and get data loaded into it.
self.friendPickerController = [[FBFriendPickerViewController alloc] init];
self.friendPickerController.title = #"Pick Friends";
self.friendPickerController.delegate = self;
}
[self.friendPickerController loadData];
[self.friendPickerController clearSelection];
[self presentViewController:self.friendPickerController animated:YES completion:nil];
}
- (void) performPublishAction:(void (^)(void)) action
{
if ([FBSession.activeSession.permissions indexOfObject:#"publish_actions"] == NSNotFound)
{
[FBSession.activeSession requestNewPublishPermissions:#[#"publish_actions"]
defaultAudience:FBSessionDefaultAudienceFriends
completionHandler:^(FBSession *session, NSError *error) {
if (!error) {
action();
} else if (error.fberrorCategory != FBErrorCategoryUserCancelled){
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:#"Permission denied"
message:#"Unable to get permission to post"
delegate:nil
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[alertView show];
}
}];
} else {
action();
}
}
- (void)loginViewFetchedUserInfo:(FBLoginView *)loginView
user:(id<FBGraphUser>)user
{
self.loggedInUser = user;
}
- (void)facebookViewControllerDoneWasPressed:(id)sender
{
NSMutableString *text = [[NSMutableString alloc] init];
for (id<FBGraphUser> user in self.friendPickerController.selection)
{
if ([text length]) {
[text appendString:#","];
}
[text appendString:[NSString stringWithFormat:#"%#",user.id]];
}
//For post to friend's wall
NSDictionary *params = #{
#"name" : #"Hello Please checkout this app",
#"caption" : #" IOS APP",
#"description" : #"",
#"picture" : #"logo#2x.png",
#"link" : #"http:www.google.com",
#"to":text,
};
// Invoke the dialog
[FBWebDialogs presentFeedDialogModallyWithSession:nil
parameters:params
handler:
^(FBWebDialogResult result, NSURL *resultURL, NSError *error) {
if (error) {
NSLog(#"Error publishing story.");
UIAlertView *alertshow = [[UIAlertView alloc]initWithTitle:#"Failed" message:#"Failed to Post" delegate:Nil cancelButtonTitle:#"ok" otherButtonTitles:nil];
[alertshow show];
} else {
if (result == FBWebDialogResultDialogNotCompleted)
{
NSLog(#"User canceled story publishing.");
UIAlertView *alertshow = [[UIAlertView alloc]initWithTitle:#"Failed" message:#"Failed to post on your friend wall" delegate:Nil cancelButtonTitle:#"ok" otherButtonTitles:nil];
[alertshow show];
} else {
NSLog(#"Story published.");
UIAlertView *alertshow = [[UIAlertView alloc]initWithTitle:#"Success" message:#"Posted on Friend wall" delegate:Nil cancelButtonTitle:#"ok" otherButtonTitles:nil];
[alertshow show];
}
}}];
[self fillTextBoxAndDismiss:text.length > 0 ? text : #"<None>"];
}
- (void)facebookViewControllerCancelWasPressed:(id)sender {
[self fillTextBoxAndDismiss:#"<Cancelled>"];
}
- (void)fillTextBoxAndDismiss:(NSString *)text
{
[self dismissModalViewControllerAnimated:YES];
}

Setting of facebook app for invite friend

It may be an easy question, but I am tired of it
Is there any setting for Facebook app (Which we set while creating an App in Facebook developer's site) for making "invite friend" functionality to be worked.
I am using WebDialog method as follows,
FBWebDialogs presentRequestsDialogModallyWithSession:nil
message:FacebookInviteMessage
title:#"Invite Friends"
parameters:parameters
handler:^(FBWebDialogResult result, NSURL *resultURL, NSError *error)
With the same method I am able to invite friend, when I am using the Facebook APP ID of another sample code in my project. But when I am using my own project's Facebook APP ID, invite is not working when I am getting Success in result url.
So I think there would be some setting in developer's site of Facebook.
Can anyone help for this
The complete code is
NSDictionary *parameters = self.fbidSelection ? #{#"to":self.fbidSelection} : nil;
MIDLog(#"self.fbidSelection %#",self.fbidSelection);
[FBWebDialogs presentRequestsDialogModallyWithSession:nil
message:FacebookInviteMessage
title:#"Invite Friends"
parameters:parameters
handler:^(FBWebDialogResult result, NSURL *resultURL, NSError *error) {
NSString *strResultUrl = [resultURL absoluteString];
if (result == FBWebDialogResultDialogCompleted && ![strResultUrl isEqualToString:#"fbconnect://success"])
{
MIDLog(#"Web dialog complete: %#", resultURL);
if (![resultURL query])
{
return;
}
NSDictionary *params = [self parseURLParams:[resultURL query]];
NSMutableArray *recipientIDs = [[NSMutableArray alloc] init];
for (NSString *paramKey in params)
{
if ([paramKey hasPrefix:#"to["])
{
[recipientIDs addObject:[params objectForKey:paramKey]];
}
}
if ([params objectForKey:#"request"])
{
NSLog(#"Request ID: %#", [params objectForKey:#"request"]);
}
if ([recipientIDs count] > 0)
{
//[self showMessage:#"Sent request successfully."];
//NSLog(#"Recipient ID(s): %#", recipientIDs);
UIAlertView *alrt = [[UIAlertView alloc] initWithTitle:#"Success!" message:#"Invitation(s) sent successfuly!" delegate:nil cancelButtonTitle:#"OK" otherButtonTitles: nil];
[alrt show];
alrt = nil;
}
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Success" message:FacebookInviteSuccess delegate:self cancelButtonTitle:#"Proceed" otherButtonTitles:nil];
alert.tag = 14114;
[alert show];
alert = nil;
// [self navigateToSearch];
}
else if (result == FBWebDialogResultDialogCompleted && [strResultUrl isEqualToString:#"fbconnect://success"])
{
MIDLog(#"Cancel Clicked");
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Cancelled" message:nil delegate:self cancelButtonTitle:#"Proceed" otherButtonTitles:nil];
alert.tag = 14114;
[alert show];
alert = nil;
}
else {
MIDLog(#"Web dialog not complete, error: %#", error.description);
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Error" message:InternetFailError delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil];
alert.tag = 15115;
[alert show];
alert = nil;
}
}
friendCache:self.friendCache];
}
Thank you

Facebook setting for Invite friend in Facebook

It may be an easy question but can anybody tell me.
Is there any setting we need to do in the app (in Facebook developer site) for invite friends?
I have done all in code to invite friends.
When I substitute the Facebook APP ID of a sample code in which invite friend is working, then my code is also working fine.
But when I substitute my APP ID then the process is going fine, showing me successfully invitation message, but invitation is not sent.
I am using the following code:
[FBWebDialogs presentRequestsDialogModallyWithSession:nil
message:FacebookInviteMessage
title:#"Invite Friends"
parameters:parameters
handler:^(FBWebDialogResult result, NSURL *resultURL, NSError *error) {
NSString *strResultUrl = [resultURL absoluteString];
if (result == FBWebDialogResultDialogCompleted && ![strResultUrl isEqualToString:#"fbconnect://success"])
{
MIDLog(#"Web dialog complete: %#", resultURL);
if (![resultURL query])
{
return;
}
NSDictionary *params = [self parseURLParams:[resultURL query]];
NSMutableArray *recipientIDs = [[NSMutableArray alloc] init];
for (NSString *paramKey in params)
{
if ([paramKey hasPrefix:#"to["])
{
[recipientIDs addObject:[params objectForKey:paramKey]];
}
}
if ([params objectForKey:#"request"])
{
NSLog(#"Request ID: %#", [params objectForKey:#"request"]);
}
if ([recipientIDs count] > 0)
{
//[self showMessage:#"Sent request successfully."];
//NSLog(#"Recipient ID(s): %#", recipientIDs);
UIAlertView *alrt = [[UIAlertView alloc] initWithTitle:#"Success!" message:#"Invitation(s) sent successfuly!" delegate:nil cancelButtonTitle:#"OK" otherButtonTitles: nil];
[alrt show];
alrt = nil;
}
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Success" message:FacebookInviteSuccess delegate:self cancelButtonTitle:#"Proceed" otherButtonTitles:nil];
alert.tag = 14114;
[alert show];
alert = nil;
}
else if (result == FBWebDialogResultDialogCompleted && [strResultUrl isEqualToString:#"fbconnect://success"])
{
MIDLog(#"Cancel Clicked");
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Cancelled" message:nil delegate:self cancelButtonTitle:#"Proceed" otherButtonTitles:nil];
alert.tag = 14114;
[alert show];
alert = nil;
}
else {
MIDLog(#"Web dialog not complete, error: %#", error.description);
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Error" message:InternetFailError delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil];
alert.tag = 15115;
[alert show];
alert = nil;
}
}
friendCache:self.friendCache]
I provided the canvas URL in the application settings under Facebook developer site and It works for me

How to post multiple pictures with an open graph request to facebook?

Code that posts one image:
-(IBAction)postImageToFacebook:(id)sender {
DLog(#"");
NSMutableDictionary* params = [[NSMutableDictionary alloc] init];
[params setObject:self.textField.text forKey:#"message"];
DLog(#"%#",[params objectForKey:#"message"]);
if(self.imageView.image){
DLog(#"not null");
UIImage *_img = self.imageView.image;
[params setObject:UIImagePNGRepresentation(_img) forKey:#"picture"];
}
[self performPublishAction:^{
[FBRequestConnection startWithGraphPath:#"me/photos"
parameters:params
HTTPMethod:#"POST"
completionHandler:^(FBRequestConnection *connection,
id result,
NSError *error)
{
if (error)
{
//showing an alert for failure
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Error"
message:#"unable to post"
delegate:nil
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[alert show];
}
else
{
//showing an alert for success
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Posted!"
message:#""
delegate:nil
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[alert show];
}
}];
}];
}
Can I add another object for a duplicate key into the NSMutableDictionary?

How to post to a friends Wall using Facebook Graph API for iPhone

I want to post something in the users friends wall.
I use this to post into the user wall
SBJSON *jsonWriter = [[SBJSON new] autorelease];
NSDictionary* actionLinks = [NSArray arrayWithObjects:[NSDictionary dictionaryWithObjectsAndKeys:
#"Always Running",#"text",#"http://itsti.me/",#"href", nil], nil];
NSString *actionLinksStr = [jsonWriter stringWithObject:actionLinks];
NSDictionary* attachment = [NSDictionary dictionaryWithObjectsAndKeys:
#"a long run", #"name",
#"The Facebook Running app", #"caption",
#"it is fun", #"description",
#"http://itsti.me/", #"href", nil];
NSString *attachmentStr = [jsonWriter stringWithObject:attachment];
NSMutableDictionary* params = [NSMutableDictionary dictionaryWithObjectsAndKeys:
#"Share on Facebook", #"user_message_prompt",
actionLinksStr, #"action_links",
attachmentStr, #"attachment",nil];
[facebook dialog:#"feed"
andParams:params
andDelegate:self];
But i need to post in my friends wall. how can i do this
In this post they have mentioned about targetId do i have to do something with that.
Have you tried using,
[facebook requestWithGraphPath:#"[friend_ID]/feed" andParams:params andHttpMethod:#"POST" andDelegate:self];
UPDATE:
Posting on friends wall will no more work with Graph API. Instead you must use FBDialog.
#mAc
You have to authorize facebook first. Then you implement 'FBRequestDelegate' methods to your class. Then make a request with graph API as given below
[facebook requestWithGraphPath:#"/me/friends" andDelegate:self];
Once your request succeeded, api will call the delegate method 'requestDdidLoad',
-(void)request:(FBRequest *)request didLoad:(id)result {
NSLog(#"Result: %#", result);
}
Form the result you will get the frieds page ID
- (IBAction)InviteAction:(id)sender // Button action
{
if (!FBSession.activeSession.isOpen) {
// if the session is closed, then we open it here, and establish a handler for state changes
[FBSession openActiveSessionWithReadPermissions:nil
allowLoginUI:YES
completionHandler:^(FBSession *session,
FBSessionState state,
NSError *error) {
if (error) {
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:#"Invite friends process cancelled"
message:nil
delegate:nil
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[alertView show];
} else if (session.isOpen) {
[self InviteAction:sender];
}
}];
return;
}
if (self.friendPickerController == nil) {
// Create friend picker, and get data loaded into it.
self.friendPickerController = [[FBFriendPickerViewController alloc] init];
self.friendPickerController.title = #"Pick Friends";
self.friendPickerController.delegate = self;
}
[self.friendPickerController loadData];
[self.friendPickerController clearSelection];
[self presentViewController:self.friendPickerController animated:YES completion:nil];
}
- (void) performPublishAction:(void (^)(void)) action
{
if ([FBSession.activeSession.permissions indexOfObject:#"publish_actions"] == NSNotFound)
{
[FBSession.activeSession requestNewPublishPermissions:#[#"publish_actions"]
defaultAudience:FBSessionDefaultAudienceFriends
completionHandler:^(FBSession *session, NSError *error) {
if (!error) {
action();
} else if (error.fberrorCategory != FBErrorCategoryUserCancelled){
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:#"Permission denied"
message:#"Unable to get permission to post"
delegate:nil
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[alertView show];
}
}];
} else {
action();
}
}
- (void)loginViewFetchedUserInfo:(FBLoginView *)loginView
user:(id<FBGraphUser>)user
{
self.loggedInUser = user;
}
- (void)facebookViewControllerDoneWasPressed:(id)sender
{
NSMutableString *text = [[NSMutableString alloc] init];
for (id<FBGraphUser> user in self.friendPickerController.selection)
{
if ([text length]) {
[text appendString:#","];
}
[text appendString:[NSString stringWithFormat:#"%#",user.id]];
}
//For post to friend's wall
NSDictionary *params = #{
#"name" : #"Hello Please checkout this app",
#"caption" : #" IOS APP",
#"description" : #"",
#"picture" : #"logo#2x.png",
#"link" : #"http:www.google.com",
#"to":text,
};
// Invoke the dialog
[FBWebDialogs presentFeedDialogModallyWithSession:nil
parameters:params
handler:
^(FBWebDialogResult result, NSURL *resultURL, NSError *error) {
if (error) {
NSLog(#"Error publishing story.");
UIAlertView *alertshow = [[UIAlertView alloc]initWithTitle:#"Failed" message:#"Failed to Post" delegate:Nil cancelButtonTitle:#"ok" otherButtonTitles:nil];
[alertshow show];
} else {
if (result == FBWebDialogResultDialogNotCompleted)
{
NSLog(#"User canceled story publishing.");
UIAlertView *alertshow = [[UIAlertView alloc]initWithTitle:#"Failed" message:#"Failed to post on your friend wall" delegate:Nil cancelButtonTitle:#"ok" otherButtonTitles:nil];
[alertshow show];
} else {
NSLog(#"Story published.");
UIAlertView *alertshow = [[UIAlertView alloc]initWithTitle:#"Success" message:#"Posted on Friend wall" delegate:Nil cancelButtonTitle:#"ok" otherButtonTitles:nil];
[alertshow show];
}
}}];
[self fillTextBoxAndDismiss:text.length > 0 ? text : #"<None>"];
}
- (void)facebookViewControllerCancelWasPressed:(id)sender {
[self fillTextBoxAndDismiss:#"<Cancelled>"];
}
- (void)fillTextBoxAndDismiss:(NSString *)text
{
[self dismissModalViewControllerAnimated:YES];
}

Resources