如何在不显示对话框的情况下发布到Facebook?

时间:2011-11-22 05:59:37

标签: iphone ios facebook

我正在做一个可以向Facebook发送文本的阅读器应用程序。但我在另一个页面完成了登录Facebook。我有一个名为“textpostingbtn”的按钮和一个保存文本的textview。我需要做的是,当用户点击按钮时,它会将文本发布到Facebook而不显示登录对话框。我对按钮进行了一些验证,因此仅当用户登录时才会启用该按钮。link that i used in login purpose

编辑:

- (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;
         }*/
    }
}

3 个答案:

答案 0 :(得分:1)

为此你必须只登录一次FB。 将访问令牌保存在NSUserDefaults中。 然后,当您想要在FB上共享时,只需访问您的NSUserDefaults并获取访问令牌并使用该令牌进行共享。

用于在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"];

}

以下是指导您的几个链接

https://developers.facebook.com/docs/mobile/ios/build/

http://coffeeshopped.com/2011/01/saving-sessions-with-the-facebook-ios-sdk

讨论SO

Saving Facebook access_token in NSUserDefaults on iOS

How to get a Facebook access token on iOS

要发布将按下按钮的代码,请按下: -

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

这是参考链接。

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

答案 1 :(得分:0)

按照本教程:https://developers.facebook.com/docs/mobile/ios/build/ 如果您按照他们的说法执行此操作,您的FB连接将是您的应用程序的全局连接。使用appDelegate,您可以在应用程序的任何视图控制器中发布,阅读和执行任何操作

答案 2 :(得分:0)

你可以使用graph api这里是教程:-https://github.com/reallylongaddress/iPhone-Facebook-Graph-API