Facebook SDK Singleton在xcode自动登录之前发布? [英] Facebook SDK Singleton in xcode auto login before post?

查看:92
本文介绍了Facebook SDK Singleton在xcode自动登录之前发布?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经按照Facebook教程的指示,在APPDelegate中启动了Facebook SDK,但是我尝试使用单例方法。我发现的每一个教程似乎都是针对旧版本的SDK,但是我已经通过这种方式成功管理了一个 http://indiedevstories.com/2011/08/30/a-facebook-reusable-class/

I have had the Facebook SDK working in the APPDelegate as instructed by the Facebook tutorial, however I am trying to put in into a singleton method. Every tutorial i've found seems to be for an older version of the SDK, but I have managed success with this one http://indiedevstories.com/2011/08/30/a-facebook-reusable-class/

我有2个问题,第一个发布在这里这是第二个:

I'm having 2 problems, the first is posted here this is the second:

我想要一个按钮发布到Facebook,但如果用户没有登录,那么他们需要先登录然后发布(而不必按单独的登录按钮第一)。
我可以登录好,我可以发布罚款,但我不能一起做。
如果没有登录,邮政编码显示登录屏幕,但登录后不会显示在屏幕上,您必须再次按下。
如果您尝试登录但已经登录,那么没有任何反应。
所以在按钮上,我使用代码登录然后发布,因为登录只是跳过,如果已经登录。
我遇到的问题是邮政代码正在运行后立即登录代码,所以在用户有机会登录之前。这导致2个弹出窗口被打开(1个登录和1个帖子,显示登录尚未登录)。

I want a button to post to Facebook, but if the user isn't logged in then they need to login first then post (without having to press a separate login button first). I can log in fine, and I can post fine, however I can't do both together. If not logged in, the post code shows the login screen, but doesn't goto the post screen after logging in. you have to press post again. If you try to login but are already logged in, then nothing happens. So on the button I use code to login then post, as the login is just skipped if already logged in. The problem i'm having, is the post code is being run instantly after the login code, so before the user has had a chance to login. This results in 2 popups being opened (1 login and 1 post which displays login as not logged in yet).

如何让我的代码等待用户登录后再转到代码中的下一行?

How can I get my code to wait for the user to login before moving onto the next line in the code to post?

FacebookHelper.h

FacebookHelper.h

@interface FacebookHelper : NSObject <FBSessionDelegate, FBRequestDelegate, FBDialogDelegate, FBLoginDialogDelegate> {
    Facebook *_facebook;
    NSArray *_permissions;
}

@property (nonatomic,strong) Facebook *facebook;

+(FacebookHelper *) sharedInstance;
+(void)fbDidLogin;

#pragma mark - Public Methods
-(BOOL) isFBSessionValid;
-(void) login;
-(void) logout;
-(void) postToWallWithDialogNewHighscore:(int)highscore;

@end

FacebookHelper.m

FacebookHelper.m

@implementation FacebookHelper
@synthesize facebook;

#pragma mark -
#pragma mark Singleton Variables
static FacebookHelper *singletonDelegate = nil;

#pragma mark -
#pragma mark Singleton Methods

+(FacebookHelper *)sharedInstance
{
    @synchronized(self) {
        if (singletonDelegate == nil) {
            singletonDelegate = [[self alloc] init]; // Assignment not done here
        }
    }
    return singletonDelegate;
}

-(id)init
{
    self = [super init];
    if (self) {
        _facebook = [[Facebook alloc] initWithAppId:kAppId andDelegate:self];
        // Restore previous session
        NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
        if ([defaults objectForKey:@"FBAccessTokenKey"] && [defaults objectForKey:@"FBExpirationDateKey"]) {
        _facebook.accessToken = [defaults objectForKey:@"FBAccessTokenKey"];
        _facebook.expirationDate = [defaults objectForKey:@"FBExpirationDateKey"];
        }
        //
    }
    return self;
}

