使用FBConnect将分数发布到FB

时间:2011-05-23 00:01:39

标签: facebook post

大家好 只是想知道如何修改我的代码以将分数发布到Facebook

目前所有正在使用以下发布到Facebook,只是没有得分

#import "GameOverViewController.h"
#import "SoundEffects.h"
#import "FBConnect.h"


@implementation GameOverViewController

@synthesize scoreLabel, highScore;
@synthesize session = _session;
@synthesize postScoreButton = _postScoreButton;
@synthesize logoutButton = _logoutButton;
@synthesize loginDialog = _loginDialog;
@synthesize facebookName = _facebookName;
@synthesize posting = _posting;

- (void)viewDidLoad {
    [super viewDidLoad];

    NSString *gameOverPath = [[NSBundle mainBundle] pathForResource:@"gameover" ofType:@"png"];
    UIImage *gameOver = [[UIImage alloc] initWithContentsOfFile: gameOverPath];
    UIImageView *gameOverViewTemp = [[UIImageView alloc] initWithImage:gameOver];
    [self.view addSubview:gameOverViewTemp];


    gameOverText = [SpriteHelpers setupAnimatedSprite:self.view numFrames:3 withFilePrefix:@"gameovertext" withDuration:0.4 ofType:@"png" withValue:0];
    gameOverText.center = CGPointMake(160, 90);

    scoreLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 200, 320, 70)];
    scoreLabel.text = [NSString stringWithFormat:@"%d points", highScore];
    [self.view addSubview:scoreLabel];
    scoreLabel.textColor = [UIColor whiteColor];
    scoreLabel.backgroundColor = [UIColor clearColor];
    scoreLabel.font = [UIFont boldSystemFontOfSize:42];
    scoreLabel.textAlignment = UITextAlignmentCenter;

    [gameOverViewTemp release];
    [gameOver release];

    // Set these values from your application page on http://www.facebook.com/developers
    // Keep in mind that this method is not as secure as using the sessionForApplication:getSessionProxy:delegate method!
    // These values are from a dummy facebook app I made called MyGrades - feel free to play around!
    static NSString* kApiKey = @"2af22b07c9730d3d502a7a401b9e48d7";
    static NSString* kApiSecret = @"738116a372130f659a761078de08b3d4";
    _session = [[FBSession sessionForApplication:kApiKey secret:kApiSecret delegate:self] retain];

    // Load a previous session from disk if available.  Note this will call session:didLogin if a valid session exists.
    [_session resume];

        [super viewDidLoad];


}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
    [self.view removeFromSuperview];
    [self release];
}

- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event {
}

- (IBAction)postScoreTapped:(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];    
    }
    // 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];
}


#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;
        [_logoutButton setTitle:[NSString stringWithFormat:@"Facebook: Logout as %@", name] forState:UIControlStateNormal];
        if (_posting) {
            [self postToWall];
            _posting = NO;
        }
    }
}

#pragma mark Post to Wall Helper

- (void)postToWall {

    FBStreamDialog* dialog = [[[FBStreamDialog alloc] init] autorelease];
    dialog.userMessagePrompt = @"Enter your message:";
    dialog.attachment = [NSString stringWithFormat:@"{\"name\":\"%@ just played SpaceRide on the iPhone!\",\"href\":\"http://www.spaceride.me/\",\"caption\":\"%@ must be really skillful!\",\"description\":\"\",\"media\":[{\"type\":\"image\",\"src\":\"http://www.spaceride.me/fbicon.png\",\"href\":\"http://www.spaceride.me/\"}]}",
                         _facebookName, _facebookName];
    dialog.actionLinks = @"[{\"text\":\"Get SpaceRide!\",\"href\":\"http://www.spaceride.me/\"}]";
    [dialog show];

}





- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];

    // Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload { 
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}


- (void)dealloc {
    [scoreLabel release];

    [super dealloc];
}


@end

1 个答案:

答案 0 :(得分:1)

更改:

dialog.attachment = [NSString stringWithFormat:@"{\"name\":\"%@ just played SpaceRide on the iPhone!\",\"href\":\"http://www.spaceride.me/\",\"caption\":\"%@ must be really skillful!\",\"description\":\"\",\"media\":[{\"type\":\"image\",\"src\":\"http://www.spaceride.me/fbicon.png\",\"href\":\"http://www.spaceride.me/\"}]}",
                         _facebookName, _facebookName];

类似于:

dialog.attachment = [NSString stringWithFormat:@"{\"name\":\"%@ just got %d playing SpaceRide on the iPhone!\",\"href\":\"http://www.spaceride.me/\",\"caption\":\"%@ must be really skillful!\",\"description\":\"\",\"media\":[{\"type\":\"image\",\"src\":\"http://www.spaceride.me/fbicon.png\",\"href\":\"http://www.spaceride.me/\"}]}",
                         _facebookName, score, _facebookName];

将“得分”更改为整数得分变量的名称。