在支持多个iOS版本时,我应该使用哪个Game Center API?

时间:2015-05-04 03:56:19

标签: ios objective-c game-center

在以下链接中,已经提到了两种向iOS上的游戏中心报告分数的方法:

https://developer.apple.com/library/ios/documentation/NetworkingInternet/Conceptual/GameKit_Guide/LeaderBoards/LeaderBoards.html#//apple_ref/doc/uid/TP40008304-CH6-SW9

第一个版本似乎适用于iOS 7 +的版本:

  

清单4-2向Game Center(iOS 7)报告分数

- (void) reportScore: (int64_t) score forLeaderboardID: (NSString*) identifier
{
    GKScore *scoreReporter = [[GKScore alloc] initWithLeaderboardIdentifier: identifier];
    scoreReporter.value = score;
    scoreReporter.context = 0;

    NSArray *scores = @[scoreReporter];
    [GKScore reportScores:scores withCompletionHandler:^(NSError *error) {
    //Do something interesting here.
    }];
}

第二个适用于iOS 6:

  

清单4-3向Game Center(iOS 6)报告分数

- (void) reportScore: (int64_t) score forLeaderboardID: (NSString*) category
{
    GKScore *scoreReporter = [[GKScore alloc] initWithCategory:category];
    scoreReporter.value = score;
    scoreReporter.context = 0;

    [scoreReporter reportScoreWithCompletionHandler:^(NSError *error) {
// Do something interesting here.
    }];
}

我的部署目标是iOS 6.0。那么我应该使用哪种API? iOS 7版本?或者我应该根据设备上的iOS版本调用API,如下面的代码所示:

- (void) reportScore: (int64_t) score forLeaderboardID: (NSString*) identifier
{

    if (floor(NSFoundationVersionNumber) > NSFoundationVersionNumber_iOS_6_1) {
        // iOS 7
        GKScore *scoreReporter = [[GKScore alloc] initWithLeaderboardIdentifier: identifier];
        scoreReporter.value = score;
        scoreReporter.context = 0;

        NSArray *scores = @[scoreReporter];
        [GKScore reportScores:scores withCompletionHandler:^(NSError *error) {
            //Do something interesting here.
        }];
    }else{
        //iOS 6
        GKScore *scoreReporter = [[GKScore alloc] initWithCategory:identifier];
        scoreReporter.value = score;
        scoreReporter.context = 0;

        [scoreReporter reportScoreWithCompletionHandler:^(NSError *error) {
            // Do something interesting here.
        }];
    }
}

编辑:iOS 6的版本在iOS的更高版本中已弃用。 编辑:此外,如果我支持两个版本取决于iOS的版本,那么标识符和类别之间有什么区别?它们都是一样的吗?

0 个答案:

没有答案