diff --git a/.gitignore b/.gitignore index 4db7e66..f21bdf7 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ # Mac crap -.DS_Store \ No newline at end of file +.DS_Store +.idea \ No newline at end of file diff --git a/README.md b/README.md index dadab4b..b8fa6f7 100644 --- a/README.md +++ b/README.md @@ -40,6 +40,20 @@ For more information, see the [PhoneGap build docs](http://docs.build.phonegap.c ## Usage +### Auth with Third Party backend + +You should do this as soon as your deviceready event has been fired. The plug handles the various auth scenarios for you. +You need to enable game-center for your app from Itunes Connect +> Sometimes it's needed to add some dummy leaderboard from game-center tab in itunes connect to make it work +``` +var successCallback = function (data) { + alert(data.alias); + // data.alias, data.bundleId, data.displayName, data.playerId, data.publicKeyUrl, data.salt, data.signature, data.timestamp +}; + +gamecenter.generateIdentityVerification(successCallback, failureCallback); +``` + ### Auth You should do this as soon as your deviceready event has been fired. The plug handles the various auth scenarios for you. diff --git a/src/ios/GameCenter.h b/src/ios/GameCenter.h index a4c78c1..6210b28 100644 --- a/src/ios/GameCenter.h +++ b/src/ios/GameCenter.h @@ -11,6 +11,7 @@ @interface GameCenter : CDVPlugin - (void) auth:(CDVInvokedUrlCommand*)command; +- (void) generateIdentityVerification:(CDVInvokedUrlCommand*)command; - (void) getPlayerImage:(CDVInvokedUrlCommand*)command; - (void) submitScore:(CDVInvokedUrlCommand*)command; - (void) showLeaderboard:(CDVInvokedUrlCommand*)command; diff --git a/src/ios/GameCenter.m b/src/ios/GameCenter.m index ec52b4a..32b229c 100644 --- a/src/ios/GameCenter.m +++ b/src/ios/GameCenter.m @@ -13,7 +13,7 @@ - (void) auth:(CDVInvokedUrlCommand*)command; { [self.commandDelegate runInBackground:^{ - __weak GKLocalPlayer *localPlayer = [GKLocalPlayer localPlayer]; + GKLocalPlayer *localPlayer = [GKLocalPlayer localPlayer]; localPlayer.authenticateHandler = ^(UIViewController *viewController, NSError *error) { CDVPluginResult* pluginResult = nil; @@ -47,9 +47,65 @@ - (void) auth:(CDVInvokedUrlCommand*)command; }]; } +- (void) generateIdentityVerification:(CDVInvokedUrlCommand*)command; +{ + [self.commandDelegate runInBackground:^{ + + GKLocalPlayer *localPlayer = [GKLocalPlayer localPlayer]; + + localPlayer.authenticateHandler = ^(UIViewController *viewController, NSError *error) { + CDVPluginResult* pluginResult = nil; + if (viewController != nil) + { + // Login required + [self.viewController presentViewController:viewController animated:YES completion:nil]; + } + else + { + if (localPlayer.isAuthenticated) + { + [localPlayer generateIdentityVerificationSignatureWithCompletionHandler:^(NSURL *publicKeyUrl, NSData *signature, NSData *salt, uint64_t timestamp, NSError *error) { + __block CDVPluginResult* pluginResult = nil; + if(error != nil) + { + pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:[error localizedDescription]]; + [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId]; + } else { + NSDictionary* user = @{ + @"playerId":localPlayer.playerID, + @"alias":localPlayer.alias, + @"displayName":localPlayer.displayName, + @"publicKeyUrl":[publicKeyUrl absoluteString], + @"signature":[self base64forData:signature], + @"salt":[self base64forData:salt], + @"timestamp":@(timestamp), + @"bundleId": [[NSBundle mainBundle] bundleIdentifier] + }; + pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsDictionary:user]; + [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId]; + } + + }]; + + } + else if (error != nil) + { + pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:[error localizedDescription]]; + [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId]; + } + else + { + pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR]; + [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId]; + } + } + }; + }]; +} + - (void) getPlayerImage:(CDVInvokedUrlCommand*)command; { - __weak GKLocalPlayer *localPlayer = [GKLocalPlayer localPlayer]; + GKLocalPlayer *localPlayer = [GKLocalPlayer localPlayer]; __block CDVPluginResult* pluginResult = nil; NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, @@ -188,7 +244,7 @@ - (void) resetAchievements:(CDVInvokedUrlCommand*)command; [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId]; } - ]; + ]; } @@ -236,10 +292,10 @@ - (void) gameCenterViewControllerDidFinish:(GKGameCenterViewController *)gameCen - (void) getAchievements:(CDVInvokedUrlCommand*)command; { - __block CDVPluginResult* pluginResult = nil; - NSMutableArray *earntAchievements = [NSMutableArray array]; + __block CDVPluginResult* pluginResult = nil; + NSMutableArray *earntAchievements = [NSMutableArray array]; - [GKAchievement loadAchievementsWithCompletionHandler:^(NSArray *achievements, NSError *error) + [GKAchievement loadAchievementsWithCompletionHandler:^(NSArray *achievements, NSError *error) { if (error == nil) { @@ -264,5 +320,35 @@ - (void) getAchievements:(CDVInvokedUrlCommand*)command; [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId]; }]; } +- (NSString*)base64forData:(NSData*)theData { + const uint8_t* input = (const uint8_t*)[theData bytes]; + NSInteger length = [theData length]; + + static char table[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/="; + + NSMutableData* data = [NSMutableData dataWithLength:((length + 2) / 3) * 4]; + uint8_t* output = (uint8_t*)data.mutableBytes; + + NSInteger i; + for (i=0; i < length; i += 3) { + NSInteger value = 0; + NSInteger j; + for (j = i; j < (i + 3); j++) { + value <<= 8; + + if (j < length) { + value |= (0xFF & input[j]); + } + } + + NSInteger theIndex = (i / 3) * 4; + output[theIndex + 0] = table[(value >> 18) & 0x3F]; + output[theIndex + 1] = table[(value >> 12) & 0x3F]; + output[theIndex + 2] = (i + 1) < length ? table[(value >> 6) & 0x3F] : '='; + output[theIndex + 3] = (i + 2) < length ? table[(value >> 0) & 0x3F] : '='; + } + + return [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding]; +} @end diff --git a/www/gamecenter.js b/www/gamecenter.js index b35213a..d6b68e4 100644 --- a/www/gamecenter.js +++ b/www/gamecenter.js @@ -9,6 +9,10 @@ GameCenter.prototype.auth = function (success, failure) { exec(success, failure, "GameCenter", "auth", []); }; +GameCenter.prototype.generateIdentityVerification = function (success, failure) { + exec(success, failure, "GameCenter", "generateIdentityVerification", []); +}; + GameCenter.prototype.getPlayerImage = function (success, failure) { exec(success, failure, "GameCenter", "getPlayerImage", []); };