Supporting Multiplayer Games and Matchmaking

Problem

You want to allow multiple players to join the same game and play your game together.

Solution

Incorporate matchmaking in your app, as explained in the Discussion section.

Discussion

One of the most important functionalities provided to iOS developers in Game Center is matchmaking. Matchmaking allows two or more players to play the same game in multiplayer mode at the same time. You can either use Apple’s servers for multiplayer games or host your own server. In this book, we will only cover matchmaking using Apple’s server, for the sake of simplicity.

Note

Sending matchmaking invites is not possible from the iOS Simulator. Since matchmaking is between two or more players, you need at least two real iOS devices to test it, even on Sandbox servers. For the examples in this section, I am testing the code on an iPhone 4 and an iPad 2.

There are two essential programming activities in a multiplayer game using Game Center:

  1. Creating, waiting for, and accepting new match requests.

  2. Transmitting game data during play.

The first part is perhaps the more difficult one to understand. To make it easier for you, let me paint a rather general picture of how things work in multiplayer mode in Game Center. When your app runs on an iOS device, it must:

  1. Authenticate the local player (see Authenticating the Local Player in Game Center).

  2. Tell Game Center which block of code has to be executed, if an invitation is received from Game Center. This block of code (a ...

Get Writing Game Center Apps in iOS now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.