I am doing a reader application which can send text to Facebook. But I completed the login to Facebook in another page. I have a button called ‘textpostingbtn‘ and a textview which holds the text. What I need to do is, when the user taps the button it posts the text to Facebook without showing the login dialog. I have some validation on the button so the button is enabled only if the user is logged in. link that i used in login purpose.
EDIT:
- (IBAction)postGradesTapped:(id)sender {
//_posting = YES;
// If we're not logged in, log in first...
if (![_session isConnected]) {
self.loginDialog = nil;
_loginDialog = [[FBLoginDialog alloc] init];
[_loginDialog show];
_logoutButton.hidden = YES;
}
// If we have a session and a name, post to the wall!
//else if (_facebookName != nil) {
//[self postToWall];
//}
// Otherwise, we don't have a name yet, just wait for that to come through.
}
- (IBAction)logoutButtonTapped:(id)sender {
[_session logout];
[[NSUserDefaults standardUserDefaults] setBool:NO forKey:@"FB_logged"];
_btnFacebookmain.enabled =NO;
}
#pragma mark FBSessionDelegate methods
- (void)session:(FBSession*)session didLogin:(FBUID)uid {
[self getFacebookName];
}
- (void)session:(FBSession*)session willLogout:(FBUID)uid {
_logoutButton.hidden = YES;
_facebookName = nil;
}
#pragma mark Get Facebook Name Helper
- (void)getFacebookName {
NSString* fql = [NSString stringWithFormat:
@"select uid,name from user where uid == %lld", _session.uid];
NSDictionary* params = [NSDictionary dictionaryWithObject:fql forKey:@"query"];
[[FBRequest requestWithDelegate:self] call:@"facebook.fql.query" params:params];
}
#pragma mark FBRequestDelegate methods
- (void)request:(FBRequest*)request didLoad:(id)result {
if ([request.method isEqualToString:@"facebook.fql.query"]) {
NSArray* users = result;
NSDictionary* user = [users objectAtIndex:0];
NSString* name = [user objectForKey:@"name"];
self.facebookName = name;
_logoutButton.hidden = NO;
[[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"FB_logged"];
[_logoutButton setTitle:[NSString stringWithFormat:@"Facebook: Logout as %@", name] forState:UIControlStateNormal];
/*if (_posting) {
[self postToWall];
_posting = NO;
}*/
}
}
Well for this you have to log in to FB only one time.
Save the access token in your NSUserDefaults.
Then when ever you want to share on FB just access your NSUserDefaults and get the access token and use that token for sharing.
for storing the access token in NSUserDefaults
}
below are the couple of links that will guide you
https://developers.facebook.com/docs/mobile/ios/build/
http://coffeeshopped.com/2011/01/saving-sessions-with-the-facebook-ios-sdk
discussion on SO
Saving Facebook access_token in NSUserDefaults on iOS
How to get a Facebook access token on iOS
To posting code that will go to you button press will be this:-
here is the reference link.
http://www.raywenderlich.com/77/how-to-post-on-facebook-with-your-iphone-app