2

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;
         }*/
    }
}
ICoder
  • 1,347
  • 2
  • 13
  • 23

3 Answers3

1

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

      facebook = [[Facebook alloc] initWithAppId:@"YOUR_APP_ID" andDelegate:self];
      NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
if ([defaults objectForKey:@"FBAccessTokenKey"] 
    && [defaults objectForKey:@"FBExpirationDateKey"]) {
    facebook.accessToken = [defaults objectForKey:@"FBAccessTokenKey"];
    facebook.expirationDate = [defaults objectForKey:@"FBExpirationDateKey"];

}

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:-

        FBStreamDialog* dialog = [[[FBStreamDialog alloc] init] autorelease];
        dialog.userMessagePrompt = @"Enter your message:";
       dialog.attachment = [NSString stringWithFormat:@"I Love iOS"];
        [dialog show];

here is the reference link.

http://www.raywenderlich.com/77/how-to-post-on-facebook-with-your-iphone-app

Community
  • 1
  • 1
Ajeet Pratap Maurya
  • 4,244
  • 3
  • 28
  • 46
0

follow this tutorial: https://developers.facebook.com/docs/mobile/ios/build/ IF you'll do this as they say, your FB connection will be global for your application. using appDelegate you will can post, read and do whatever you whant from any view controller in your app

SentineL
  • 4,682
  • 5
  • 19
  • 38
0

You can use graph api for this here is the tutorial:-https://github.com/reallylongaddress/iPhone-Facebook-Graph-API

Leena
  • 2,678
  • 1
  • 30
  • 43