使用 FBConnect 将分数发布到 FB

发布于 2024-11-09 01:31:27 字数 5481 浏览 0 评论 0原文

大家好 只是想知道我如何修改我的代码以将分数发布到 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

Hi all
Just wondering how I modify my code to post a score to Facebook

Currently all working with the following posting to facebook, just no score

#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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

在风中等你 2024-11-16 01:31:27

更改:

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];

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

Change:

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];

To something like:

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];

Change 'score' to the name of your integer score variable.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文