Game Center Tutorial for iOS: How To Make A Simple Multiplayer Game: Part 1/2

The first part of a Game Center tutorial series that shows you how to create a simple multiplayer iPhone game. By Ray Wenderlich.

Leave a rating/review
Save for later
Share
You are currently viewing page 3 of 3 of this article. Click here to view the first page.

Landscape Orientation and GKMatchmakerViewController

You might have noticed that by default, the GKMatchmakerViewController appears in portrait orientation. Obviously, this is quite annoying since this Cocos2D game is in landscape!

Luckily, you can put in a patch for this with Objective-C categories by forcing the GKMatchmakerViewController to accept landscape-only orientations.

To do this, Go to File\New\New File, choose iOS\Cocoa Touch\Objective-C class, and click Next. Enter NSObject for Subclass of, click Next, name the new class GKMatchmakerViewController-LandscapeOnly.m, and click Finish.

Replace the contents of GKMatchmakerViewController-LandscapeOnly.h with the following:

#import <Foundation/Foundation.h>
#import <GameKit/GameKit.h>

@interface GKMatchmakerViewController(LandscapeOnly)
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation;
@end

Then replace the contents of GKMatchmakerViewController-LandscapeOnly.m with the following:

#import "GKMatchmakerViewController-LandscapeOnly.h"

@implementation GKMatchmakerViewController (LandscapeOnly)

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { 
    return ( UIInterfaceOrientationIsLandscape( interfaceOrientation ) );
}

@end

And that’s it! Compile and run your app and the view controller should show up in landscape mode right away:

GKMatchmakerViewController in Landscape Mode

Where To Go From Here?

Here is a sample project with all of the code we’ve developed so far in this Game Center tutorial.

In the second part of the tutorial series, we’ll cover how to send data back and forth between each device in the game, and wrap up the game into an exciting cat vs. kid race!

In the meantime, if you have any questions, comments, or suggestions for future tutorials, please join the forum discussion below! And don’t forget to vote for what tutorial you’d like to see next in the sidebar! :]