[英]Getting score from facebook using ios graph api stalls
我從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();
}
我剛遇到這個問題:
當用戶尚未向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];
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.