-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathViewController.m
executable file
·112 lines (89 loc) · 2.96 KB
/
ViewController.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
//
// ViewController.m
// ViewControllerTest
//
#import <UIKit/UIDevice.h>
#import <GameKit/Gamekit.h>
#import "ViewController.h"
#import "GameController.h"
@implementation ViewController
- (id)initWithSparrowView:(SPView *)sparrowView
{
if ((self = [super init]))
{
loggedin = NO;
mSparrowView = sparrowView;
NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
[nc addObserver:self selector:@selector(onApplicationDidBecomeActive:)
name:UIApplicationDidBecomeActiveNotification object:nil];
[nc addObserver:self selector:@selector(onApplicationWillResignActive:)
name:UIApplicationWillResignActiveNotification object:nil];
//Sign into game center
localPlayer = [GKLocalPlayer localPlayer];
[localPlayer authenticateWithCompletionHandler:^(NSError *error) {
if (localPlayer.isAuthenticated)
{
// Player was successfully authenticated.
// Perform additional tasks for the authenticated player.
loggedin = YES;
}
else {
NSLog(@"error: %@ a.H", [error userInfo]);
}
}];
}
return self;
}
- (void) showLeaderboard
{
GKLeaderboardViewController *leaderboardController = [[GKLeaderboardViewController alloc] init];
if (leaderboardController != NULL)
{
leaderboardController.category = @"mainLeaderboard";
leaderboardController.timeScope = GKLeaderboardTimeScopeToday;
leaderboardController.leaderboardDelegate = (id)self;
[self presentModalViewController: leaderboardController animated: YES];
}
}
- (void)leaderboardViewControllerDidFinish:(GKLeaderboardViewController *)viewController
{ // May need to chagne slightly (see apple docs for iOS 6), specifically the params
[self dismissModalViewControllerAnimated:YES];
}
-(void) submitScore: (int) score {
if (loggedin) {
GKScore *scoreReporter = [[GKScore alloc] initWithCategory:@"mainLeaderboard"];
scoreReporter.value = score;
scoreReporter.context = 0;
[scoreReporter reportScoreWithCompletionHandler:^(NSError *error) {
// Do something interesting here.
}];
}
}
- (id)init
{
[NSException raise:SP_EXC_INVALID_OPERATION format:@"ViewController requires Sparrow View"];
return nil;
}
- (void)didReceiveMemoryWarning
{
[SPPoint purgePool];
[SPRectangle purgePool];
[SPMatrix purgePool];
[super didReceiveMemoryWarning];
}
#pragma mark - view lifecycle
- (void)loadView
{
CGRect screenBounds = [UIScreen mainScreen].bounds;
self.view = [[SPOverlayView alloc] initWithFrame:screenBounds];
}
#pragma mark - notifications
- (void)onApplicationDidBecomeActive:(NSNotification *)notification
{
[mSparrowView start];
}
- (void)onApplicationWillResignActive:(NSNotification *)notification
{
[mSparrowView stop];
}
@end