GameCenter Achievement相关问题

在接入Game Center SDK时,会加入成就系统设计。
如何配置iTunes成就参考
http://blog.csdn.net/shenjie12345678/article/details/45025403/

配置成功后,即可添加代码,基础代码上述文章中有提到,这里不再赘述。可能相关接口已经弃用,这里贴上最新接口的代码。

- (void) loadAchievements

{

    NSMutableDictionary *achievementDictionary = [[NSMutableDictionary alloc] init];

    [GKAchievement loadAchievementsWithCompletionHandler:^(NSArray *achievements,NSError *error)

     {
         if (error == nil) {

             NSArray *tempArray = [NSArray arrayWithArray:achievements];

             for (GKAchievement *tempAchievement in tempArray) {

                 [achievementDictionary setObject:tempAchievement forKey:tempAchievement.identifier];

                 NSLog(@"    completed:%d",tempAchievement.completed);

                 NSLog(@"    hidden:%d",tempAchievement.hidden);

                 NSLog(@"    lastReportedDate:%@",tempAchievement.lastReportedDate);

                 NSLog(@"    percentComplete:%f",tempAchievement.percentComplete);

                 NSLog(@"    identifier:%@",tempAchievement.identifier);

             }

         }else{
             NSLog(@"ERROR: %@",error);
         }

     }];
}

刚开始的时候,我进入误区,误以为上述函数可获得配置的所有成就列表,调试的时候获取列表内容一直为空。
后来才弄清楚,loadAchievementsWithCompletionHandler返回的是已经获得的成就列表,由于我没完成任何一个成就,所以返回的列表就为空。

然后我尝试将其中两个成就进度提交到完成状态。提交成就代码如下:

- (void) reportAchievementIdentifier: (NSString*) identifier percentComplete: (float) percent

{

    GKAchievement *achievement = [[[GKAchievement alloc] initWithIdentifier: identifier] autorelease];

    if (achievement)

    {

        achievement.percentComplete = percent;

        [achievement reportAchievementWithCompletionHandler:^(NSError *error)

         {

             if (error != nil)

             {

                 //The proper way for your application to handle network errors is retain

                 //the achievement object (possibly adding it to an array). Then, periodically

                 //attempt to report the progress until it is successfully reported.

                 //The GKAchievement class supports the NSCoding protocol to allow your

                 //application to archive an achie

                 NSLog(@"报告成就进度失败 ,错误信息为: \n %@",error);

             }else {

                 //对用户提示,已经完成XX%进度

                 NSLog(@"报告成就进度---->成功!");

                 NSLog(@"    completed:%d",achievement.completed);

                 NSLog(@"    hidden:%d",achievement.hidden);

                 NSLog(@"    lastReportedDate:%@",achievement.lastReportedDate);

                 NSLog(@"    percentComplete:%f",achievement.percentComplete);

                 NSLog(@"    identifier:%@",achievement.identifier);

             }

         }];

    }

}

这里需要注意的一点是,identifier这里应该用配置时用的ID,之前第一反应时用苹果分配的Achievement ID,结果提交进度虽然成功,但获取的成就列表依然为空。
用本地配置的ID提交两条成就任务后,可以看到loadAchievements会返回两条成就任务信息。进入iPhone的Game Center(手机上有一个Game Center的APP),点击游戏,可以看到已提交的那两条成就已经被点亮。(未完成的成就会显示问号,解锁的成就才会显示相应的成就头像)

想获取成就描述信息,参考一下接口:

- (NSArray*)retrieveAchievmentMetadata
{
    //读取成就的描述
    [GKAchievementDescription loadAchievementDescriptionsWithCompletionHandler:
     ^(NSArray *descriptions, NSError *error) {
         if (error != nil)
         {
             // process the errors
             NSLog(@"读取成就说明出错");
         }
         if (descriptions != nil)
         {
             // use the achievement descripti*****.
             for (GKAchievementDescription *achDescription in descriptions) {
                 NSLog(@"1..identifier..%@",achDescription.identifier);
                 NSLog(@"2..achievedDescription..%@",achDescription.achievedDescription);
                 NSLog(@"3..title..%@",achDescription.title);
                 NSLog(@"4..unachievedDescription..%@",achDescription.unachievedDescription);
                 NSLog(@"5............%@",achDescription.image);

                 //获取成就图片,如果成就未解锁,返回一个大文号
                 /*
                  [achDescription loadImageWithCompletionHandler:^(UIImage *image, NSError *error) {
                  if (error == nil)
                  {
                  // use the loaded image. The image property is also populated with the same image.
                  NSLog(@"成功取得成就的图片");
                  UIImage *aImage = image;
                  UIImageView *aView = [[UIImageView alloc] initWithImage:aImage];
                  aView.frame = CGRectMake(50, 50, 200, 200);
                  aView.backgroundColor = [UIColor clearColor];
                  [[[CCDirector sharedDirector] openGLView] addSubview:aView];
                  }else {
                  NSLog(@"获得成就图片失败");
                  }
                  }];
                  */
             }
         }
     }];
    return nil;
}

若对本文有疑问,还请指出一起讨论。

你可能感兴趣的:(IOS)