使用ios graph api stall从facebook获得分数

时间:2013-11-11 16:31:43

标签: ios facebook facebook-graph-api

我从friendsmash Facebook示例开始,如果之前未设置分数,则得分无效。代码如下。如果调用它只是在FBRequestConnection停止并且不产生错误或任何结果,则不会在块中执行进一步的代码。显然这是不可接受的。

如果我在没有先检查其值的情况下发布分数,它会发布正常,然后我可以正确检查其值。我假设如果根本没有设置分数,那么呼叫不应该“挂起”,或者我应该不检查分数并始终发布它?

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();
}

1 个答案:

答案 0 :(得分:0)

我刚遇到这个问题:

当用户未向您的Facebook提交任何应用分数时,从[result objectForKey:@“data”]返回的字典为空。这会导致objectAtIndex抛出异常。无论如何,我刚刚更换了:

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

使用:

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