forked from sgonzalez/GameCenterManager
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGameCenterManager.m
127 lines (103 loc) · 4.54 KB
/
GameCenterManager.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
//
// GameCenterManager.m
//
// Copyright 2011 Hicaduda. All rights reserved.
//
/*
hicaduda.com || http://github.com/sgonzalez/GameCenterManager
Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
files (the "Software"), to deal in the Software without
restriction, including without limitation the rights to use,
copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software in binary form, and to permit persons to whom the
Software is furnished to do so, subject to the following
conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
*/
#import "GameCenterManager.h"
#import "SynthesizeSingleton.h"
@implementation GameCenterManager
@synthesize gcSuccess;
SYNTHESIZE_SINGLETON_FOR_CLASS(GameCenterManager)
#pragma mark -
- (void)achievementViewControllerDidFinish:(GKAchievementViewController *)viewController {
[viewController dismissModalViewControllerAnimated:YES];
}
- (void)leaderboardViewControllerDidFinish:(GKLeaderboardViewController *)viewController {
[viewController dismissModalViewControllerAnimated:YES];
}
- (void)showLeaderboardsFromViewController:(UIViewController *)viewController {
GKLeaderboardViewController *leaderboardController = [[GKLeaderboardViewController alloc] init];
if (leaderboardController != nil) {
leaderboardController.leaderboardDelegate = self;
[viewController presentModalViewController: leaderboardController animated: YES];
}
[leaderboardController release];
}
- (void)showAchievementsFromViewController:(UIViewController *)viewController {
GKAchievementViewController *achievementvc = [[GKAchievementViewController alloc] init];
if (achievementvc != nil) {
achievementvc.achievementDelegate = self;
[viewController presentModalViewController:achievementvc animated:YES];
}
}
#pragma mark -
- (void)authenticateLocalPlayer {
[[GKLocalPlayer localPlayer] authenticateWithCompletionHandler:^(NSError *error) {
if (error == nil) {
// Insert code here to handle a successful authentication.
gcSuccess = YES;
}
else
{
// Your application can process the error parameter to report the error to the player.
/*UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error" message:@"Could not connect with Game Center servers." delegate:nil cancelButtonTitle:@"Try Later" otherButtonTitles:nil];
[alert show];
[alert release];*/
}
}];
}
- (void)reportScore:(int64_t)score forCategory:(NSString*)category {
if (!gcSuccess) return;
GKScore *scoreReporter = [[[GKScore alloc] initWithCategory:category] autorelease];
scoreReporter.value = score;
[scoreReporter reportScoreWithCompletionHandler:^(NSError *error) {
if (error != nil) {
// handle the reporting error
/*UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error" message:@"Could not submit score with Game Center." delegate:nil cancelButtonTitle:@"Try Later" otherButtonTitles:nil];
[alert show];
[alert release];*/
}
}];
}
- (void)reportAchievementIdentifier:(NSString*)identifier percentComplete:(float)percent {
[self reportAchievementIdentifier:identifier percentComplete:percent withBanner:NO];
}
- (void)reportAchievementIdentifier:(NSString*)identifier percentComplete:(float)percent withBanner:(BOOL)banner {
if (!gcSuccess) return;
GKAchievement *achievement = [[[GKAchievement alloc] initWithIdentifier: identifier] autorelease];
if (achievement) {
achievement.percentComplete = percent;
[achievement setShowsCompletionBanner:banner];
[achievement reportAchievementWithCompletionHandler:^(NSError *error)
{
if (error != nil) {
// Retain the achievement object and try again later (not shown).
/*UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error" message:@"Could not submit achievement with Game Center." delegate:nil cancelButtonTitle:@"Try Later" otherButtonTitles:nil];
[alert show];
[alert release];*/
}
}];
}
}
@end