-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathGameCenterPlugin.m
110 lines (96 loc) · 3.61 KB
/
GameCenterPlugin.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
//
// GameCenterPlugin.m
// Detonate
//
// Created by Marco Piccardo on 04/02/11.
// Copyright 2011 Eurotraining Engineering. All rights reserved.
//
#import "GameCenterPlugin.h"
#ifdef CORDOVA_FRAMEWORK
#import <Cordova/CDVViewController.h>
#else
#import "CDVViewController.h"
#endif
@implementation GameCenterPlugin
- (void)authenticateLocalPlayer:(NSMutableArray*)arguments withDict:(NSMutableDictionary*)options
{
[[GKLocalPlayer localPlayer] authenticateWithCompletionHandler:^(NSError *error) {
if (error == nil)
{
NSString* jsCallback = [NSString stringWithFormat:@"GameCenter._userDidLogin();",@""];
[self.webView stringByEvaluatingJavaScriptFromString:jsCallback];
}
else
{
NSString* jsCallback = [NSString stringWithFormat:@"GameCenter._userDidFailLogin();",@""];
[self.webView stringByEvaluatingJavaScriptFromString:jsCallback];
}
}];
}
- (void)reportScore:(NSMutableArray*)arguments withDict:(NSMutableDictionary*)options
{
NSString *category = (NSString*) [arguments objectAtIndex:0];
int64_t score = [[arguments objectAtIndex:1] integerValue];
GKScore *scoreReporter = [[[GKScore alloc] initWithCategory:category] autorelease];
scoreReporter.value = score;
[scoreReporter reportScoreWithCompletionHandler:^(NSError *error) {
if (error != nil)
{
NSString* jsCallback = [NSString stringWithFormat:@"GameCenter._userDidSubmitScore();",@""];
[self.webView stringByEvaluatingJavaScriptFromString:jsCallback];
} else {
NSString* jsCallback = [NSString stringWithFormat:@"GameCenter._userDidFailSubmitScore();",@""];
[self.webView stringByEvaluatingJavaScriptFromString:jsCallback];
}
}];
}
- (void)showLeaderboard:(NSMutableArray*)arguments withDict:(NSMutableDictionary*)options
{
GKLeaderboardViewController *leaderboardController = [[GKLeaderboardViewController alloc] init];
if (leaderboardController != nil)
{
leaderboardController.leaderboardDelegate = self;
leaderboardController.category = (NSString*) [arguments objectAtIndex:0];
CDVViewController* cont = (CDVViewController*)[super viewController];
[cont presentModalViewController: leaderboardController animated: YES];
}
}
- (void)showAchievements:(NSMutableArray*)arguments withDict:(NSMutableDictionary*)options
{
GKAchievementViewController *achievements = [[GKAchievementViewController alloc] init];
if (achievements != nil)
{
achievements.achievementDelegate = self;
CDVViewController* cont = (CDVViewController*)[super viewController];
[cont presentModalViewController: achievements animated: YES];
}
[achievements release];
}
- (void)leaderboardViewControllerDidFinish:(GKLeaderboardViewController *)viewController
{
CDVViewController* cont = (CDVViewController*)[super viewController];
[cont dismissModalViewControllerAnimated:YES];
}
- (void)achievementViewControllerDidFinish:(GKAchievementViewController *)viewController
{
CDVViewController* cont = (CDVViewController*)[super viewController];
[cont dismissModalViewControllerAnimated:YES];
}
- (void)reportAchievementIdentifier:(NSMutableArray*)arguments withDict:(NSMutableDictionary*)options
{
NSString *identifier = (NSString*) [arguments objectAtIndex:0];
float percent = [[arguments objectAtIndex:1] floatValue];
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).
}
}];
}
}
@end