Your game uses the GKFriendRequestComposeViewController class to present a screen that allows the local player to send friend requests to other players.


iOS, Mac Catalyst
class GKFriendRequestComposeViewController : UINavigationController
class GKFriendRequestComposeViewController : NSViewController


To show a friend request, initialize a new GKFriendRequestComposeViewController object and set the delegate. Optionally, you can customize the request by adding a text message or a list of recipients. Then, present the new view controller and wait for the delegate to be called. Once the delegate is called, dismiss the view controller.

On iOS, you present and dismiss the view controller from another view controller in your game, using the methods provided by the UIViewController class. In macOS, you use the GKDialogController class to present and dismiss the view controller. Listing 1 shows one way your view controller can allow a player to send a request to other players. For this method, an array of GKPlayer objects is passed in as a parameter. The method instantiates a GKFriendRequestComposeViewController object, sets its delegate, and adds the list of players intended to receive the invitation. The view controller then presents the friend request and returns.

Listing 1

Displaying a friend request

- (void) inviteFriends: (NSArray*) players
    GKFriendRequestComposeViewController *friendRequestViewController = [[GKFriendRequestComposeViewController alloc] init];
    friendRequestViewController.composeViewDelegate = self;
    if (players.count)
        [friendRequestViewController addRecipientPlayers: players];
    [self presentViewController: friendRequestViewController animated: YES completion:nil];
    [friendRequestViewController release];

Subclassing Notes

The GKFriendRequestComposeViewController class is not intended to be subclassed.


Determining the Maximum Number of Recipients

class func maxNumberOfRecipients() -> Int

Returns the maximum number of recipients permitted in a single request.

Adding Recipients

func addRecipients(withEmailAddresses: [String])

Adds recipients based on their email addresses..

func addRecipientPlayers([GKPlayer])

Adds recipients based on their Game Center player identifiers.

func addRecipients(withPlayerIDs: [String])

Adds recipients based on their Game Center player identifiers.

Setting an Invitation Message

func setMessage(String?)

Sets the text message included in the friend invitation.

See Also

Friend Request View Controllers

protocol GKFriendRequestComposeViewControllerDelegate

The GKFriendRequestComposeViewControllerDelegate protocol is implemented by delegates of the GKFriendRequestComposeViewController class. The delegate is called when the player dismisses the friend request.