3

Followed the Ray Wenderlich tutorial on implementing the Game Center with iOS 7

http://www.raywenderlich.com/3276/game-center-tutorial-for-ios-how-to-make-a-simple-multiplayer-game-part-12

However I am not getting a prompt to sign in nor is the welcome back banner appearing, tried numerous tutorials, can't seem to get it to work with iOS 7.

Here's my code

GCHelper.h

#import <Foundation/Foundation.h>
#import <GameKit/GameKit.h>

@interface GCHelper : NSObject {
    BOOL gameCenterAvailable;
    BOOL userAuthenticated;
}

@property (assign, readonly) BOOL gameCenterAvailable;

+ (GCHelper *)sharedInstance;
- (void)authenticateLocalUser;

@end

GCHelper.m

#import "GCHelper.h"

@implementation GCHelper

@synthesize gameCenterAvailable;

#pragma mark Initialization

static GCHelper *sharedHelper = nil;
+ (GCHelper *) sharedInstance {
    if (!sharedHelper) {
        sharedHelper = [[GCHelper alloc] init];
    }
    return sharedHelper;
}

- (BOOL)isGameCenterAvailable {
    // check for presence of GKLocalPlayer API
    Class gcClass = (NSClassFromString(@"GKLocalPlayer"));

    // check if the device is running iOS 4.1 or later
    NSString *reqSysVer = @"4.1";
    NSString *currSysVer = [[UIDevice currentDevice] systemVersion];
    BOOL osVersionSupported = ([currSysVer compare:reqSysVer
                                           options:NSNumericSearch] != NSOrderedAscending);

    return (gcClass && osVersionSupported);
}

- (id)init {
    if ((self = [super init])) {
        gameCenterAvailable = [self isGameCenterAvailable];
        if (gameCenterAvailable) {
            NSNotificationCenter *nc =
            [NSNotificationCenter defaultCenter];
            [nc addObserver:self
                   selector:@selector(authenticationChanged)
                       name:GKPlayerAuthenticationDidChangeNotificationName
                     object:nil];
        }
    }
    return self;
}

- (void)authenticationChanged {

    if ([GKLocalPlayer localPlayer].isAuthenticated && !userAuthenticated) {
        NSLog(@"Authentication changed: player authenticated.");
        userAuthenticated = TRUE;
    } else if (![GKLocalPlayer localPlayer].isAuthenticated && userAuthenticated) {
        NSLog(@"Authentication changed: player not authenticated");
        userAuthenticated = FALSE;
    }

}

#pragma mark User functions

