using xcode with the FB API to post on peoples walls - iphone

I am using the FB API and intergrating FB so that i caqn post on the users walls when they accept it,
i have this code sao far from the fb site its working all ok but the users are allowed to enter there own message on the post box.
- (void)fbDidLogin {
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:[facebook accessToken] forKey:#"FBAccessTokenKey"];
[defaults setObject:[facebook expirationDate] forKey:#"FBExpirationDateKey"];
[defaults synchronize];
NSArray *permissions = [[NSArray alloc] initWithObjects:
#"user_likes",
#"read_stream",
nil];
[facebook authorize:permissions];
[permissions release];
[facebook dialog:#"feed" andDelegate:self];
}
I was wondering if i could have a preset message to appear in the box somthing like the code below.
[NSString stringWithFormat:#" My New High Score i%",highscore]
and locked so that they can not edit it.

I found out by using this it posts everything i need and allows the user to add there comment plus they cant edit there high score which is what i was looking for.
- (void)postwall
{
NSString *string = #"I reached a score of ";
string = [string stringByAppendingString:[NSString stringWithFormat:#"%d", highscore]];
string = [string stringByAppendingString:#" can you beat me?"];
NSMutableDictionary* params = [NSMutableDictionary dictionaryWithObjectsAndKeys:
//kAppId, #"app_id",
#"https://developers.facebook.com/docs/reference/dialogs/", #"link",
#"http://fbrell.com/f8.jpg", #"picture",
#"Watch Ma Balls", #"name",
#"How long can you keep out of the way of ma balls for", #"caption",
string, #"description",
nil];
[facebook dialog:#"feed" andParams:params andDelegate:self];
}

Related

iOS Post to Facebook publish screen doesn't show after authentication

Alrighty. I have a button (below) where I take some info from an app and I post it to the users FB feed.
It almost never works the first time. Them, it will usually work pretty well after that. What happens is, when I press the button it tells me that I already gave the app permission to use FB (which I would like to only have to see the first time, not every time). Then when I press OK from there it will either go back to the app, or it will go to show me the page where I can actually post to my wall (which is what I want).
Im trying different things to get this to work more consistently. Is there something here that would be causing this behavior?
-(IBAction)fbButton:(id)sender {
if (![facebook isSessionValid]) {
[facebook authorize:permissions];
[permissions release];
}
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
if ([defaults objectForKey:#"FBAccessTokenKey"]
&& [defaults objectForKey:#"FBExpirationDateKey"]) {
facebook.accessToken = [defaults objectForKey:#"FBAccessTokenKey"];
facebook.expirationDate = [defaults objectForKey:#"FBExpirationDateKey"];
}
//Post to Wall
NSString *poopDollas = [[NSString alloc] initWithFormat:
#"sometext $%#!",dollas.text];
SBJSON *jsonWriter = [[SBJSON new] autorelease];
// The action links to be shown with the post in the feed
NSArray* actionLinks = [NSArray arrayWithObjects:[NSDictionary dictionaryWithObjectsAndKeys:
#"Get Started",#"name",#"http://www.facebook.com/pages/xx/204640386498567",#"link", nil], nil];
NSString *actionLinksStr = [jsonWriter stringWithObject:actionLinks];
// Dialog parameters
NSMutableDictionary *params = [NSMutableDictionary dictionaryWithObjectsAndKeys:
#"DM", #"name",
poopDollas, #"caption",
#"sometext", #"description",
#"http://www.facebook.com/pages/xx/204640386498567", #"link",
#"http://farm8.staticflickr.com/7185/6999841878_e66a8e00fc_t.jpg", #"picture",
actionLinksStr, #"actions",
nil];
FBDialog *delegate = (FBDialog *)[UIApplication sharedApplication].delegate;
[facebook dialog:#"feed" andParams:params andDelegate:(id <FBDialogDelegate>)delegate];
}
I took out the
[facebook authorize:permissions];
[permissions release];
In that first IF statement and my problems seem to be solved..

how to add a value to the fb post xcode

Im creating a game an want to be able to add there score so when they post it on fb it shows them there high score, (to add a bit more of a social aspect as)
here is the code i am suing but it dosnt seem to work and crashes the app,
- (void)fbDidLogin {
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:[facebook accessToken] forKey:#"FBAccessTokenKey"];
[defaults setObject:[facebook expirationDate] forKey:#"FBExpirationDateKey"];
[defaults synchronize];
[self postwall];
}
- (void)postwall
{
NSMutableDictionary* params = [NSMutableDictionary dictionaryWithObjectsAndKeys:
//kAppId, #"app_id",
#"https://developers.facebook.com/docs/reference/dialogs/", #"link",
#"http://fbrell.com/f8.jpg", #"picture",
#"Watch Ma Balls", #"name",
#"How long can you keep out of the way of ma balls for", #"caption",
#"I reached a score of i% can you beat me!.",highscore, #"description",
nil];
[facebook dialog:#"feed" andParams:params andDelegate:self];
}
the code works fine with out the
highscore
there
is there a way that it can be added to the string so it shows?
Try this (assuming highscore is an int):
NSString *string = #"I reached a score of ";
string = [string stringByAppendingString:[NSString stringWithFormat:#"%d", highscore]];
string = [string stringByAppendingString:#" can you beat me?"];
NSMutableDictionary* params = [NSMutableDictionary dictionaryWithObjectsAndKeys:
//kAppId, #"app_id",
#"https://developers.facebook.com/docs/reference/dialogs/", #"link",
#"http://fbrell.com/f8.jpg", #"picture",
#"Watch Ma Balls", #"name",
#"How long can you keep out of the way of ma balls for", #"caption",
string, #"description",
nil];

how to post to wall after logging in to FB

i will like to know how to post to my own wall with a string #"123456789" after logging in to FB with this
facebook = [[Facebook alloc] initWithAppId:kAppId andDelegate:self];
// Check and retrieve authorization information
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
if ([defaults objectForKey:#"FBAccessTokenKey"] && [defaults objectForKey:#"FBExpirationDateKey"]) {
facebook.accessToken = [defaults objectForKey:#"FBAccessTokenKey"];
facebook.expirationDate = [defaults objectForKey:#"FBExpirationDateKey"];
}
// Initialize user permissions
userPermissions = [[NSMutableDictionary alloc] initWithCapacity:1];
if (![facebook isSessionValid]) {
NSArray *permissions = [[NSArray alloc] initWithObjects:
#"publish_stream",
nil];
[facebook authorize:permissions];
//[permissions release];
}
use the following code to feed
NSMutableDictionary *params = [NSMutableDictionary dictionaryWithObjectsAndKeys:
[NSString stringWithFormat:#"YOUR MESSAGE"],
#"message", nil];
[facebook requestWithGraphPath:#"me/feed" andParams:params andHttpMethod: #"POST" andDelegate:self];
You can always create your own request and post it to facebook, like the following request:
you specify what you want from: type, link, picture, source, message, name, caption, description, and you must provide an access_token, which if you're using facebook api, you can get easily.
https://graph.facebook.com/me/feed?type=&link=&picture=&source=&message=&name=jwt&caption=inmyworld&description=&access_token=

iPhone facebook sdk shows Valid session even when I logged out through Facebook app

After many hours of trying codes, searching online, I have to ask for help. I have a share button in a UITableViewCell. On its touch action I open facebook login dialog and then if logged in I show feed dialog. It working fine but even if I logout through Facebook app the sessionIsValid shows YES. Here's the Code...
if(indexPath.row==0)
{
AppId=#"xxx";
AppDelegate *appDelegate=(AppDelegate*)[[UIApplication sharedApplication]delegate];
appDelegate.scheduleViewController=self;
self.facebook = [[Facebook alloc] initWithAppId:AppId andDelegate:self];
// [self.facebook requestWithGraphPath:#"me" andDelegate:self];
if ([defaults objectForKey:#"FBAccessTokenKey"]
&& [defaults objectForKey:#"FBExpirationDateKey"]) {
self.facebook.accessToken = [defaults objectForKey:#"FBAccessTokenKey"];
self.facebook.expirationDate = [defaults objectForKey:#"FBExpirationDateKey"];
}
if (![self.facebook isSessionValid]) {
[self.facebook authorize:permissions];
}
else
[self doStuffAfterLogin:[self.facebook accessToken]];
}
- (void)fbDidLogin {
[defaults setObject:[self.facebook accessToken] forKey:#"FBAccessTokenKey"];
[defaults setObject:[self.facebook expirationDate] forKey:#"FBExpirationDateKey"];
[defaults synchronize];
[self doStuffAfterLogin:[self.facebook accessToken]];
}
- (void)request:(FBRequest *)request didFailWithError:(NSError *)error {
NSLog(#"%#", [error localizedDescription]);
NSLog(#"Err details: %#", [error description]);
UIAlertView *alertView=[[UIAlertView alloc]initWithTitle:#"Error!" message:#"Error!" delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil, nil];
[alertView show];
[defaults removeObjectForKey:#"FBAccessTokenKey"];
[defaults removeObjectForKey:#"FBExpirationDateKey"];
[self.facebook authorize:permissions];
}
-(void)doStuffAfterLogin:(NSString*)accessToken;
{
NSMutableString *imgUrl=[[NSMutableString alloc]init];
if([[defaults objectForKey:#"imageUrl"] isEqualToString:#""])
imgUrl= #"http://artist.eventseekr.com/images/no-photo.png";
else
imgUrl=[defaults objectForKey:#"imageUrl"];
NSMutableDictionary* params = [NSMutableDictionary dictionaryWithObjectsAndKeys:
AppId, #"app_id",
imgUrl, #"picture",
[NSString stringWithFormat:#"Watch %# in action at the %# event",[defaults objectForKey:#"name"],[defaults objectForKey:#"eventName"]], #"caption",
#"Sample Festival App", #"description",
nil];
[self.facebook dialog:#"feed" andParams:params andDelegate:self];
}
That's correct. Each app has it's own sandboxed area. So you have authorised your app and your app has stored a token that it can use for validating itself. It knows nothing about any other app. So if you logout in the facebook app, it doesn't know.

why fbdidlogin didn't call?

- (id)init {
if (self == [super init]) {
facebook = [[Facebook alloc] initWithAppId:kAppId];
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
if ([defaults objectForKey:#"FBAccessTokenKey"]
&& [defaults objectForKey:#"FBExpirationDateKey"]) {
facebook.accessToken = [defaults objectForKey:#"FBAccessTokenKey"];
facebook.expirationDate = [defaults objectForKey:#"FBExpirationDateKey"];
}
if (![facebook isSessionValid]) {
permissions = [[NSArray arrayWithObjects:
#"read_stream", #"user_birthday",
#"publish_stream", nil] retain];
[facebook authorize:permissions delegate:self];
}
[self login];
}
return self;
}
- (void)login {
if (![_session isConnected]) {
[self postToWall];
}
if (![facebook isSessionValid]) {
[facebook authorize:permissions delegate:self];
}
}
- (void)fbdidLogin {
[[NSUserDefaults standardUserDefaults] setObject:self.facebook.accessToken forKey:#"FBAccessToken"];
[[NSUserDefaults standardUserDefaults] setObject:self.facebook.expirationDate forKey:#"FBExpirationDate"];
// User has logged in to Facebook, now get their userId from Facebook
[facebook requestWithGraphPath:#"me" andDelegate:self];
}
-(void)postToWall
{
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:
#"Your Happiness!", #"name",
#"asda", #"caption",
#"asdf", #"description",
nil];
NSString *attachmentStr = [jsonWriter stringWithObject:attachment];
NSMutableDictionary *params = [NSMutableDictionary dictionaryWithObjectsAndKeys:
#"Share on Facebook", #"user_message_prompt",
actionLinksStr, #"action_links",
attachmentStr, #"attachment",
nil];
[facebook dialog:#"stream.publish" andParams:params andDelegate:self];
}
- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url {
return [facebook handleOpenURL:url];
}
This is my methods for calling facebook dialog boxes.But every time when i click to score for publishing;The permission box came first.I need to give permission after that the publish dialog box comes.I want to give permission ones and i want it to save it;after saving it i don't wanna see the permission box ever again.How can i do it?What is wrong with my code?
edit:
My access token and expiration date is null i guess thats because of this.
A few things to check, you call the method login handling method "fbdidLogin" instead of fbDidLogin. This is case sensitive.
Make sure in your view controller header file that you added FBSessionDelegate as one of the protocols you support. That could be a reason fbDidLogin is not called. You could always add an NSLog to verify that the fbDidLogin method is reached.
You need to implement Facebook's Single-Sign-On feature.
http://developers.facebook.com/docs/guides/mobile/#ios
This link has the step-by-step guide for implementing SSO, if you follow it carefully your App will request for permission once, then it will never request permission again (until the user remove your App)
Note that, you will need to go to www.facebook.com and use the "Developer Tools" to create a new Facebook App, you will be the admin of the app, and you will have your AppID, you will need this too.