Repeated Game Center Welcome Banners - ios5

How do you prevent the Game Center "Welcome Back" message from displaying every time your app wakes up from sleep? Some apps (like Scramble CE, Jetpack Joyride, and Bubblin) handle it correctly (just one welcome message on launch), while others (like Backgammon NJ) don't (welcome message every time the device wakes up).
The block code that's running is no longer in my control (sent to authenticateWithCompletionHandler), and the welcome message appears even if the block is empty anyway.
This behavior started happening with iOS 5.0 (was fine in 4.x), and happens in both the Simulator and real devices, in the Sandbox and not.
Thanks!

I'm from BivisSoft. We've developed Bubblin.
We have a Singleton that controls GameCenter.
Here is our code...
It's based on tutorial by Jacob Gundersen - http://www.raywenderlich.com/5480/beginning-turn-based-gaming-with-ios-5-part-1.
You can try it!
#pragma mark Singleton
static BSGameCenterManager *sharedHelper = nil;
+ (BSGameCenterManager *) sharedInstance {
if (!sharedHelper) {
sharedHelper = [[BSGameCenterManager alloc] init];
}
return sharedHelper;
}
#pragma mark Initializers
- (id)init {
if ((self = [super init])) {
gameCenterAvailable = [self isGameCenterAvailable];
if (gameCenterAvailable) {
self.localPlayerId = #"";
NSNotificationCenter *nc =
[NSNotificationCenter defaultCenter];
[nc addObserver:self
selector:#selector(authenticationChanged)
name:GKPlayerAuthenticationDidChangeNotificationName
object:nil];
}
}
return self;
}
// Check if GameCenter is Avaiable
- (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);
}
#pragma mark Authentication
- (void)authenticationChanged {
if ([GKLocalPlayer localPlayer].isAuthenticated &&
!self.userAuthenticated) {
NSLog(#"GameCenter authentication changed: player authenticated.");
self.userAuthenticated = TRUE;
self.localPlayerId = [[GKLocalPlayer localPlayer] playerID];
[[NSNotificationCenter defaultCenter] postNotificationName: #"gameCenterPlayerAuthenticated" object: nil userInfo: nil];
[self checkNotSentScores];
} else if (![GKLocalPlayer localPlayer].isAuthenticated &&
self.userAuthenticated) {
NSLog(#"GameCenter authentication changed: player not authenticated");
self.userAuthenticated = FALSE;
self.localPlayerId = #"";
}
}
- (void)authenticateLocalUser {
if (!gameCenterAvailable) return;
NSLog(#"GameCenter authenticating local user...");
if ([GKLocalPlayer localPlayer].authenticated == NO) {
[[GKLocalPlayer localPlayer]
authenticateWithCompletionHandler:nil];
} else {
NSLog(#"GameCenter already authenticated!");
}
}

In the Game Kit programming guide I found this bit of documentation:
All games that support Game Center must authenticate the local player
before using any of Game Center’s features. Your game should
authenticate the player as early as possible after launching. Ideally,
authentication should happen as soon as your game can present a user
interface to the player. When your game authenticates a player, Game
Kit first checks to see whether there is already an authenticated
player on the device. If there is an authenticated player, Game Kit
briefly displays a welcome banner to the player.
It doesn't seem as if you can change this behavior with the public API's.

Related

Sprite Kit Game Center Leaderboards

so I've been reading articles and the Apple developer library for a while but I'm not able to solve this.
I want to have a Game Center Leaderboard for my app, that's accessible through my game menu. However, since I'm using Sprite Kit for creating my game this causing me some problems.
I registered my app in iTunes Connect, enabled Game Center and the Identity information within Xcode matches the ones in iTunes Connect.
I created a button, that should open the Game Center Leaderboard. When it's tapped this is called:
GKLeaderboardViewController *leaderboardController = [[GKLeaderboardViewController alloc] init];
if (leaderboardController != NULL)
{
leaderboardController.timeScope = GKLeaderboardTimeScopeAllTime;
leaderboardController.leaderboardDelegate = self;
GKGameCenterViewController *gameCenterController = [[GKGameCenterViewController alloc] init];
if (gameCenterController != nil)
{
gameCenterController.viewState = GKGameCenterViewControllerStateLeaderboards;
UIViewController *vc = self.view.window.rootViewController;
[vc presentViewController: gameCenterController animated: YES completion:nil];
}
}
Now I get the message, that Game Center is unavailable because no player is signed in.
To fix that I tried using the authentication method from apple:
- (void) authenticateLocalPlayer
{
GKLocalPlayer *localPlayer = [GKLocalPlayer localPlayer];
localPlayer.authenticateHandler = ^(UIViewController *viewController, NSError *error){
if (viewController != nil)
{
//showAuthenticationDialogWhenReasonable: is an example method name.
[self showAuthenticationDialogWhenReasonable: viewController];
}
else if (localPlayer.isAuthenticated)
{
//authenticatedPlayer: is an example method name.
[self authenticatedPlayer: localPlayer];
}
else
{
[self disableGameCenter];
}
}];
}
I'm now having trouble defining these methods being called in the authentication method.
My explicit question: How do I authenticate a local player?

Game Center Matchmaking GKTurnBasedMatch has significant lag (~1 min)

I'm implementing a turn-based game with multiplayer mode through gamecenter. I have 2 devices (1 ipad, 1 iphone) to test in sandbox mode which were working fine but lately it has started to struggle in auto matchmaking process. After I send the first turn from one user, the other device doesn't immediately recognize that game but opens up its own fresh game. Before it was able to immediately spot the game started in the other device and matchmaking was fairly straightforward. And I don't remember changing any parts relevant to matchmaking (NSCoding, GKTurnBasedEventHandler, GKTurnBasedMatchmakerViewControllerDelegate delegate methods etc).
Now I send the first turn from one device and need to wait around 1 min so the other device can successfully connect to that game. After connection occurs endTurnWithMatchData calls work without any problems, it can send and receive data within 1-2 secs. But it won't be a good UX if users start a fresh game and had to wait 1 min so another user can connect to his game. Has anyone been experiencing significant lag in auto matchmaking process? I didn't implement invitations yet, so I cannot check it. The matchdata I archive with NSKeyedArchiver seemed quite big, 3396 bytes, even for a fresh game with almost no data. And here are relevant parts of my code:
GameOptionsViewController:
- (void)turnBasedMatchmakerViewControllerWasCancelled:(GKTurnBasedMatchmakerViewController *)viewController
{
[self dismissViewControllerAnimated:YES completion:nil];
}
- (void)turnBasedMatchmakerViewController:(GKTurnBasedMatchmakerViewController *)viewController didFailWithError:(NSError *)error
{
[self dismissViewControllerAnimated:YES completion:nil];
}
- (void)turnBasedMatchmakerViewController:(GKTurnBasedMatchmakerViewController *)viewController didFindMatch:(GKTurnBasedMatch *)match
{
[self dismissViewControllerAnimated:NO completion:nil];
self.gcMatch = match;
[self performSegueWithIdentifier:#"GameMultiplayer" sender:self];
}
- (void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if([segue.identifier isEqualToString:#"GameMultiplayer"])
{
GameViewController *GameVC = (GameViewController *)segue.destinationViewController;
[GameVC setGameMode:GAMEMODE_MULTIPLAYER_SAMEDEVICE];
//Multiplayer game it is
if(self.gcMatch != nil)
{
[GameVC setGameMode:GAMEMODE_MULTIPLAYER_GAMECENTER];
GameVC.gcMatchDelegate = self;
GameVC.gcMatch = self.gcMatch;
NSLog(#"Game OVC Segue: Match ID | %#", self.gcMatch.matchID);
}
}
else
{
...
}
}
GameViewController:
//This method is called according to user actions
//It's the only method I use to send data to other participant
-(void) sendCurrentGameDataWithNewTurn:(BOOL) newTurn
{
NSLog(#"Sending game data current participant : %#", gcMatch.currentParticipant.playerID);
//Update match data if it is corrupted anyhow
if (gcMatch.currentParticipant == nil)
{
[GKTurnBasedMatch loadMatchWithID:gcMatch.matchID withCompletionHandler:^(GKTurnBasedMatch *match, NSError *error)
{
if (error != nil)
{
NSLog(#"Error :%#", error);
return ;
}
[self sendCurrentGameDataWithNewTurn:newTurn];
}];
}
else
{
NSData *matchData = [NSKeyedArchiver archivedDataWithRootObject:game];
//Game advances to new player, buttons are disabled
if(newTurn)
{
NSLog(#"SENDING NEW TURN");
NSUInteger currentIndex = [gcMatch.participants
indexOfObject:gcMatch.currentParticipant];
GKTurnBasedParticipant *nextParticipant;
nextParticipant = [gcMatch.participants objectAtIndex:
((currentIndex + 1) % [gcMatch.participants count])];
[gcMatch endTurnWithNextParticipants:[NSArray arrayWithObject:nextParticipant] turnTimeout:GC_TURN_TIMEOUT matchData:matchData completionHandler:^(NSError *error) {
NSLog(#"Sent");
if (error) {
NSLog(#"SNT - %#", error);
}
}];
}
else
{
NSLog(#"ONLY UPDATING DATA");
[gcMatch saveCurrentTurnWithMatchData:matchData completionHandler:^(NSError *error) {
NSLog(#"Sent");
if (error) {
NSLog(#"OUD - %#", error);
}
}];
}
}
}
-(void) updateGameDataWithGCMatch
{
//Update whole game data
self.game = [NSKeyedUnarchiver unarchiveObjectWithData:self.gcMatch.matchData];
//Update game ui
...
}
-(void) handleTurnEventForMatch:(GKTurnBasedMatch *)match didBecomeActive:(BOOL)didBecomeActive
{
//Check if I got data for the currently active match that options vc forwarded me here, if not do some debug print and return
if(![self.gcMatch.matchID isEqual:match.matchID])
{
//For debugging reasons I skip if i get info for any previous match (other player quit etc)
NSLog(#"GCMatch matchID: %# match matchID: %#",self.gcMatch.matchID,match.matchID);
return;
}
NSLog(#"Turn event handle");
self.gcMatch = match;
if([match.currentParticipant.playerID isEqualToString: [GKLocalPlayer localPlayer].playerID ])
{
//Disable field buttons
[self setFieldButtonsEnabled:TRUE];
[self turnChangeAnimationFromLeftToRight:FALSE];
}
[self updateGameDataWithGCMatch];
}
As for your question:
I myself tempered with matchmaking over Game Center quite a bit and also experienced lags quite frequently, which have been proven to not have been caused by my site but by apples game center servers.
As for additional guidance:
As far as I can see your current approach to matchmaking on a device is:
look if there is a match I can connect to --> If YES request gamedata and connect to the match ELSE start your own match and broadcast the matchdata
From my experience it is better practice to start with matchrequestbroadcasts, wait until you find a second player, define the server device (e.g. by lower checksum of game-center names) and then start the game on that device.

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.

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:]