iOS GameCenter排行榜

iOS排行榜

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 <Foundation/Foundation.h>
#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 <GameKit/GameKit.h>

@interface GameKitHelper : NSObject
//處理錯誤
@property (nonatomic, readonly) NSError* lastError;

// 初始化
+ (id) sharedGameKitHelper;

// Player authentication, info
-(void) authenticateLocalPlayer;
-(void) setLastError:(NSError*)error;

//提交排行榜數(shù)據(jù)
-(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 ()
<GKGameCenterControllerDelegate> {
    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];
}

// 這里兩個參數(shù) score是數(shù)據(jù), category是ID,就是我們創(chuàng)建排行榜以后,不可更改的那個ID。
-(void) submitScore:(int64_t)score category:(NSString*)category {
    // 檢查是否在登錄狀態(tài)
    if (!_gameCenterFeaturesEnabled)    {
        NSLog(@"GameCenter -- submitScore -- Player not authenticated");
        return;
    }
    
    // 創(chuàng)建一個分數(shù)對象
    GKScore* gkScore = [[GKScore alloc] initWithCategory:category];
    
    // 設置分數(shù)對象的值
    gkScore.value = score;
    
    // 向GameCenter提交數(shù)據(jù)
    [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];
    }
}

//關閉排行榜回調(diào)
- (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


iOS排行榜后臺設置
?著作權歸作者所有,轉載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內(nèi)容

  • 發(fā)現(xiàn) 關注 消息 iOS 第三方庫、插件、知名博客總結 作者大灰狼的小綿羊哥哥關注 2017.06.26 09:4...
    肇東周閱讀 12,198評論 4 61
  • 給我回饋作為安慰吧 給我擁抱讓我遠行吧
    彧愔從閱讀 272評論 0 0
  • 米拉分手了,分手的原因是,男朋友的媽媽覺得她個子矮。 米拉的男朋友一把眼淚一把鼻涕的跟米拉說:“我是真的愛你,可我...
    柒月記者閱讀 250評論 0 1
  • 前行至一山頭,手機信號在此完結。放眼望去,但見此后之山換了一副模樣,不再是一塊巨石一座山,代之以斗大石塊堆砌構連之...
    dd7901360167閱讀 459評論 0 0
  • 常憶家鄉(xiāng)日暮,霞飛馬良山麓。 吆牛喚伴回還,炊煙裊裊歸處。 身披金色余暉,嬉笑打鬧趕路。 未到村頭門口,饑腸早已轆...
    西江月兒閱讀 211評論 0 1