Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

generateIdentityVerification method added for third-party server authentication #39

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
method fixed, README added
var77 committed Sep 14, 2018
commit 4d0449e70e65e7767c6e62f733b6372ed291961e
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
# Mac crap
.DS_Store
.DS_Store
.idea
20 changes: 19 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -23,7 +23,7 @@ cordova plugin add cordova-plugin-game-center
#### Latest version from GitHub

```
cordova plugin add https://github.com/leecrossley/cordova-plugin-game-center.git
cordova plugin add https://github.com/var77/cordova-plugin-game-center.git
```

You **do not** need to reference any JavaScript, the Cordova plugin architecture will add a gamecenter object to your root automatically when you build. It will also automatically add the GameKit framework dependency.
@@ -38,8 +38,26 @@ Add the following to your `config.xml` to use version 0.4.1 (you can also omit t

For more information, see the [PhoneGap build docs](http://docs.build.phonegap.com/en_US/configuring_plugins.md.html#Plugins).

**Before building for iOS disable ARC for GameCenter.m file**
> Project -> Build Phases -> Compile Sources -> Double Click on GameCenter.m -> paste -fno-objc-arc
![alt Disable ARC](https://user-images.githubusercontent.com/17221195/45549686-abd04580-b839-11e8-88ab-898cb605ba4f.png)

## 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.
66 changes: 50 additions & 16 deletions src/ios/GameCenter.m
Original file line number Diff line number Diff line change
@@ -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;
@@ -51,7 +51,7 @@ - (void) generateIdentityVerification:(CDVInvokedUrlCommand*)command;
{
[self.commandDelegate runInBackground:^{

__weak GKLocalPlayer *localPlayer = [GKLocalPlayer localPlayer];
GKLocalPlayer *localPlayer = [GKLocalPlayer localPlayer];

localPlayer.authenticateHandler = ^(UIViewController *viewController, NSError *error) {
CDVPluginResult* pluginResult = nil;
@@ -68,40 +68,44 @@ - (void) generateIdentityVerification:(CDVInvokedUrlCommand*)command;
__block CDVPluginResult* pluginResult = nil;
if(error != nil)
{
return; //some sort of error, can't authenticate right now
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];
}
NSDictionary* user = @{
@"playerId":localPlayer.playerID,
@"alias":localPlayer.alias,
@"displayName":localPlayer.displayName,
@"publicKeyUrl":publicKeyUrl,
@"signature":signature,
@"salt":salt,
@"timestamp":@(timestamp),
@"bundleId": [[NSBundle mainBundle] bundleIdentifier]
};
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsDictionary:user];

}];

}
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];
}
[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,
@@ -316,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] autorelease];
}

@end