Instance Method

sendExchange(to:data:localizableMessageKey:arguments:timeout:completionHandler:)

Sends an exchange request to one or more participants.

Declaration

func sendExchange(to participants: [GKTurnBasedParticipant], data: Data, localizableMessageKey key: String, arguments: [String], timeout: TimeInterval, completionHandler: ((GKTurnBasedExchange?, Error?) -> Void)? = nil)

Parameters

participants

An array of GKTurnBasedParticipant objects containing the participants who are to receive the reminder.

data

The data to be exchanged between players.

key

The location of the alert message string in the Localizable.strings file for the current localization.

arguments

An array of NSString objects to be substituted using the format string.

timeout

The length of time the next player has to complete their turn.

completionHandler

A block to be called after the exchange request has been sent.

The block receives the following parameters:

exchange

The updated exchange to be passed.

error

If an error occurred, this error object describes the error. If the operation was completed successfully, the 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.

Allows an exchange request to be sent to one or more participants. Each recipient receives a push notification that uses the supplied localizable message. An error is returned if any of the participants are inactive. Listing 1 showws a basic exchange requeste.

Listing 1

Sending an exchange request to another player

- (void) exchangeRequest (GKTurnBasedParticipant receiver)
{
     NSArray *recipients = @[receiver];
     [self sendExchangeToParticipants:recipients data:data localizableMessageKey:key arguments:arguments timeout:timeout completionHandler:^(GKTurnBasedExchange *exchange, NSError *error) {
          // refresh your match data
     }];
}

See Also

Transferring Information with Exchanges

var GKExchangeTimeoutDefault: TimeInterval

The exchange will timeout after one day if no reply is received.

var GKExchangeTimeoutNone: TimeInterval

The exchange will not timeout.

var activeExchanges: [GKTurnBasedExchange]?

Returns the exchanges that are active for the local player.

var completedExchanges: [GKTurnBasedExchange]?

The exchanges that have been completed and need to be merged by the local participant.

var exchangeDataMaximumSize: Int

The maximum amount of data allowed for an exchange.

var exchangeMaxInitiatedExchangesPerPlayer: Int

Limits the number of exchanges the player can have initiated at once.

var exchanges: [GKTurnBasedExchange]?

The current exchanges that are in progress for the match.