14: Game Center in Objective-C

Generating Data Matrix 2d barcode in Objective-C 14: Game Center

CHAPTER 14: Game Center
Data Matrix ECC200 Maker In Objective-C
Using Barcode maker for iPhone Control to generate, create Data Matrix 2d barcode image in iPhone applications.
www.OnBarcode.com
UCC - 12 Creation In Objective-C
Using Barcode maker for iPhone Control to generate, create UPC-A Supplement 5 image in iPhone applications.
www.OnBarcode.com
@property (nonatomic, readonly) NSError* lastError; +(GameKitHelper*) sharedGameKitHelper; // Player authentication, info -(void) authenticateLocalPlayer; -(void) getLocalPlayerFriends; -(void) getPlayerInfo:(NSArray*)players; @end
Code 39 Full ASCII Generation In Objective-C
Using Barcode printer for iPhone Control to generate, create Code 3/9 image in iPhone applications.
www.OnBarcode.com
Make Barcode In Objective-C
Using Barcode generator for iPhone Control to generate, create Barcode image in iPhone applications.
www.OnBarcode.com
For your convenience, the GameKitHelper class also stores the last error in its lastError property. This allows you to check if any error occurred, and if so, what kind of error, without actually receiving the Game Center messages directly. The GameKitHelper class is a singleton, which was described in 3, so I ll leave the singleton-specific code out of the discussion. The remaining properties and methods will be discussed shortly. For now, let s have a look how the TileMapLayer class is extended so that it can function as the delegate for GameKitHelper. The essential changes to the header file are importing GameKitHelper.h and specifying that TileMapLayer implements GameKitHelperProtocol:
Generate GTIN - 128 In Objective-C
Using Barcode generator for iPhone Control to generate, create UCC - 12 image in iPhone applications.
www.OnBarcode.com
Create QR Code JIS X 0510 In Objective-C
Using Barcode creation for iPhone Control to generate, create Quick Response Code image in iPhone applications.
www.OnBarcode.com
#import "GameKitHelper.h" @interface TileMapLayer : CCLayer <GameKitHelperProtocol> { ... }
DataMatrix Generator In Objective-C
Using Barcode creation for iPhone Control to generate, create ECC200 image in iPhone applications.
www.OnBarcode.com
UPC E Generation In Objective-C
Using Barcode generator for iPhone Control to generate, create UPC E image in iPhone applications.
www.OnBarcode.com
Then you can set the TileMapLayer class to be the delegate of the GameKitHelper class, in the init method:
DataMatrix Encoder In None
Using Barcode generation for Software Control to generate, create Data Matrix 2d barcode image in Software applications.
www.OnBarcode.com
Painting ECC200 In .NET
Using Barcode creation for Visual Studio .NET Control to generate, create DataMatrix image in .NET applications.
www.OnBarcode.com
GameKitHelper* gkHelper = [GameKitHelper sharedGameKitHelper]; gkHelper.delegate = self; [gkHelper authenticateLocalPlayer];
Generating Code-128 In None
Using Barcode drawer for Software Control to generate, create Code 128 Code Set B image in Software applications.
www.OnBarcode.com
Barcode Printer In None
Using Barcode creation for Font Control to generate, create Barcode image in Font applications.
www.OnBarcode.com
Note that you are responsible for setting the GameKitHelper delegate back to nil when appropriate for example, shortly before changing scenes. Because GameKitHelper retains the delegate, it will not be released from memory, even if it otherwise would (e.g., during a scene change). That would not only keep the delegate itself in memory but all of its member variables as well, including all of its children if it s a CCNode class.
Scan Barcode In C#
Using Barcode recognizer for .NET Control to read, scan read, scan image in Visual Studio .NET applications.
www.OnBarcode.com
Barcode Maker In None
Using Barcode drawer for Online Control to generate, create Barcode image in Online applications.
www.OnBarcode.com
Checking for Game Center Availability
EAN 13 Decoder In Java
Using Barcode recognizer for Java Control to read, scan read, scan image in Java applications.
www.OnBarcode.com
Paint Code 128 Code Set A In None
Using Barcode maker for Microsoft Word Control to generate, create Code 128B image in Office Word applications.
www.OnBarcode.com
The GameKitHelper class starts by checking for Game Center availability right in its init method (Listing 14 3). It only needs to do that once because the conditions never change while the app is running.
Making QR Code ISO/IEC18004 In None
Using Barcode creator for Online Control to generate, create QR Code image in Online applications.
www.OnBarcode.com
UCC - 12 Generator In None
Using Barcode generation for Word Control to generate, create GS1-128 image in Office Word applications.
www.OnBarcode.com
Listing 14 3. Testing for Game Center Availability // Test for Game Center availability Class gameKitLocalPlayerClass = NSClassFromString(@"GKLocalPlayer"); bool isLocalPlayerAvailable = (gameKitLocalPlayerClass != nil); // Test if device is running iOS 4.1 or higher NSString* reqSysVer = @"4.1";
Print UPC - 13 In None
Using Barcode generation for Font Control to generate, create EAN / UCC - 13 image in Font applications.
www.OnBarcode.com
Data Matrix 2d Barcode Generator In None
Using Barcode encoder for Online Control to generate, create DataMatrix image in Online applications.
www.OnBarcode.com
CHAPTER 14: Game Center
NSString* currSysVer = [[UIDevice currentDevice] systemVersion]; bool isOSVer41 = ([currSysVer compare:reqSysVer options:NSNumericSearch] != NSOrderedAscending); isGameCenterAvailable = (isLocalPlayerAvailable && isOSVer41);
The first test is simply to check if a specific Game Center class is available. In this case, the Objective-C runtime method NSClassFromString is used to get one of the Game Center classes by name. If this call returns nil, you can be certain that Game Center is unavailable. But it s not quite that simple. Because Game Center was already partially available in beta versions prior to iOS 4.1, you also need to check if the device is running at least iOS 4.1. This is done by comparing the reqSysVer string with the systemVersion string. Once both checks are made, the results are combined using the && (and) operator, so that both must be true for isGameCenterAvailable to become true. The isGameCenterAvailable variable is used to safeguard all calls to Game Center functionality within the GameKitHelper class. This avoids accidentally calling Game Center functionality when it is not available, which would crash the application. Note that this is how Apple recommends to check for Game Center availability. You should not try any other methods for example, determining the type of device your game is running on. While certain devices are excluded from using Game Center, this is already accounted for with the preceding check.
Authenticating the Local Player
The local player is a fundamental concept to Game Center programming. It refers to the player account that is signed into the device. This is important to know because only the local player can send scores to leaderboards and report achievement progress to the Game Center service. The very first thing a Game Center application needs to do is authenticate the local player. If that fails, you cannot use most of the Game Center services, and in fact Apple recommends not using any Game Center functionality unless there is an authenticated local player. In the GameKitHelper init method, the registerForLocalPlayerAuthChange method is called so that GameKitHelper receives events concerning authentication changes for the local player. This is the only Game Center notification that is sent through NSNotificationCenter. You register a selector to receive the message, as shown in Listing 14 4.
Listing 14 4. Registering for Local Player Authentication Changes -(void) registerForLocalPlayerAuthChange { if (isGameCenterAvailable == NO) return; NSNotificationCenter* nc = [NSNotificationCenter defaultCenter]; [nc addObserver:self selector:@selector(onLocalPlayerAuthenticationChanged)
Copyright © OnBarcode.com . All rights reserved.