Post to Facebook Friend's Wall in iPhone using New Facebook SDK - iphone

I want to post some texts and photo to one of my friend's wall using FBDialog.
I have a method like this
- (void)apiDialogFeedFriend:(NSString *)friendID :(NSString *)mImageSTR{
currentAPICall = kDialogFeedFriend;
FBSBJSON *jsonWriter = [FBSBJSON new];
NSMutableDictionary *params = [NSMutableDictionary dictionaryWithObjectsAndKeys:
friendID, #"to",
#"I'm using the Hackbook for iOS app", #"name",
#"Hackbook for iOS.", #"caption",
#"Check out Hackbook for iOS to learn how you can make your iOS apps social using Facebook Platform.", #"description",
#"http://m.facebook.com/apps/hackbookios/", #"link",
#"http://www.facebookmobileweb.com/hackbook/img/facebook_icon_large.png", #"picture",
actionLinksStr, #"actions",
nil];
AppDelegate *delegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
[[delegate facebook] dialog:#"feed"
andParams:params
andDelegate:self];
}
Through these i get call back to dialogCompleteWithUrl method of FBDialogDelegate.
I have implemented this delegate method like this :
- (void)dialogCompleteWithUrl:(NSURL *)url {
if (![url query]) {
return;
}
NSDictionary *params = [self parseURLParams:[url query]];
switch (currentAPICall) {
case kDialogFeedUser:
case kDialogFeedFriend:
{
// Successful posts return a post_id
if ([params valueForKey:#"post_id"]) {
[self showMessage:#"Published feed successfully."];
NSLog(#"Feed post ID: %#", [params valueForKey:#"post_id"]);
}
break;
}
case kDialogRequestsSendToMany:
case kDialogRequestsSendToSelect:
case kDialogRequestsSendToTarget:
{
// Successful requests return the id of the request
// and ids of recipients.
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);
}
break;
}
default:
break;
}
}
Here I get correct value of url as fbconnect://success?post_id=100001402819851_418640698226650.
Now, my problem is that in my friend wall my post is not seen/visible.
Any help would be appreciated.

Related

How to post to a users wall using Facebook SDK

I want to post some text to a users wall using the facebook sdk in an iOS app.
Is posting an open graph story now the only way to do that?
I've found with open graph stories they are really strange, you can only post things in the format "user x a y" where you preset x and y directly on facebook, like user ata a pizza or user played a game. Setting up each one is pretty laborious too because you have to create a .php object on an external server for each one.
Am I missing something or is there a simpler way to go about this?
Figured it out by browsing the facebook tutorials a bit more.
-(void) postWithText: (NSString*) message
ImageName: (NSString*) image
URL: (NSString*) url
Caption: (NSString*) caption
Name: (NSString*) name
andDescription: (NSString*) description
{
NSMutableDictionary* params = [[NSMutableDictionary alloc] initWithObjectsAndKeys:
url, #"link",
name, #"name",
caption, #"caption",
description, #"description",
message, #"message",
UIImagePNGRepresentation([UIImage imageNamed: image]), #"picture",
nil];
if ([FBSession.activeSession.permissions indexOfObject:#"publish_actions"] == NSNotFound)
{
// No permissions found in session, ask for it
[FBSession.activeSession requestNewPublishPermissions: [NSArray arrayWithObject:#"publish_actions"]
defaultAudience: FBSessionDefaultAudienceFriends
completionHandler: ^(FBSession *session, NSError *error)
{
if (!error)
{
// If permissions granted and not already posting then publish the story
if (!m_postingInProgress)
{
[self postToWall: params];
}
}
}];
}
else
{
// If permissions present and not already posting then publish the story
if (!m_postingInProgress)
{
[self postToWall: params];
}
}
}
-(void) postToWall: (NSMutableDictionary*) params
{
m_postingInProgress = YES; //for not allowing multiple hits
[FBRequestConnection startWithGraphPath:#"me/feed"
parameters:params
HTTPMethod:#"POST"
completionHandler:^(FBRequestConnection *connection,
id result,
NSError *error)
{
if (error)
{
//showing an alert for failure
UIAlertView *alertView = [[UIAlertView alloc]
initWithTitle:#"Post Failed"
message:error.localizedDescription
delegate:nil
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[alertView show];
}
m_postingInProgress = NO;
}];
}
the easiest way of sharing something from your iOS app is using the UIActivityViewController class, here you can find the documentation of the class and here a good example of use. It is as simple as:
NSString *textToShare = #”I just shared this from my App”;
UIImage *imageToShare = [UIImage imageNamed:#"Image.png"];
NSURL *urlToShare = [NSURL URLWithString:#"http://www.bronron.com"];
NSArray *activityItems = #[textToShare, imageToShare, urlToShare];
UIActivityViewController *activityVC = [[UIActivityViewController alloc]initWithActivityItems:activityItems applicationActivities:nil];
[self presentViewController:activityVC animated:TRUE completion:nil];
This will only work on iOS 6 and it makes use of the Facebook account configured in the user settings, and the Facebook SDK is not needed.
You can use Graph API as well.
After all the basic steps to create facebook app with iOS, you can start to enjoy the functionality of Graph API. The code below will post "hello world!" on your wall:
#import <FBSDKCoreKit/FBSDKCoreKit.h>
#import <FBSDKLoginKit/FBSDKLoginKit.h>
...
//to get the permission
//https://developers.facebook.com/docs/facebook-login/ios/permissions
if ([[FBSDKAccessToken currentAccessToken] hasGranted:#"publish_actions"]) {
NSLog(#"publish_actions is already granted.");
} else {
FBSDKLoginManager *loginManager = [[FBSDKLoginManager alloc] init];
[loginManager logInWithPublishPermissions:#[#"publish_actions"] handler:^(FBSDKLoginManagerLoginResult *result, NSError *error) {
//TODO: process error or result.
}];
}
if ([[FBSDKAccessToken currentAccessToken] hasGranted:#"publish_actions"]) {
[[[FBSDKGraphRequest alloc]
initWithGraphPath:#"me/feed"
parameters: #{ #"message" : #"hello world!"}
HTTPMethod:#"POST"]
startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection, id result, NSError *error) {
if (!error) {
NSLog(#"Post id:%#", result[#"id"]);
}
}];
}
...
The basic staff is presented here: https://developers.facebook.com/docs/ios/graph
The explorer to play around is here:
https://developers.facebook.com/tools/explorer
A good intro about it: https://www.youtube.com/watch?v=WteK95AppF4

Post on personal Facebook wall using ios sdk and tag multiple friends at once..?

I am trying to post a message on my wall and wanted to tag multiple users at a time in this post. I tried the various options on the FB post page but couldn't do it. May be I am not doing it right. Any help is appreciated and this is how I am doing it...
NSMutableDictionary* params = [NSMutableDictionary dictionaryWithObjectsAndKeys:
#"Test 2",#"message",
#"100004311843201,1039844409", #"to",
nil];
[self.appDelegate.facebook requestWithGraphPath:#"me/feed" andParams:params andHttpMethod:#"POST" andDelegate:self];
I have also tried message_tags but that doesn't seem to work as well.
You would need to use Open Graph to tag people with a message. The me/feed Graph API endpoint doesn't support this.
Mentions Tagging
https://developers.facebook.com/docs/technical-guides/opengraph/mention-tagging/
Action Tagging:
https://developers.facebook.com/docs/technical-guides/opengraph/publish-action/
You can take a look at the Scrumptious sample app that comes included with the latest Facebook SDK for iOS to see how to do this.
To tag a friend in ur fb status ..you need "facebook id" of your friend by using FBFriendPickerViewController and "place id" using FBPlacePickerViewController. Following code will help you.
NSString *apiPath = nil;
apiPath = #"me/feed";
if(![self.selectedPlaceID isEqualToString:#""]) {
[params setObject:_selectedPlaceID forKey:#"place"];
}
NSString *tag = nil;
if(mSelectedFriends != nil){
for (NSDictionary *user in mSelectedFriends) {
tag = [[NSString alloc] initWithFormat:#"%#",[user objectForKey:#"id"] ];
[tags addObject:tag];
}
NSString *friendIdsSeparation=[tags componentsJoinedByString:#","];
NSString *friendIds = [[NSString alloc] initWithFormat:#"[%#]",friendIdsSeparation ];
[params setObject:friendIds forKey:#"tags"];
}
FBRequest *request = [[[FBRequest alloc] initWithSession:_fbSession graphPath:apiPath parameters:params HTTPMethod:#"POST"] autorelease];
[request startWithCompletionHandler:^(FBRequestConnection *connection, id result, NSError *error) {
[SVProgressHUD dismiss];
if (error) {
NSLog(#"Error ===== %#",error.description);
if (_delegate != nil) {
[_delegate facebookConnectFail:error requestType:FBRequestTypePostOnWall];
}else{
NSLog(#"Error ===== %#",error.description);
}
}else{
if (_delegate != nil) {
[_delegate faceboookConnectSuccess:self requestType:FBRequestTypePostOnWall];
}
}
If you followed the tutorial on how to setup Open Graph for iOS, you can do something like this if you use the friendsPickerController:
// Create an action
id<FBOpenGraphAction> action = (id<FBOpenGraphAction>)[FBGraphObject graphObject];
//Iterate over selected friends
if ([friendPickerController.selection count] > 0) {
NSMutableArray *temp = [NSMutableArray new];
for (id<FBGraphUser> user in self.friendPickerController.selection) {
NSLog(#"Friend selected: %#", user.name);
[temp addObject:[NSString stringWithFormat:#"%#", user.id]];
}
[action setTags:temp];
}
Basically, you can set an array of friend's ids on the "tags" property on an action

Publish Feed with SSO in Facebook (IOS)

I am working on Facebook integration and trying for Single Sign-On with publish feed functionality.
I am using latest FacebookSDK. I have Facebook's Hackbook example code but, i am new to all this so it is being difficult to understand completely all this things.
While searching on SSO i got some code, It is working fine. Here is the code i am using (At the end of this page there is a source code attached)
FBUtils.h and FBUtils.m class
ViewController.m
- (IBAction)publishFeed:(id)sender {
//For SSO
[[FBUtils sharedFBUtils] initializeWithAppID:#"3804765878798776"];
NSArray *permision = [NSArray arrayWithObjects:#"read_stream",#"publish_stream", nil];
[[FBUtils sharedFBUtils] LoginWithPermisions:permision];
[FBUtils sharedFBUtils].delegate = self;
FBSBJSON *jsonWriter = [FBSBJSON new];
/// for publishfeed
NSArray* actionLinks = [NSArray arrayWithObjects:[NSDictionary dictionaryWithObjectsAndKeys:
#"Get Started",#"name",#"https://itunes.apple.com?ls=1&mt=8",#"link", nil], nil];
NSString *actionLinksStr = [jsonWriter stringWithObject:actionLinks];
// Dialog parameters
NSMutableDictionary *params = [NSMutableDictionary dictionaryWithObjectsAndKeys:
#"I have lot of fun preparing.", #"name",
#" exam", #"caption",
#" ", #"description",
#"https://itunes.apple.com", #"link",
#"http://mypng", #"picture",
actionLinksStr, #"actions",
nil];
AppDelegate *delegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
[[delegate facebook] dialog:#"feed"
andParams:params
andDelegate:self];
When i tap Facebook button in my app it is redirect me to Facebook and then retuning back to my app. Now , what i want is to fire publishFeed event right after returning back to the app and it should ask direct for post or cancel options to the user. But it is asking for login again like this.
Can any one help me in this or please suggest me the right way.
Your Suggestions would be a great help.
In your method, you're not checking if the app has permissions to publish post and if the user logged in before. So, every time you call this method, the app wants you to login. I think that is the problem.
If I'm right, you need to add permission and login control in your method like this. This is my sample code from another project, you can get the logic behind it.
- (IBAction)facebookShare:(id)sender
{
AppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
// You check the active session here.
if (FBSession.activeSession.isOpen)
{
// You check the permissions here.
if ([FBSession.activeSession.permissions
indexOfObject:#"publish_actions"] == NSNotFound) {
// No permissions found in session, ask for it
[FBSession.activeSession
reauthorizeWithPublishPermissions:
[NSArray arrayWithObject:#"publish_actions"]
defaultAudience:FBSessionDefaultAudienceFriends
completionHandler:^(FBSession *session, NSError *error) {
if (!error) {
// If permissions granted, publish the story
[self postFacebook];
[[[UIAlertView alloc] initWithTitle:#"Result"
message:#"Posted in your wall."
delegate:self
cancelButtonTitle:#"OK"
otherButtonTitles:nil]
show];
}
}];
} else {
// If permissions present, publish the story
[self postFacebook]; // ------> This is your post method.
[[[UIAlertView alloc] initWithTitle:#"Sonuç"
message:#"Duvarında paylaşıldı."
delegate:self
cancelButtonTitle:#"Tamam"
otherButtonTitles:nil]
show];
}
}
else
{
// If there is no session, ask for it.
[appDelegate openSessionWithAllowLoginUI:YES];
}
// NSLog(#"Post complete.");
}

How can i hide the url of image when i post image on the Facebook by fbconnect?

I am using this code for post the image on the facebook but it also shows image url on the facebook. I want to hide this url:
this is my code for posting an image to facebook
#pragma mark Facebook
-(void)facebook
{
AppDelegate *delegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
NSMutableDictionary *params = [NSMutableDictionary dictionaryWithObjectsAndKeys:
#"I'm using Shape App", #"message", #"I'm using Shape App", #"caption",
#"http://www.imageurlhost.com/images/3kft472rvl2qtnzx11_Sample.png", #"picture",
#"I'm using Shape App", #"title",nil];
[[delegate facebook] dialog:#"feed" andParams:params andDelegate:self];
}
please check the image below
What you are doing is posting a URL, so it shows up as that: A link you shared. It does not matter that this link does point to an image and not a website.
Your options to change that are,
a) post a link to an Open Graph page instead, that contains the image – then you can set a title and description that will show up for that shared link, or
b) post it as a real photo, https://developers.facebook.com/docs/reference/api/user/#photos
i have resolved by latest ios6 facebook integration:
for this you have to import Social.framework after this put this code in method where you want to call Facebook.but it will work only in iOS 6 and later
NSData *imageData=[NSData dataWithContentsOfURL:[NSURL urlWithString:#"http://www.imageurlhost.com/images/3kft472rvl2qtnzx11_Sample.png"]];
if([SLComposeViewController isAvailableForServiceType:SLServiceTypeFacebook])
{
mySLComposerSheet = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeFacebook];
[mySLComposerSheet setInitialText:[NSString stringWithFormat:#"I am using Shape App"]];
[mySLComposerSheet addImage:[UIImage imageWithData:imageData]];
[self presentViewController:mySLComposerSheet animated:YES completion:nil];
}
else
{
UIAlertView *alertView=[[UIAlertView alloc]initWithTitle:#"No Facebook Account" message:#"There are no Facebook accounts configured.You can add or create a Facebook account in Settings" delegate:nil cancelButtonTitle:#"Ok" otherButtonTitles:nil, nil];
[alertView show];
}
[mySLComposerSheet setCompletionHandler:^(SLComposeViewControllerResult result)
{
// NSLog(#"dfsdf");
switch (result) {
case SLComposeViewControllerResultCancelled:
break;
case SLComposeViewControllerResultDone:
break;
default:
break;
}
}];

iphone FBconnect - publish stream for multiple friend's

currently i m working on iphone application(facebook connect),
Is it possible to post the messages to MULTIPLE friends walls? Currently i am able to send message on single friend wall using "PUBLISH STREAM".
SO Using publish stream is it possible to send message on multiple friend's wall at a time ??
There is no such functionality I guess. But you can do it.
Check the example:
These are Fb delegate methods add to the class.
- (void)request:(FBRequest *)request didReceiveResponse:(NSURLResponse *)response {
NSLog(#"received response");
};
Getting friends UIDs: after logged in get the friends details and store the friends UIds in an array 'uids'
- (void)request:(FBRequest *)request didLoad:(id)result {
if([result isKindOfClass:[NSDictionary class]]) {
NSLog(#"dictionary");
result=[result objectForKey:#"data"];
if ([result isKindOfClass:[NSArray class]]) {
for(int i=0;i<[result count];i++){
NSDictionary *result2=[result objectAtIndex:i];
NSString *result1=[result2 objectForKey:#"id"];
NSLog(#"uid:%#",result1);
[uids addObject:result1];
}
}
}
}
- (void)request:(FBRequest *)request didLoadRawResponse:(NSData *)data
{
NSString *dataresponse=[[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];
NSLog(#"data is :%#",dataresponse);
}
Posting To All FB Friends:
Itterate the post till [uids count];
- (void)conncetToFriends:(id)sender {
static int ij=0;
NSMutableDictionary* params1 = [NSMutableDictionary dictionaryWithObjectsAndKeys:appId, #"api_key", #"Happy Holi", #"message", #"http://www.holifestival.org/holi-festival.html", #"link", #"http://www.onthegotours.com/blog/wp-content/uploads/2010/08/Holi-Festival.png", #"picture", #"Wanna Kno abt HOLI.. Check this...", #"name", #"Wish u 'n' Ur Family, a Colourful day...", #"description", nil];
NSLog(#"uid count:%i",[uids count]);
for(int i=0;i<[uids count];i++) {
NSString *path=[[NSString alloc]initWithFormat:#"%#/feed",[uids objectAtIndex:i]];
NSLog(#"i value:%i",ij);
//[facebook dialog:#"me/feed" andParams:params1 andDelegate:self];
[facebook requestWithGraphPath:path andParams:params1 andHttpMethod:#"POST" andDelegate:self];
ij++;
}
}