- (void)authenticateLocalUser {

    if (!gameCenterAvailable) return;

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

@end

and in my application did finish launching method I have

[[GCHelper sharedInstance] authenticateLocalUser];

Is it because I authenticateWithCompletionHandler is deprecated?

Sami
  • 1,374
  • 1
  • 16
  • 43

3 Answers3

5

here is the code I'm using to show game centre login using iOS7

gamecentercontrol.h

#import <Foundation/Foundation.h>
#import <GameKit/GameKit.h>

@interface gamecentercontrol : NSObject {

    BOOL gameCentreAvailable;
    BOOL userAuthenticated;

}

@property (assign, readonly) BOOL gameCentreAvailable;

+ (gamecentercontrol *)sharedInstance;

-(void)authenticateLocalUser;


@end

gamecentercontrol.m

#import "gamecentercontrol.h"

@interface gamecentercontrol () <GKGameCenterControllerDelegate> {

    BOOL _gameCenterFeaturesEnabled;

}
@end


@implementation gamecentercontrol

@synthesize gameCentreAvailable;

static gamecentercontrol *sharedControl = nil;
+ (gamecentercontrol *) sharedInstance {
    if (!sharedControl) {
        sharedControl = [[gamecentercontrol alloc]init];
    }
    return sharedControl;
}

-(BOOL)isGameCentreAvailable {
    // check for presence of GKLocalPlayer API
    Class gcClass = (NSClassFromString(@"GKLocalPlayer"));

    //check if the device is running iOS 4.1 or later
    NSString *reqSysVer = @"4.1";
    NSString *currSysVer = [[UIDevice currentDevice] systemVersion];
    BOOL osVersionSupported = ([currSysVer compare:reqSysVer options:NSNumericSearch] != NSOrderedAscending);

    return (gcClass && osVersionSupported);
}

- (id)init {
    if ((self = [super init])) {

        gameCentreAvailable = [self isGameCentreAvailable];
        if (gameCentreAvailable) {
            NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
            [nc addObserver:self selector:@selector(authenticationChanged) name:GKPlayerAuthenticationDidChangeNotificationName object:nil];
        }

    }
    return self;
}


- (void)authenticationChanged {

   if ([GKLocalPlayer localPlayer].isAuthenticated && !userAuthenticated) {
        NSLog(@"Authentication Changed. User Authenticated");
        userAuthenticated = TRUE;
    }
    else if (![GKLocalPlayer localPlayer].isAuthenticated && userAuthenticated) {

        NSLog(@"Authentication Changed. User Not Authenticated");
        userAuthenticated = FALSE;
    }

}


-(void) authenticateLocalUser {
    if ([GKLocalPlayer localPlayer].authenticated == NO) {
        GKLocalPlayer* localPlayer = [GKLocalPlayer localPlayer];

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

        if(gcvc) {

            [self presentViewController:gcvc];
        }
        else {
            _gameCenterFeaturesEnabled = NO;
        }
    };
    }

    else if ([GKLocalPlayer localPlayer].authenticated == YES){
        _gameCenterFeaturesEnabled = YES;
    }

}

-(UIViewController*) getRootViewController {
    return [UIApplication sharedApplication].keyWindow.rootViewController;
}

-(void)presentViewController:(UIViewController*)gcvc {
    UIViewController* rootVC = [self getRootViewController];
    [rootVC presentViewController:gcvc animated:YES completion:nil];
}


-(void)gameCenterViewControllerDidFinish:(GKGameCenterViewController *)gameCenterViewController {


} 
@end

Then just use

[[gamecentercontrol sharedInstance] authenticateLocalUser]

in your view controller file where ever you want it to complete your authentication.

andyc101
  • 2,203
  • 2
  • 15
  • 8
1

#import "GameCenterManager.h" from apple sample code. set your project target to iOS 6.0 put the code in viewdidload it will work fine

   @property (nonatomic, retain) GameCenterManager *gameCenterManager;

    if([GameCenterManager isGameCenterAvailable])
      {
        self.gameCenterManager= [[GameCenterManager alloc] init];
            [self.gameCenterManager setDelegate: self];
        [self.gameCenterManager authenticateLocalUser];
        }
Vivek Sehrawat
  • 6,560
  • 2
  • 26
  • 39
  • Please use following link to setup game center with leader board. "https://developer.apple.com/library/ios/documentation/LanguagesUtilities/Conceptual/iTunesConnectGameCenter_Guide/Leaderboards/Leaderboards.html" – dheerendra Jun 08 '15 at 14:05
0
///////just Update the method 

- (void) authenticateLocalPlayer

{

GKLocalPlayer *localPlayer = [GKLocalPlayer localPlayer];

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


viewController=[[UIViewController alloc] init];

         if (viewController != nil)

         {

             //showAuthenticationDialogWhenReasonable: is an example method name. Create your own method that displays an authentication view when appropriate for your app.

             [self showAuthenticationDialogWhenReasonable: viewController];

         }

         else if (localPlayer.isAuthenticated)

         {

             //authenticatedPlayer: is an example method name. Create your own method that is called after the local player is authenticated.

             [self authenticatedPlayer: localPlayer];

         }

         else

         {

             [self disableGameCenter];

         }

     };

}
Kumar Saurabh
  • 2,297
  • 5
  • 29
  • 43
  • Welcome to Stack Overflow! While this code snippet may solve the question, [including an explanation](http://meta.stackexchange.com/questions/114762/explaining-entirely-‌​code-based-answers) really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion. – NathanOliver Nov 16 '15 at 18:40