Type Method

loadPlayersForIdentifiers:withCompletionHandler:

Loads information from Game Center about a list of players.

Declaration

+ (void)loadPlayersForIdentifiers:(NSArray<NSString *> *)identifiers withCompletionHandler:(void (^)(NSArray<GKPlayer *> *players, NSError *error))completionHandler;

Parameters

identifiers

An array of NSString objects, each a unique identifier for a Game Center player.

completionHandler

A block to be called when the player data is retrieved from Game Center.

The block receives the following parameters:

players

An array of GKPlayer objects, one per identifier. If an error occurred, this may be non-nil. In that case, the array holds whatever data Game Kit was able to retrieve for the requested players.

error

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

Discussion

Listing 1 shows an example of how to load the GKPlayer objects associated with player identifiers.

Listing 1

Retrieving player details

- (void) loadPlayerData: (NSArray *) identifiers
{
    [GKPlayer loadPlayersForIdentifiers:identifiers withCompletionHandler:^(NSArray *players, NSError *error) {
        if (error != nil)
        {
            // Handle the error.
        }
        if (players != nil)
        {
            // Process the array of GKPlayer objects.
        }
     }];
}

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.

See Also