Created
May 15, 2015 15:44
-
-
Save kneeko/8b2ce4e6d3b62b6eee8d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import <GameKit/GameKit.h> | |
#import "GameCenterManager.h" | |
@interface GameKitHelper : NSObject <GameCenterManagerDelegate> | |
@property (nonatomic, readonly) UIViewController *window; | |
@property (nonatomic, readonly) UIViewController *topViewController; | |
@property (nonatomic, readonly) UIViewController *authenticationViewController; | |
+ (instancetype)sharedGameKitHelper; | |
- (void)showLeaderboard; | |
- (void)hideLeaderboard; | |
- (void)authenticatePlayer; | |
- (void)reportScore; | |
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import "GameKitHelper.h" | |
#import "GameCenterManager.h" | |
@implementation GameKitHelper | |
NSString *const PresentAuthenticationViewController = @"present_authentication_view_controller"; | |
UIWindow *window; | |
+ (instancetype)sharedGameKitHelper | |
{ | |
static GameKitHelper *sharedGameKitHelper; | |
static dispatch_once_t onceToken; | |
dispatch_once(&onceToken, ^{ | |
sharedGameKitHelper = [[GameKitHelper alloc] init]; | |
}); | |
return sharedGameKitHelper; | |
} | |
- (id)init | |
{ | |
self = [super init]; | |
if (self) { | |
window = [[UIApplication sharedApplication] keyWindow]; | |
[[GameCenterManager sharedManager] setupManagerAndSetShouldCryptWithKey:@"YOUR_KEY_HERE" ]; | |
[[GameCenterManager sharedManager] setDelegate:self]; | |
} | |
return self; | |
} | |
- (void) gameCenterManager:(GameCenterManager *)manager authenticateUser:(UIViewController *)gameCenterLoginController | |
{ | |
UIViewController *topController = window.rootViewController; | |
[topController presentViewController:gameCenterLoginController animated:YES completion:nil]; | |
} | |
- (void) gameCenterManager:(GameCenterManager *)manager availabilityChanged:(NSDictionary *)availabilityInformation | |
{ | |
//NSLog(@"%@", availabilityInformation); | |
} | |
- (void) showLeaderboard | |
{ | |
UIViewController *topController = window.rootViewController; | |
[[GameCenterManager sharedManager] presentLeaderboardsOnViewController:topController]; | |
} | |
- (void)leaderboardViewControllerDidFinish:(GKGameCenterViewController *)viewController | |
{ | |
UIViewController *topController = window.rootViewController; | |
[topController dismissViewControllerAnimated:YES completion: nil]; | |
} | |
- (void) hideLeaderboard | |
{ | |
UIViewController *topController = window.rootViewController; | |
[topController dismissViewControllerAnimated:YES completion: nil]; | |
} | |
- (void) reportScore: (int) score forLeaderboardID: (NSString*) identifier | |
{ | |
[[GameCenterManager sharedManager] saveAndReportScore:score leaderboard:identifier sortOrder:GameCenterSortOrderHighToLow]; | |
} | |
- (BOOL) isAvailable | |
{ | |
BOOL isAvailable = [[GameCenterManager sharedManager] checkGameCenterAvailability]; | |
return isAvailable; | |
} | |
- (int) getScore: (NSString*) identifier | |
{ | |
int highScore = [[GameCenterManager sharedManager] highScoreForLeaderboard:identifier]; | |
return highScore; | |
} | |
- (NSString*) getIdentifier | |
{ | |
NSString *playerID = [[GameCenterManager sharedManager] localPlayerId]; | |
return playerID; | |
} | |
- (void)gameCenterManager:(GameCenterManager *)manager error:(NSError *)error | |
{ | |
//NSLog(@"%@", error); | |
} | |
- (void)openAppStore | |
{ | |
NSString *url = @"itms-apps://itunes.apple.com/WebObjects/MZStore.woa"; | |
url = [NSString stringWithFormat:@"%@/wa/viewContentsUserReviews?", url]; | |
url = [NSString stringWithFormat:@"%@type=Purple+Software&id=", url]; | |
url = [NSString stringWithFormat:@"%@YOUR_APP_ID", url]; | |
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:url]]; | |
} | |
- (BOOL)isInternetAvailable { | |
BOOL isAvailable = [[GameCenterManager sharedManager] isInternetAvailable]; | |
return isAvailable; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment