简体   繁体   English

使用iOS图形API摊位从Facebook获取分数

[英]Getting score from facebook using ios graph api stalls

I started with the friendsmash Facebook example and getting a score does not work if no score has previously been set. 我从Friendsmash Facebook示例开始,如果之前未设置分数,则无法获得分数。 The code is below. 代码如下。 If this is called it simply stalls at FBRequestConnection and does not produce an error or any result, no further code is executed in the block. 如果调用此方法,它只会停在FBRequestConnection上,并且不会产生错误或任何结果,因此该块中不会再执行任何代码。 Obviously this is unacceptable. 显然,这是不可接受的。

If I post the score without first checking its value, it posts fine, and then I can check its value correctly. 如果我在未首先检查其值的情况下发布分数,则它会很好地发布,然后可以正确检查其值。 I assume the call is not supposed to 'hang' if no score is set at all, or should I just not check the score and always post it? 我认为如果根本没有设置分数,则通话不应该挂起,还是应该不检查分数并始终发布?

void FB_Controller::FB_SendScore(const int nScore)
{
    // Make sure we have write permissions
    FB_RequestWritePermissions();

    NSMutableDictionary* params =   [NSMutableDictionary dictionaryWithObjectsAndKeys:
                                        [NSString stringWithFormat:@"%d", nScore], @"score",
                                        nil];

    NSLog(@"Fetching current score");

    // Get the score, and only send the updated score if it's highter
    [FBRequestConnection startWithGraphPath:[NSString stringWithFormat:@"%llu/scores", m_uPlayerFBID] parameters:params HTTPMethod:@"GET" completionHandler:^(FBRequestConnection *connection, id result, NSError *error) {

        if(error)
        {
            NSLog(@"ERROR getting score %@", error);
        }
        else if (result && !error) {

            int nCurrentScore = [[[[result objectForKey:@"data"] objectAtIndex:0] objectForKey:@"score"] intValue];

            NSLog(@"Current score is %d", nCurrentScore);

            if (nScore > nCurrentScore) {

                NSLog(@"Posting new score of %d", nScore);

                [FBRequestConnection startWithGraphPath:[NSString stringWithFormat:@"%llu/scores", m_uPlayerFBID] parameters:params HTTPMethod:@"POST" completionHandler:^(FBRequestConnection *connection, id result, NSError *error) {

                    if(error)
                    {
                        NSLog(@"ERROR posting score %@", error);
                    }
                    else
                        NSLog(@"Score posted");
                }];
            }
            else {
                NSLog(@"Existing score is higher - not posting new score");
            }
        }

        }];

    // Send our custom OG
    //FB_SendOG();
}

I just had this problem which is: 我刚遇到这个问题:

The dictionary returned from [result objectForKey:@"data"] is empty when a user has not submitted any score for your app to Facebook. 当用户尚未向Facebook提交您的应用程序的任何分数时,从[result objectForKey:@“ data”]返回的词典为空。 This causes objectAtIndex to throw an exception. 这将导致objectAtIndex引发异常。 Anyway, I just replaced: 无论如何,我只是替换了:

int nCurrentScore = [[[[result objectForKey:@"data"] objectAtIndex:0] objectForKey:@"score"] intValue];

With: 附:

int nCurrentScore = 0;
if ([[result objectForKey:@"data"] count]) {
    nCurrentScore = [[[[result objectForKey:@"data"] objectAtIndex:0] objectForKey:@"score"] intValue];
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM