Skip to content

Instantly share code, notes, and snippets.

@zhiguangwang
Created June 4, 2015 03:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zhiguangwang/f748b872301dfd8c5634 to your computer and use it in GitHub Desktop.
Save zhiguangwang/f748b872301dfd8c5634 to your computer and use it in GitHub Desktop.
Game Center authentication with third party server.
#pragma mark - GameKit related stuff
// See documentation:
// https://developer.apple.com/library/ios/documentation/NetworkingInternet/Conceptual/GameKit_Guide/Users/Users.html#//apple_ref/doc/uid/TP40008304-CH8-SW17
// Call this method in (void)viewDidAppear:(BOOL)animated
- (void) authenticateLocalPlayer {
[GKLocalPlayer localPlayer].authenticateHandler = ^(UIViewController *viewController, NSError *error) {
if (viewController != nil) {
[self showAuthenticationDialog: viewController];
return;
}
GKLocalPlayer *localPlayer = [GKLocalPlayer localPlayer];
if (localPlayer.isAuthenticated) {
[self authenticatedPlayer: localPlayer];
} else {
NSLog(@"Not authenticated!\n");
}
};
}
- (void) showAuthenticationDialog:(UIViewController*)viewController {
[self presentViewController: viewController animated: YES completion:nil];
}
- (void) authenticatedPlayer:(GKLocalPlayer*)localPlayer {
NSLog(@"Authenticated player: %@\n", localPlayer.playerID);
// See documentation:
// https://developer.apple.com/library/ios/documentation/GameKit/Reference/GKLocalPlayer_Ref/index.html#//apple_ref/occ/instm/GKLocalPlayer/generateIdentityVerificationSignatureWithCompletionHandler:
[localPlayer generateIdentityVerificationSignatureWithCompletionHandler:^(NSURL *publicKeyUrl, NSData *signature, NSData *salt, uint64_t timestamp, NSError *error) {
if (error) {
NSLog(@"ERROR: %@", error);
return;
}
// Data to be sent to your own server for authentication
NSLog(@"public_key_url=%@\n", publicKeyUrl.absoluteString);
NSLog(@"signature=%@\n", [signature base64EncodedStringWithOptions:0]);
NSLog(@"salt=%@\n", [salt base64EncodedStringWithOptions:0]);
NSLog(@"timestamp=%@\n", [NSString stringWithFormat:@"%llu", timestamp]);
NSLog(@"player_id=%@\n", localPlayer.playerID);
NSLog(@"bundle_id=%@\n", [[NSBundle mainBundle] bundleIdentifier]);
}];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment