I'm attempting to implement GameKit in my OSX game. Unfortunately I can't find much information about how to do this; all tutorials seem to be iOS (though the documentation clearly states "Game Center is available on iOS and OS X").
Everything is compiling fine; the problem comes when I try to authenticate the local user:
[[GKLocalPlayer localPlayer] setAuthenticateHandler:^(id viewController, NSError *error) {
if(error) {
DLog(@"Error: %@",error);// This is always returning an error
}
else if(viewController) {
// WHAT DO I DO HERE??
}
}];
I have 2 problems:
First, the handler always gets an error: Error Domain=GKErrorDomain Code=6 "The requested operation could not be completed because local player has not been authenticated." UserInfo=0x10103bc70 {NSLocalizedDescription=The requested operation could not be completed because local player has not been authenticated.}
.
Second, I don't know how to present the view controller.
On the iPhone, this code works fine: there's no error, and I simply present the viewController (which is the login screen).
I'm sorry, and I realize that my answer is almost a year late, but in case it may still be relevant for others who are still asking this question like myself. Ed Marty is mostly correct, but what I've discovered that works for me is this.
[[GKLocalPlayer localPlayer] setAuthenticateHandler:^(NSViewController < GKViewController > viewController, NSError *error) {
if(error) {
NSLog(@"Error: %@",error);
}
else if(viewController) {
GKDialogController *presenter = [GKDialogController sharedDialogController];
presenter.parentWindow = myWindow;
[presenter presentViewController:viewController];
}
}];
The main difference is using NSViewController conforming to GKViewController instead of id.(Also, I used NSLog instead of DLog, but that isn't too important).
"I have found, however, that this is completely useless, and it presents the login dialog before it even calls the handler."
To make sure that it works, set up a new Game Center account through your app. When you run your program and it loads a window for you to sign in, press "Create New Apple ID" even if you already have an Apple ID. The button may not work, so if that's the case, open up Game Center and press "Create New Apple ID." Either way, your objective is to create a "Sandboxed" Game Center account, which you can learn more about here: https://developer.apple.com/library/ios/documentation/NetworkingInternet/Conceptual/GameKit_Guide/TestingYourGameCenter-AwareGame/TestingYourGameCenter-AwareGame.html#//apple_ref/doc/uid/TP40008304-CH17-SW1
You can tell if the account is Sandboxed if when you are reading the terms and conditions, the word "Sandbox" appears on a yellow banner in the top left. Once again, I'm sorry I'm late, but hopefully this clears the topic for all future viewers.
P.S. I'm sorry that the code did not format properly.
I have found that the GameKit documentation for OSX is woefully lacking, misleading, and sometimes downright wrong. That said, here's what you're supposed to do, according to this document:
[[GKLocalPlayer localPlayer] setAuthenticateHandler:^(id viewController, NSError *error) {
if(error) {
DLog(@"Error: %@",error);// This is always returning an error
}
else if(viewController) {
GKDialogController *presenter = [GKDialogController sharedDialogController];
presenter.parentWindow = myWindow;
[presenter presentViewController:viewController];
}
}];
I have found, however, that this is completely useless, and it presents the login dialog before it even calls the handler.