How to check when GameKit UI displays - iphone

I've got very simple game that I'm trying to connect to Game Center.
After calling:
GKLocalPlayer *localPlayer = [GKLocalPlayer localPlayer];
[localPlayer authenticateWithCompletionHandler:^(NSError *error) {
}];
First time, Game Center dialog comes up asking for username and password and after that the whole Game Center windows slides up to set up account.
The problem is that my game is still running underneath that window and I just can not find any notifications to pause it.
viewWillDisappear, viewDidDisappear in UIViewController don't get called; applicationWillResignActive in AppDelegate is not called either.
Is there any way to detect that Game Center windows shows up?

Could you start the game after the user is authenticated?
Here would be an example from Apple's Docs (http://developer.apple.com/library/ios/#documentation/NetworkingInternet/Conceptual/GameKit_Guide/Users/Users.html)
- (void) authenticateLocalPlayer
{
GKLocalPlayer *localPlayer = [GKLocalPlayer localPlayer];
[localPlayer authenticateWithCompletionHandler:^(NSError *error) {
if (localPlayer.isAuthenticated)
{
// Start Game
}
}];
}

Related

Game Center Invitations Not Displayed

I have been developing a game which allows for multiplayer matches. I had previous tested the multiplayer invitations and they had all worked. Sending a request from one device displayed a banner on the other and if the invite was accepted the game started.
Just before submitting the app, two nights ago, I tested this functionality again only to find that it has stopped working.
- (void)authenticateLocalUser:(UIViewController *)viewController :(id<GCHelperDelegate>)theDelegate
{
delegate = theDelegate;
self.presentingViewController = viewController;
if (!gameCenterAvailable) {
// Game Center is not available.
userAuthenticated = FALSE;
}
else{
GKLocalPlayer *localPlayer = [GKLocalPlayer localPlayer];
/*
The authenticateWithCompletionHandler method is like all completion handler methods and runs a block
of code after completing its task. The difference with this method is that it does not release the
completion handler after calling it. Whenever your application returns to the foreground after
running in the background, Game Kit re-authenticates the user and calls the retained completion
handler. This means the authenticateWithCompletionHandler: method only needs to be called once each
time your application is launched. This is the reason the sample authenticates in the application
delegate's application:didFinishLaunchingWithOptions: method instead of in the view controller's
viewDidLoad method.
Remember this call returns immediately, before the user is authenticated. This is because it uses
Grand Central Dispatch to call the block asynchronously once authentication completes.
*/
//ios 6
[localPlayer setAuthenticateHandler:(^(UIViewController* viewcontroller, NSError *error) {
if (viewcontroller != nil){
userAuthenticated = FALSE;
[self.presentingViewController presentViewController: viewcontroller animated: YES completion:nil];
}
else if (localPlayer.isAuthenticated){
// Enable Game Center Functionality
userAuthenticated = TRUE;
[self checkForInvite:self.presentingViewController :delegate];
if (! self.currentPlayerID || ! [self.currentPlayerID isEqualToString:localPlayer.playerID]) {
// Current playerID has changed. Create/Load a game state around the new user.
self.currentPlayerID = localPlayer.playerID;
// get friends of local player
[localPlayer loadFriendsWithCompletionHandler:^(NSArray *friends, NSError *error) {
if (friends != nil)
{
[self loadPlayerData: friends];
}
}];
}
}
else{
userAuthenticated = FALSE;
}
[scoreHandler setGameCentreAvailable:userAuthenticated];
})];
}
}
- (void)checkForInvite :(UIViewController *)viewController :(id<GCHelperDelegate>)theDelegate
{
delegate = theDelegate;
self.presentingViewController = viewController;
NSLog(#"Invite handler installed");
[GKMatchmaker sharedMatchmaker].inviteHandler = ^(GKInvite *acceptedInvite, NSArray *playersToInvite) {
// Insert application-specific code here to clean up any games in progress.
if (acceptedInvite){
NSLog(#"Accepted");
GKMatchmakerViewController *mmvc = [[[GKMatchmakerViewController alloc] initWithInvite:acceptedInvite] autorelease];
mmvc.matchmakerDelegate = self;
[viewController presentViewController: mmvc animated: YES completion:nil];
} else if (playersToInvite) {
NSLog(#"Match Request");
GKMatchRequest *request = [[[GKMatchRequest alloc] init] autorelease];
request.minPlayers = 2;
request.maxPlayers = 2;
request.playersToInvite = playersToInvite;
GKMatchmakerViewController *mmvc = [[[GKMatchmakerViewController alloc] initWithMatchRequest:request] autorelease];
mmvc.matchmakerDelegate = self;
[viewController presentViewController: mmvc animated: YES completion:nil];
}
};
}
The debug window in xcode shows the following:
2013-03-27 18:06:20.112 MyApp[791:907] Authentication changed: player authenticated.
2013-03-27 18:06:21.219 MyApp[791:907] Invite handler installed
Mar 27 18:06:21 Neils-iPhone MyApp[791] <Notice>: 18:06:21.356712 com.apple.GameKitServices: -[GKDiscoveryManager startAdvertisingLocalPlayer:discoveryInfo:]: I am [<nil>] [7989F444CF2BDA83] discoveryInfo [{
e = 2;
h = A42FD7FD;
}]
Is the "I am []..." significant in the lines above?
I have even downloaded and run the tutorial from Ray Wenderlich's site for creating a multiplayer game and tried that. That exhibits the same issues, unless it is running in the foreground on both devices. My app does not display invitation requests even if running in the foreground.
Has anyone else experienced this problem or have any ideas what is going on? authenticateLocalUser is called from applicationDidFinishLaunching
The only way I could make invites-by-name work is by going to Settings/Notifications/Game Center and making Game Center display Alerts, not Banners.
If you have GC display alerts, you get a popup box like this:
This dialog acts like a big parent. If the user hits Accept, then your [GKMatchmaker sharedMatchmaker].inviteHandler gets invoked.
If the user hits Decline, then your game never knows that he was invited to any party ever. User hitting Decline means the parent rips up the invitation and never tells his child he got invited to a game at all.
This is the only way I could get invite-by-name to work.

GameCenter Operation cancelled if account never used with game center

I am using GameCenter on my app. I have these lines
GKLocalPlayer *localPlayer = [GKLocalPlayer localPlayer];
[localPlayer authenticateWithCompletionHandler:^(NSError *error) {
if (localPlayer.isAuthenticated)
{
}
the problem is that the localPlayer.isAuthenticated flag is always TRUE bur error variable comes with code 2 = "operation was cancelled" (???).
I have sign out from device's game center and from the store but this flag is always true and I do not see the game center sign in that my app should show when it starts. I don't see either the "welcome" banner that always show when a game that uses game center starts.
How do I force a sign out of game center to make the sign in window to show again?
I am compiling for iOS 4.3.
thanks
What I have discovered now is that this happens if you never signed in on device's game center. Once you login there, and say you want to use your username on game center, the app works. The worst part is this: suppose someone downloads the game but does not have the game center set yet. So, the game will never work for them? My game is supposed to work exclusively with game center on. So, for me this is an issue.
For me its working great. Just changed code from
if([GKLocalPlayer localPlayer].authenticated)
To
if([GKLocalPlayer localPlayer].authenticated == NO)
//Other codes
if([GKLocalPlayer localPlayer].authenticated == NO)
{
[[GKLocalPlayer localPlayer] authenticateWithCompletionHandler:^(NSError *error)
{
[self processGameCenterAuth: error];
}];
}
- (void) processGameCenterAuth: (NSError*) error
{
if(error == NULL)
{
[mGameCenterManager reloadHighScoresForCategory: self.currentLeaderBoard];
}
else
{
// NSLog(#"%#\n\n",[NSString stringWithFormat: #"Reason: %#", [error localizedDescription]]);
AppController *app = (AppController*)[UIApplication sharedApplication].delegate;
if(!app.isgameCenterStarted)
{
UIAlertView* alert= [[[UIAlertView alloc] initWithTitle:#"Game Center Unavailable" message: #"Player is not signed in"
delegate: NULL cancelButtonTitle: #"OK" otherButtonTitles: NULL] autorelease];
[alert show];
}
else
{
[[NSNotificationCenter defaultCenter] postNotificationName:#"GameCenterUnAvailable" object:nil];
}
}
}

Login in iPhone App via GameKit

I want to login to my app via GameCenter Login API.
Is it possible ?
Is Apple game Center login API public?
If you're using iOS 6, see the documentation for GKLocalPlayer. You'll see that you assign a block to the 'authenticateHandler' property of localPlayer. When you assign it, if the player isn't already logged into Game Center, one of the arguments to the block (UIViewController *viewController) gets filled in with the address of a view controller that will present the regular Apple Game Center login screen. After you get that address you do presentViewController:viewController and the user sees the normal Apple login screen. When the user finishes interacting with it you get a call back to 'gameCenterViewControllerDidFinish'. The block you provide runs more than once, which makes the process pretty hard to follow, but it works. For what it's worth I'll post below a method I'm using that seems to work. It assumes either iOS5 or iOS6. It isn't be good for anything earlier than 5. OS6 is method that returns YES on iOS6 and NO otherwise. This wasn't written for public consumption so please excuse all the debugging stuff and the unexplained stuff in it.
-(void) authenticateLocalPlayer {
ANNOUNCE
GKLocalPlayer *localPlayer = [GKLocalPlayer localPlayer];
_lastError = nil;
//iOS 6
if ( [self os6] ) {
localPlayer.authenticateHandler = ^(UIViewController *loginVC, NSError *error) {
NSLog(#"in authenticateHandler 1");
[self setLastError:error];
//... resume application responses
[[CCDirector sharedDirector] resume]; //if not paused does nothing
if ( [GKLocalPlayer localPlayer].authenticated) {
NSLog(#"in authenticateHandler 2 - local player is authenticated");
} else if (loginVC) {
NSLog(#"in authenticateHandler 3 - local player is not authenticated, will present VC");
//... pause applications responses
[[CCDirector sharedDirector] pause];
[self presentViewController:loginVC];
} else {
NSLog(#"in authenticateHandler 4 - local player is NOT authenticated, no VC returned");
}
NSLog(#"authenticateHandler error: %#", error.localizedDescription);
};
//iOS 5
} else {
if ( [GKLocalPlayer localPlayer].authenticated == NO ) {
//no completion handler because we're relying on NSNotificationCenter
[[GKLocalPlayer localPlayer] authenticateWithCompletionHandler:nil];
NSLog(#"local player authentication requested");
} else {
NSLog(#"local player was already authenticated");
}
}
}
You can do that surely. there is no gamecenter API for direct use. you can show the gamecenter authentication screen and after authentication, you can proceed.

Gaming Center iphone-sdk, how to get current player's nickname?

When my app start it should promt user to log into gaming center so that I can retrieve his nickname and then use it later to display his name,
I have the following code which somehow worked once:
- (void) authenticateLocalPlayer
{
GKLocalPlayer *localPlayer = [GKLocalPlayer localPlayer];
[localPlayer authenticateWithCompletionHandler:^(NSError *error) {
if (localPlayer.isAuthenticated)
{
// Perform additional tasks for the authenticated player.
}
}];
}
It showed a alert view with and some buttons, but it doesn't work. Help, maybe there's an easier way to retrieve current player's nickname. Thanks!!
Getting a player's alias requires authentication with Game Center. Once you have authentication, all you have to do is get your GKPlayer instance by doing this:
GKLocalPlayer *lp = [GKLocalPlayer localPlayer];
and then, just make sure authentication occurred and get your alias:
if (lp.authenticated) {
return lp.alias;
//Any other stuff you need to do with this local player's instance goes here.
}

Game center authentication block keeps getting called

So, I noticed that after calling initializeGameCenter() once, every time my application gets back to the foreground the below block(after authenticateWithCompletionHandler) is getting called - is this regular behavior of Game Center ?? (I made sure to place a breakpoint to verify that only the block is getting called but not the initializeGameCenter itself)
- (void)initializeGameCenter
{
// Don't initialize Game Center unless we have access to the classes from iOS 4.1 or greater.
if (![self isGameCenterAvailable]) {
return;
}
[[GKLocalPlayer localPlayer] authenticateWithCompletionHandler:^(NSError *error) {
NSDictionary *userInfo = nil;
if (error == nil) {
// Game Center will present a "Welcome Back" message when we have authenticated
GTMLoggerInfo(#"Game Center successfully authenticated");
}
else {
userInfo = [NSDictionary dictionaryWithObject:error forKey:#"NSError"];
GTMLoggerDebug(#"error authenticating game center");
}
[[NSNotificationCenter defaultCenter] postNotificationName:GameCenterAuthenticateDidFinishNotification
object:self
userInfo:userInfo];
}];
}
From the Game Kit Programming Guide:
" [when] your game moves back to the foreground, Game Kit authenticates the player, and your authentication handler is called."
http://developer.apple.com/library/ios/documentation/NetworkingInternet/Conceptual/GameKit_Guide/Users/Users.html#//apple_ref/doc/uid/TP40008304-CH8-SW11
I.e. your completion block will get called every time your app moves to the foreground, as if you had called -[[GKLocalPlayer localPlayer] authenticateWithCompletionHandler:]