Home » Swift » Game Center Challenges in iOS 10

Game Center Challenges in iOS 10

Posted by: admin November 30, 2017 Leave a comment

Questions:

I’ve implemented Game Center features like achievements and leaderboards, and now I’m working on the challenges. I was under the impression that I didn’t have to add any additional code – if I had achievements or leaderboards, players would be able to send challenges to their friends. But now, in iOS10, you no longer have the ability to add players as friends – the challenges are handled through iMessages. The problem is – I don’t see that feature anywhere in the GKViewController screen. If you select an achievement/leaaderboard score, you can tap on ‘Challenge Friends’, but it only suggests the players you already have in your friends list rather than in your contact list. Apple has also deprecated GKChallengesViewController, so I’m not sure where to look on how to do this.

Does anyone know how to add the iMessage Challenges feature to Game Center in iOS 10?

Update: I have seen that this feature lives within the GKMatchmakerViewController, but that seems to be for multiplayer type things. I’m still not sure how to use this to just send challenges.

Answers:

From the Apple Docs:

Issuing a challenge does not display a user interface to the player issuing the challenge; this is code you need to implement yourself.

There are also a few examples on how to issue challenges and how to find players you can invite, such as:

- (void) challengePlayersToCompleteAchievement: (GKAchievement*) achievement
{
    [achievement selectChallengeablePlayers:[GKLocalPlayer localPlayer].friends withCompletionHandler:^(NSArray *challengeablePlayerI, NSError *error) {
        if (challengeablePlayers)
        {
            [self presentChallengeWithPreselectedPlayers: challengeablePlayers];
        }
    }];
}

…or:

- (void) challengeLesserMortalsForScore: (int64_t) playerScore inLeaderboard: (NSString*) leaderboard
{
    GKLeaderboard *query = [[GKLeaderboard alloc] init];
    query.leaderboardIdentifier = leaderboard;
    query.playerScope = GKLeaderboardPlayerScopeFriendsOnly;
    query.range = NSMakeRange(1,100);
    [query loadScoresWithCompletionHandler:^(NSArray *scores, NSError *error) {
        NSPredicate *filter = [NSPredicate predicateWithFormat:@"value < %qi",playerScore];
        NSArray *lesserScores = [scores filteredArrayUsingPredicate:filter];
        [self presentChallengeWithPreselectedScores: lesserScores];
    }];
}

By the looks of it you still can only invite players that are already part of game center, i.e. no arbitrary “contacts” from the contact list (which makes sense), but this is only an assumption.