+(id)allocWithZone:(NSZone *)zone
{
    @synchronized(self) {
        if (singletonDelegate == nil) {
            singletonDelegate = [super allocWithZone:zone];
            // assignment and return on first allocation
            return singletonDelegate;
        }
    }
    //on subsequent allocation attemps, return nil
    return nil;
}

-(id)copyWithZone:(NSZone *)zone
{
    return self;
}

#pragma mark - Facebook Delegate Methods

-(void)fbDidLogin
{
    NSLog(@"fbDidLogin");
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    [defaults setObject:[_facebook accessToken] forKey:@"FBAccessTokenKey"];
    [defaults setObject:[_facebook expirationDate] forKey:@"FBExpirationDateKey"];
    [defaults synchronize];
}

-(void)fbDidLogout
{
    NSLog(@"fbDidLogout");
    // Remove saved authorisation information if it exists
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    if ([defaults objectForKey:@"FBAccessTokenKey"]) {
        [defaults removeObjectForKey:@"FBAccessTokenKey"];
        [defaults removeObjectForKey:@"FBExpirationDateKey"];
        [defaults synchronize];
    }
}

#pragma mark - Public Methods

-(NSMutableDictionary *) buildPostParamsWithHighScore:(int)highscore
{
    NSString *customMessage = [NSString stringWithFormat:kCustomMessage, highscore, kAppName];
    NSString *postName = kAppName;
    NSString *serverLink = [NSString stringWithFormat:kServerLink];
    NSString *imageSrc = kImageScr;

    //Final params build
    NSMutableDictionary *params = [NSMutableDictionary dictionaryWithObjectsAndKeys:
                                   //@"message", @"message",
                                   imageSrc, @"picture",
                                   serverLink, @"link",
                                   postName, @"name",
                                   @" ", @"caption",
                                   customMessage, @"description",
                                   nil];
    return params;
}

-(BOOL) isFBSessionValid
{
    // Check if there is a valid session
    //_facebook = [[Facebook alloc] initWithAppId:kAppId andDelegate:self];
    _facebook.accessToken = [[NSUserDefaults standardUserDefaults] objectForKey:@"FBAccessTokenKey"];
    _facebook.expirationDate = [[NSUserDefaults standardUserDefaults] objectForKey:@"FBExpirationDateKey"];
    NSLog(@"accessToken=%@ expirationDaate=%@",_facebook.accessToken,_facebook.expirationDate);
    if (![_facebook isSessionValid]) {
        NSLog(@"FacebookHelper isFBSessionValid = NO");
        return NO;
    } else {
        NSLog(@"FacebookHelper isFBSessionValid = YES");
        return YES;
    }

    return NO;
}

-(void) login
{
    NSLog(@"FacebookHelper login");
    _permissions = [NSArray arrayWithObjects:@"publish_stream", nil]; //@"read_stream", @"offline_access"
    [_facebook authorize:_permissions];
}

-(void) logout
{
    [_facebook logout];
}

-(void) postToWallWithDialogNewHighscore:(int)highscore
{
    NSMutableDictionary *params = [self buildPostParamsWithHighScore:highscore];

    NSLog(@"Post Feed");
    [_facebook dialog:@"feed" andParams:params andDelegate:self];
}

@end

按钮操作:

- (IBAction)facebookTest:(id)sender {
    [[FacebookHelper sharedInstance] login];
    [[FacebookHelper sharedInstance] postToWallWithDialogNewHighscore:123];
}


推荐答案

我已更新单身人士类向Facebook Wall发布分数。查看新版本: http:// indiedevstories.com/2012/04/11/facebookscorer-post-highscores-to-users-facebook-wall/

I have updated the singleton class to post scores to Facebook Wall. Check out the new version: http://indiedevstories.com/2012/04/11/facebookscorer-post-highscores-to-users-facebook-wall/

这个新版本正确处理内部当需要授权和登录时的状态。

This new version handles correctly the internal state when authorization and login is required.

HTH

这篇关于Facebook SDK Singleton在xcode自动登录之前发布?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