iOS Developer Library

Developer

GameKit Framework Reference GKFriendRequestComposeViewController Class Reference

Options
Deployment Target:

On This Page
Language:

GKFriendRequestComposeViewController

Inherits From


Import Statement


Swift

import GameKit

Objective-C

@import GameKit;

Availability


Available in iOS 4.2 and later

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

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. On OS X, 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 1Displaying 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.

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

    Declaration

    Swift

    class func maxNumberOfRecipients() -> Int

    Objective-C

    + (NSUInteger)maxNumberOfRecipients

    Return Value

    The maximum number of recipients.

    Discussion

    If you add more recipients than the value returned from this method, Game Kit throws an exception.

    Import Statement

    Objective-C

    @import GameKit;

    Swift

    import GameKit

    Availability

    Available in iOS 4.2 and later

  • The view controller’s delegate

    Declaration

    Swift

    unowned(unsafe) var composeViewDelegate: GKFriendRequestComposeViewControllerDelegate!

    Objective-C

    @property(nonatomic, assign) id< GKFriendRequestComposeViewControllerDelegate > composeViewDelegate

    Discussion

    Before displaying the friend request, your game must set a delegate.

    Import Statement

    Objective-C

    @import GameKit;

    Swift

    import GameKit

    Availability

    Available in iOS 4.2 and later

  • Adds recipients based on their email addresses..

    Declaration

    Swift

    func addRecipientsWithEmailAddresses(_ emailAddresses: [AnyObject]!)

    Objective-C

    - (void)addRecipientsWithEmailAddresses:(NSArray *)emailAddresses

    Parameters

    emailAddresses

    An array with one or more NSString objects, each containing an email address.

    Discussion

    If you do not add at least once recipient, the recipients field is selected when the view controller is presented so that the player can type a list of recipients. Adding more players than defined by the maxNumberOfRecipients property causes an exception to be thrown.

    Import Statement

    Objective-C

    @import GameKit;

    Swift

    import GameKit

    Availability

    Available in iOS 4.2 and later

  • Adds recipients based on their Game Center player identifiers.

    Declaration

    Swift

    func addRecipientPlayers(_ players: [AnyObject]!)

    Objective-C

    - (void)addRecipientPlayers:(NSArray *)players

    Parameters

    players

    An array with one or more GKPlayer objects, each containing an player identifier.

    Discussion

    If you do not add at least once recipient, the recipients field is selected when the view controller is presented so that the player can type a list of recipients. Adding more players than defined by the maxNumberOfRecipients property causes an exception to be thrown.

    Import Statement

    Objective-C

    @import GameKit;

    Swift

    import GameKit

    Availability

    Available in iOS 8.0 and later

  • Adds recipients based on their Game Center player identifiers.

    Declaration

    Swift

    func addRecipientsWithPlayerIDs(_ playerIDs: [AnyObject]!)

    Objective-C

    - (void)addRecipientsWithPlayerIDs:(NSArray *)playerIDs

    Parameters

    playerIDs

    An array with one or more NSString objects, each containing an player identifier.

    Discussion

    If you do not add at least once recipient, the recipients field is selected when the view controller is presented so that the player can type a list of recipients.

    Import Statement

    Objective-C

    @import GameKit;

    Swift

    import GameKit

    Availability

    Available in iOS 4.2 and later

    Deprecated in iOS 8.0

  • Sets the text message included in the friend invitation.

    Declaration

    Swift

    func setMessage(_ message: String!)

    Objective-C

    - (void)setMessage:(NSString *)message

    Parameters

    message

    The text message.

    Discussion

    If you do not set a text message, Game Kit sets a default text message.

    Import Statement

    Objective-C

    @import GameKit;

    Swift

    import GameKit

    Availability

    Available in iOS 4.2 and later