1

I used to use this code in order to login users account in game center :

[[GKLocalPlayer localPlayer] authenticateWithCompletionHandler:^(NSError *error)
     {
         if (error != nil)
         {
             NSLog(@"LOGIN");
         } else {
             NSLog(@"CANT LOGIN");
         }
     }];

this code works fine with iOS 5.x but crashes in iOS 6 , I would be grateful if you help to fix it.

thanks

Moonlight
  • 37
  • 1
  • 5
  • This method is deprecated in iOS 6; set the `authenticateHandler` property instead. (This probably won't fix your crash though. For that you need to show more code.) – titaniumdecoy Sep 26 '12 at 21:55
  • this is my code for login in GameCenter!(up) , can you give me a sample code? – Moonlight Sep 26 '12 at 21:59

3 Answers3

3

You need to check for iOS5 or 6. iOS6 changed the authentication function

iOS6

   localPlayer.authenticateHandler = ^(UIViewController *viewController, NSError *error)
    {
        //Something
    }

iOS5

    [localPlayer authenticateWithCompletionHandler:^(NSError *error)
    {
        //Something
    }];
0x78
  • 136
  • 2
  • 5
1

This works in iOS 6:

        GKLocalPlayer *localPlayer = [GKLocalPlayer localPlayer];
    localPlayer.authenticateHandler = ^(UIViewController *viewController, NSError *error) {
        if (viewController != nil) {
            [self presentViewController:viewController animated:YES completion:nil];
        } else if (localPlayer.isAuthenticated) {
            // do post-authentication work
        } else {
            // do unauthenticated work, such as error message, etc
        }
    };
NathanChristie
  • 2,374
  • 21
  • 20
0

Here you have the Game Center Programming Guide

This is how you must authenticate users in iOS6:

- (void) authenticateLocalPlayer
{
    GKLocalPlayer *localPlayer = [GKLocalPlayer localPlayer];
    localPlayer.authenticateHandler = ^(UIViewController *viewController, NSError *error){
         if (viewController != nil)
         {
             [self showAuthenticationDialogWhenReasonable: viewController
         }
         else if (localPlayer.isAuthenticated)
         {
             [self authenticatedPlayer: localPlayer];
         }
         else
         {
             [self disableGameCenter];
         }
     }];
}

You should also checkout this question, because your app might be crashing due to autorotation issues and game center, not the authentication mechanism itself

Community
  • 1
  • 1
Lio
  • 4,225
  • 4
  • 33
  • 40
  • thanks a lot Lio , this code work at iOS 5 and iOS 6 together? my game work very good on iOS 5. i want my game work at iOS 5 and iOS 6 together! please help me.i have force majeure work. thx a lot. – Moonlight Sep 27 '12 at 19:32
  • see this error : http://1.3.3.img98.net/out.php/i474324_screen-shot-2012-09-27-at18-pm.png – Moonlight Sep 27 '12 at 19:38
  • 1
    Those errors are because the code I pasted is sample code, you actually have to implement those methods. And to answer your previous method, no, the code will only work in iOS6. I am using the old iOS5 code in iOS6 and it still works alright. You should track the crash down and find out, the reason. I recommend that you look into that question I linked in my answer. Good luck – Lio Sep 27 '12 at 19:46
  • This is copy / pasted from Apple's Game Center Programming guide: http://developer.apple.com/library/ios/#documentation/NetworkingInternet/Conceptual/GameKit_Guide/Users/Users.html#//apple_ref/doc/uid/TP40008304-CH8-SW17 , which itself contains an error. – NathanChristie Dec 06 '12 at 00:15