Instance Method

selectChallengeablePlayers(_:withCompletionHandler:)

Finds the subset of players that can earn an achievement.

Declaration

func selectChallengeablePlayers(_ players: [GKPlayer], withCompletionHandler completionHandler: (([GKPlayer]?, Error?) -> Void)? = nil)

Parameters

players

An array of GKPlayer objects containing a list of players. The list of players is used to find those players that are eligible to earn the achievement.

completionHandler

A block to be called when the download is completed.

The block receives the following parameters:

challengeablePlayers

An array of GKPlayer objects representing the players in the original array that are able to complete the challenge. If an error occurred, this parameter may be non-nil, in which case the array holds whatever achievement information Game Kit was able to fetch.

error

If an error occurred, this object describes the error. If the operation completed successfully, this value is nil.

Discussion

When this method is called, it creates a new background task to handle the request. The method then returns control to your game. Later, when the task is complete, Game Kit calls your completion handler. The completion handler is always called on the main thread. Listing 1 shows how to find the players eligible to be challenged to complete an achievement.

Listing 1

Determining the list of players who can complete an achievement challenge

- (void) challengePlayersToCompleteAchievement: (GKAchievement*) achievement
{
    [achievement selectChallengeablePlayers:[GKLocalPlayer localPlayer].friends withCompletionHandler:^(NSArray *challengeablePlayers, NSError *error) {
        if (challengeablePlayers)
        {
            //send a challenge to each of the eligible players.
        }
    }];
}

See Also

Issuing Achievement Challenges

func challengeComposeController(withMessage: String?, players: [GKPlayer], completionHandler: GKChallengeComposeCompletionBlock?) -> UIViewController

Provides a challenge compose view controller with a preformatted message for the achievement and pre-selected GKPlayer objects.