7

I am working on game which is using Game Center and I get next warning;

... 'authenticateWithCompletionHandler:' is deprecated: first deprecated in iOS 6.0

Ok, I searched and found out that there is new code for authenticate Local User so I replaced

old code:

- (void)authenticateLocalUser {

    if (!gameCenterAvailable) return;

    NSLog(@"Authenticating local user...");
    if ([GKLocalPlayer localPlayer].authenticated == NO) {
        [[GKLocalPlayer localPlayer] authenticateWithCompletionHandler:nil];
    } else {
        NSLog(@"Already authenticated!");
    }
}

with the new one:

- (void)authenticateLocalUser {

    if (!gameCenterAvailable) return;

    NSLog(@"Authenticating local user...");

    if ([GKLocalPlayer localPlayer].authenticated == NO) {

        GKLocalPlayer *localPlayer = [GKLocalPlayer localPlayer];
        [localPlayer setAuthenticateHandler:(^(UIViewController* viewcontroller, NSError *error) {
        //[localPlayer authenticateWithCompletionHandler:^(NSError *error) { OLD CODE!
            if(localPlayer.isAuthenticated) {
                //do some stuff
            }else {
                // not logged in   
            }
        })]; 
    } else {
        NSLog(@"Already authenticated!");   
    }   
}

and everything is ok except one thing. If user is not logged in there is no Game Center login form. With old code it shows up Game Center login form if user is not logged in.

is there any extra code that I must put in or something else?

Extra info: - landscape mode - deployment target: 6.0

Darvas
  • 974
  • 3
  • 14
  • 27
iWizard
  • 6,816
  • 19
  • 67
  • 103

1 Answers1

10

Yes, you have to manually present the login form with iOS6, this gives you more control over when to present the screen. Give this a try

localPlayer.authenticateHandler = ^(UIViewController *viewController,NSError *error) {
if (localPlayer.authenticated) { 
//already authenticated
} else if(viewController) {
[self presentViewController:viewController];//present the login form
} else {
//problem with authentication,probably bc the user doesn't use Game Center
} 
};
Kaan Dedeoglu
  • 14,765
  • 5
  • 40
  • 41
  • I get this error: Warning: Attempt to present – iWizard Jan 21 '13 at 08:41
  • Extra info: - landscape mode - deployment target: 6.0 – iWizard Jan 21 '13 at 08:43
  • AppDelegate - didFinishLaunchingWithOptions. – iWizard Jan 21 '13 at 08:49
  • Have you link to any working example? I've searched and searched for working example with this new Game center method "authenticateHandler" but with no success. This would solve my problem. – iWizard Jan 21 '13 at 09:04
  • Yes, but it's from iOS 6 By Tutorials which is sold on Raywenderlich's site (highly recommended!), and its not really legal for me to share the book. You'll have to buy it. However, the code I provided is very similar to what they provide. [self presentViewController:self] fails in the AppDelegate since it's no view controller, you should make sure that a view controller makes the presentation. – Kaan Dedeoglu Jan 21 '13 at 09:26
  • I have buyed "iOS 5 By Tutorials Second Edition" and on top of that pdf stays. Fully updated for iOS 6. – iWizard Jan 21 '13 at 09:50
  • I've put this method to my ViewController and there app crashes up on line: [self presentViewController:viewController animated:NO completion:nil]; – iWizard Jan 21 '13 at 09:53
  • I would appreciate that :) – Kaan Dedeoglu Jan 21 '13 at 10:09
  • Tell me please one more thing if you know. I have implemented like is in book and it works, nbut only when I check in UI that portrait mode is supported. My game works only in landscape mode. How to solve that? Thank you – iWizard Jan 21 '13 at 10:25
  • 1
    I cooked up a category, try downloading this and adding it to your project https://dl.dropbox.com/u/22953018/GKLeaderboardProtocol.zip – Kaan Dedeoglu Jan 21 '13 at 10:34