iOS排行榜
代码文件:
- GameCenterController.h
- GameCenterController.m
- GameKitHelper.h
- GameKitHelper.m
//GameCenterController.h
#ifndef GameCenterController_h
#define GameCenterController_h
#endif /* GameCenterController_h */
@interface GameCenterController:NSObject
+(void)loginGameCenter;
+(void)uploadScore:(NSDictionary *)dict;
+(void)showLeaderboard:(NSDictionary *)dict;
//+(void)retrieveTopTenScores;
+(void)getScoreData:(NSDictionary *)dict;
+(int)getScore;
@end
//GameCenterController.m
#import
#import "GameCenterController.h"
#import "GameKitHelper.h"
@implementation GameCenterController
+(void)loginGameCenter{
[[GameKitHelper sharedGameKitHelper] authenticateLocalPlayer];
}
+(void) uploadScore:(NSDictionary *)dict {
NSString* rID = [dict objectForKey:@"id"];
int score = [[dict objectForKey:@"score"] intValue];
[[GameKitHelper sharedGameKitHelper] submitScore:(int64_t)score category:rID];
}
+(void)showLeaderboard:(NSDictionary *)dict {
NSString* rID = [dict objectForKey:@"id"];
[[GameKitHelper sharedGameKitHelper] showLeaderboard:rID];
}
+(void)retrieveTopTenScores{
[[GameKitHelper sharedGameKitHelper] retrieveTopTenScores];
}
+(void)getScoreData:(NSDictionary *)dict{
NSString* rID = [dict objectForKey:@"id"];
[[GameKitHelper sharedGameKitHelper] getScoreData:rID];
}
+(int)getScore{
return [[GameKitHelper sharedGameKitHelper] getScore];
}
@end
//GameKitHelper.h
#import
@interface GameKitHelper : NSObject
//处理错误
@property (nonatomic, readonly) NSError* lastError;
// 初始化
+ (id) sharedGameKitHelper;
// Player authentication, info
-(void) authenticateLocalPlayer;
-(void) setLastError:(NSError*)error;
//提交排行榜数据
-(void) submitScore:(int64_t)score category:(NSString*)category;
//-(void) uploadScore:(NSDictionary *)dict;
//显示排行榜
- (void)showLeaderboard:(NSString*)rID;
- (void)leaderboardViewControllerDidFinish:(GKLeaderboardViewController *)viewController;
//
//- (void) retrieveTopTenScores;
-(void)getScoreData:(NSString*)rID;
-(int)getScore;
@end
//GameKitHelper.m
#import "GameKitHelper.h"
@interface GameKitHelper ()
{
BOOL _gameCenterFeaturesEnabled;
UIViewController* currentModalViewController;
int score;
}
@end
@implementation GameKitHelper
//#pragma mark Singleton stuff
+(id) sharedGameKitHelper {
static GameKitHelper *sharedGameKitHelper;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
sharedGameKitHelper =
[[GameKitHelper alloc] init];
});
return sharedGameKitHelper;
}
//#pragma mark Player Authentication
-(void) authenticateLocalPlayer {
GKLocalPlayer* localPlayer = [GKLocalPlayer localPlayer];
localPlayer.authenticateHandler = ^(UIViewController *viewController, NSError *error) {
[self setLastError:error];
if (localPlayer.authenticated) {
_gameCenterFeaturesEnabled = YES;
} else if(viewController) {
[self presentViewController:viewController];
} else {
_gameCenterFeaturesEnabled = NO;
}
};
}
//#pragma mark Property setters
-(void) setLastError:(NSError*)error {
_lastError = [error copy];
if (_lastError) {
NSLog(@"GameCenter -- setLastError -- ERROR: %@", [[_lastError userInfo]
description]);
}
}
#pragma mark UIViewController stuff
-(UIViewController*) getRootViewController {
return [UIApplication
sharedApplication].keyWindow.rootViewController;
}
-(void)presentViewController:(UIViewController*)vc {
UIViewController* rootVC = [self getRootViewController];
[rootVC presentViewController:vc animated:YES
completion:nil];
}
// 这里两个参数 score是数据, category是ID,就是我们创建排行榜以后,不可更改的那个ID。
-(void) submitScore:(int64_t)score category:(NSString*)category {
// 检查是否在登录状态
if (!_gameCenterFeaturesEnabled) {
NSLog(@"GameCenter -- submitScore -- Player not authenticated");
return;
}
// 创建一个分数对象
GKScore* gkScore = [[GKScore alloc] initWithCategory:category];
// 设置分数对象的值
gkScore.value = score;
// 向GameCenter提交数据
[gkScore reportScoreWithCompletionHandler: ^(NSError* error) {
[self setLastError:error];
}];
}
//-(void) uploadScore:(NSDictionary *)dict {
// NSString* rID = [dict objectForKey:@"id"];
// int score = [[dict objectForKey:@"score"] intValue];
//
// [[GameKitHelper sharedGameKitHelper] submitScore:(int64_t)score category:rID];
//}
- (void) showLeaderboard:(NSString*)rID{
GKLeaderboardViewController *leaderboardController = [[GKLeaderboardViewController alloc] init];
if (leaderboardController != nil) {
[leaderboardController setCategory:rID];
leaderboardController.leaderboardDelegate = self;
UIWindow *window = [[UIApplication sharedApplication] keyWindow];
currentModalViewController = [[UIViewController alloc] init];
[window addSubview:currentModalViewController.view];
[currentModalViewController presentModalViewController:leaderboardController animated:YES];
}
}
//关闭排行榜回调
- (void)leaderboardViewControllerDidFinish:(GKLeaderboardViewController *)viewController{
if(currentModalViewController !=nil){
[currentModalViewController dismissModalViewControllerAnimated:NO];
// [currentModalViewController release];
[currentModalViewController.view removeFromSuperview];
currentModalViewController = nil;
}
}
//- (void) retrieveTopTenScores
//{
// GKLeaderboard *leaderboardRequest = [[GKLeaderboard alloc] init];
// //NSLog(@" value:%@",leaderboardRequest.localPlayerScore.value);
// if (leaderboardRequest != nil)
// {
// leaderboardRequest.playerScope = GKLeaderboardPlayerScopeGlobal;
// leaderboardRequest.timeScope = GKLeaderboardTimeScopeAllTime;
// leaderboardRequest.range = NSMakeRange(1,10);
// leaderboardRequest.category = @"20180709";
//
// //__block NSString *score;
// [leaderboardRequest loadScoresWithCompletionHandler: ^(NSArray *scores, NSError *error) {
// if (error != nil){
// // handle the error.
// NSLog(@"下载失败");
// }
// if (scores != nil){
// NSLog(@" value:%d",leaderboardRequest.localPlayerScore.value);
// //score = [NSString stringWithFormat:@"%lld", scoreInt];
// // process the score information.
// NSLog(@"下载成功....");
// NSArray *tempScore = [NSArray arrayWithArray:leaderboardRequest.scores];
// for (GKScore *obj in tempScore) {
// NSLog(@" playerID : %@",obj.playerID);
// NSLog(@" category : %@",obj.category);
// NSLog(@" date : %@",obj.date);
// NSLog(@" formattedValue : %@",obj.formattedValue);
// NSLog(@" value : %d",obj.value);
// NSLog(@" rank : %d",obj.rank);
// NSLog(@"**************************************");
// }
// }
// }];
// }
//}
- (void) getScoreData:(NSString*)rID{
GKLeaderboard *leaderboardRequest = [[GKLeaderboard alloc] init];
if (leaderboardRequest != nil)
{
leaderboardRequest.playerScope = GKLeaderboardPlayerScopeGlobal;
leaderboardRequest.timeScope = GKLeaderboardTimeScopeAllTime;
leaderboardRequest.range = NSMakeRange(1,10);
leaderboardRequest.category = rID;
[leaderboardRequest loadScoresWithCompletionHandler: ^(NSArray *scores, NSError *error) {
if (error != nil){
// handle the error.
NSLog(@"下载失败");
}
if (scores != nil){
score = (int)leaderboardRequest.localPlayerScore.value;
}
}];
}
}
-(int)getScore{
return score;
}
@end
AppStore后台设置取得排行榜ID