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.
}
Related
I am making an iphone app (a game) and while it will not be my first game, it will be my first game that uses a network connection (I intend on adding a few multiplier features)
So, now for my question.
I want the player to be able to open my app, sign into game center, and then use that to connect to my server. Is this possible? In other words:
Player signs in to game center
Sends username to my server
if (game center username doesnt exist)
{
Add their username to my sql database
}
else if (username does exist)
{
Send sign in successful message to device.
}
I believe I read another question from a while ago asking how to do this, but the answer said something about using the device UDID, which I do not want to do (for obvious reasons if you read technology related news).
So anyway, is there a trusted way that I can be sure that the user is who they say they are once they authenticate with game center that I can use to authenticate them against my server? Thank you
I think instead of UDID you can use GKPlayer.playerID which is unique to each gamecenter account. For your other purposes you can customize the completion block of authenticate handler
GKLocalPlayer * __weak localPlayer = [GKLocalPlayer localPlayer];
localPlayer.authenticateHandler = ^(UIViewController *viewController, NSError *error)
{
if (viewController != nil)
{
[currentViewController presentViewController:viewController animated:YES completion:nil];
}
else if (localPlayer.isAuthenticated)
{
NSLog(#"Player authenticated");
/* Your custom methods here
Sends username to my server -- send localPlayer.playerID to your server
if (game center username doesnt exist)
{
Add their username to my sql database
}
else if (username does exist)
{
Send sign in successful message to device.
}
*/
}
else
{
NSLog(#"Player not authenticated");
//disableGameCenter
}
NSLog(#"Error: %#",error);
};
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.
I set up an achievement for passing the first level of my game and it works but when i replay the level and pass it it shows the notification banner again, how can i prevent this from happening?
Use this method to submit the achievement:
-(void) reportAchievementWithID:(NSString*) achievementID {
[GKAchievement loadAchievementsWithCompletionHandler:^(NSArray *achievements, NSError *error) {
if(error) NSLog(#"error reporting ach");
for (GKAchievement *ach in achievements) {
if([ach.identifier isEqualToString:achievementID]) { //already submitted
return ;
}
}
GKAchievement *achievementToSend = [[GKAchievement alloc] initWithIdentifier:achievementID];
achievementToSend.percentComplete = 100;
achievementToSend.showsCompletionBanner = YES;
[achievementToSend reportAchievementWithCompletionHandler:NULL];
}];
}
Save that the user has passed the level to NSUserDefaults, then when the user passes the level check NSUserDefaults for your key, if it is there then don't do the achievement code for Game Center.
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
}
}];
}
I have my app give a notification when a Game Center achievement is achieved/reaches 100%, however it shows the notification every times the user completes it, but I only want it to notify the first time in actually completed.
I found this in the Apple docs:
http://developer.apple.com/library/ios/documentation/NetworkingInternet/Conceptual/GameKit_Guide/Achievements/Achievements.html#//apple_ref/doc/uid/TP40008304-CH7-SW11
However didn't quite understand how it help fix my issue.
I only want to call this notification once, when the achievement is first achieved. So only show it if its not already achieved.
Edit
I have this to unlock the achievement:
- (void) reportAchievementIdentifier: (NSString*) identifier percentComplete: (float) percent
{
GKAchievement *achievement = [[[GKAchievement alloc] initWithIdentifier: identifier] autorelease];
if (achievement)
{
achievement.percentComplete = percent;
[achievement reportAchievementWithCompletionHandler:^(NSError *error)
{
if (error != nil)
{
// Retain the achievement object and try again later (not shown).
}
}];
}
}
And unlock with this:
[self reportAchievementIdentifier:identifier percentComplete:percent];
Then show the notification with this line:
[[GKAchievementHandler defaultHandler] notifyAchievementTitle:#"Title" andMessage:#"Message"];
So do I simply need something like this in that code chunk?
if (achievement.completed != TRUE) {
[[GKAchievementHandler defaultHandler] notifyAchievementTitle:#"Title" andMessage:#"Message"];
}
There's a "completed"-property in GKAchievement... Try to make a new GKAchievement and check if it's not completed, then unlock it.