user1833903
user1833903

Reputation: 197

iOS 6 : Game center authentication

I have a problem with the integration of game center in my app' which use iOS 6 SDK. In fact I use the sample code from Apple, but it looks like incomplete :

I have tried this code :

-(void) authenticateLocalPlayer {

GKLocalPlayer* localPlayer =
[GKLocalPlayer localPlayer];

localPlayer.authenticateHandler =
^(UIViewController *loginVC,
  NSError *error) {

    [self setLastError:error];

    if ([GKLocalPlayer localPlayer].authenticated)
    {
        // authentication successful
        [self enableGameCenterForPlayer:[GKLocalPlayer localPlayer]];
    }
    else if (loginVC)
    {
        // player not logged in yet, present the vc
        [self pauseGame];
        [self presentLoginVC:loginVC];
    }
    else
    {
        // authentication failed, provide graceful fallback
        [self disableGameCenter];
    }
    };

}

But the problem is that enableGameCenterForPlayer, pauseGame, presentLoginVC, disableGameCenter are not implemented methods, and it returns :

Instance method '-enableGameCenterForPlayer:' not found (return type defaults to 'id')

How can I fix this problem ?

Thanks

Upvotes: 0

Views: 915

Answers (1)

mevdev
mevdev

Reputation: 797

I use the method [self presentLoginVC:VC] to pass my UITabViewController or UINavigationController the viewController because the block below is not on the main thread.

localPlayer.authenticateHandler = ^(UIViewController *loginVC, NSError *error) {

When you are in a block you should be sure not to change UI elements because you really don't know when it will complete or where you will be in your app. There are probably many ways to do this, but this is my solution.

Below is my UITabBarController 'category' .m file (additions of methods for a class without subclassing) I create the method presentLoginVC and just have it call 'showGameCenterViewController' through my UITabBarController:

    #import "UITabBarController+GameKitAdditions.h"

    @implementation UITabBarController (GameKitAdditions)

    -(void) showGameCenterViewController: (UIViewController *)VC {
        [self presentViewController:VC animated:NO completion:nil];
    }

    -(void)dismissGameCenterViewController:(UIViewController *)VC {
        [self dismissViewControllerAnimated:YES completion:nil];
    }
    @end

As to the other functions:

    -(void) enableGameCenterForPlayer:(GKLocalPlayer *) localPlayer;
    -(void) disableGameCenter;
    -(void) pauseGame;

They could be as simple as just setting a BOOL called enableGameCenter to YES or NO. To get around errors you can just add these prototypes to your .h file and then write the functions just to output something to NSLog() or something.

Upvotes: 1

Related Questions