-2

I have an iOS Application which will load a particular view controller for authentication. However no matter what, the view controller will not load. Below is the code for my method which handles this (This is for a Game Center authentication popup):

-(void)authenticateLocalUser {

GKLocalPlayer *localPlayer = [GKLocalPlayer localPlayer];
localPlayer.authenticateHandler = ^(UIViewController *viewController, NSError *error) {

    if (viewController != nil) {
        NSLog(@"AUTH VIEW CONTROLLER");
        [[[UIApplication sharedApplication] keyWindow].rootViewController presentViewController:viewController animated:YES completion:nil];
    }

    else if (localPlayer.isAuthenticated) {
        NSLog(@"User is authenticated");
    }

    else {
        NSLog(@"Game Center Auth error.");
    }
};
}

As you can see, I have followed the official Apple developer documentation. However the output of this method is always:

Game Center Auth Error (Last NSLog).

Why doesn't the view controller ever load? Because I need it to load so that the user can login to Game Center if they haven't done so already.

Thanks for your time, Dan.

Supertecnoboff
  • 6,406
  • 11
  • 57
  • 98
  • 1
    This is your handler block, but you don't show where you actually attempt to load the view controller – Paulw11 May 24 '14 at 10:49
  • The line "[[[UIApplication sharedApplication] keyWindow].rootViewController presentViewController:viewController animated:YES completion:nil];" Opens the view controller. – Supertecnoboff May 24 '14 at 10:50
  • As for the method itself, it is called on viewdidload in my view controller. – Supertecnoboff May 24 '14 at 10:50
  • That presents the view controller, but it doesn't load a view controller – Paulw11 May 24 '14 at 10:50
  • So how do I load the view controller? – Supertecnoboff May 24 '14 at 10:50
  • @IndrajeetSenger I have added that, but the problem is that the view controller seems to be nil as the very last else statement keeps on begin called – Supertecnoboff May 24 '14 at 10:54
  • did u try to log NSError object? – Indrajeet May 24 '14 at 10:58
  • Ok as weird as this is now, but the code above is not working perfectly. I just logged in/out of the game centre app on the iOS simulator, deleted the app and the build and run again. Now it works...... – Supertecnoboff May 24 '14 at 11:07
  • There are A LOT of possible reason..this code is insufficient..is the block called? where is this pear of code? why do you use the rootViewController to present the new viewController, actually where do you are when you call this block? are you in the main thread?? – Matteo Gobbi May 24 '14 at 11:08
  • @Paulw11 how is possible presenting viewController and not loading it? – E-Riddie May 24 '14 at 11:09
  • It turns out that the Game kit player singleton is...interesting...Setting the authentication handler property triggers the creation of an authentication view if necessary. – Paulw11 May 24 '14 at 11:13
  • It's possible this will help you ... http://stackoverflow.com/a/23403979/294884 – Fattie May 24 '14 at 12:01

1 Answers1

0

It turned out that the issue in the end was to do with the Game Center app itself. I just logged in/out of that and it all worked. I have also now seen this same issue replicated in other iOS apps too which use Game Center.

Supertecnoboff
  • 6,406
  • 11
  • 57
  • 98