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
//處理錯(cuò)誤
@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];
}

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

//關(guān)閉排行榜回調(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后臺(tái)設(shè)置取得排行榜ID


iOS排行榜后臺(tái)設(shè)置
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末爆土,一起剝皮案震驚了整個(gè)濱河市箭跳,隨后出現(xiàn)的幾起案子聚磺,更是在濱河造成了極大的恐慌笋轨,老刑警劉巖,帶你破解...
    沈念sama閱讀 216,744評(píng)論 6 502
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件家浇,死亡現(xiàn)場(chǎng)離奇詭異本砰,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī)钢悲,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,505評(píng)論 3 392
  • 文/潘曉璐 我一進(jìn)店門点额,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人莺琳,你說我怎么就攤上這事还棱。” “怎么了惭等?”我有些...
    開封第一講書人閱讀 163,105評(píng)論 0 353
  • 文/不壞的土叔 我叫張陵珍手,是天一觀的道長。 經(jīng)常有香客問我辞做,道長琳要,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,242評(píng)論 1 292
  • 正文 為了忘掉前任凭豪,我火速辦了婚禮焙蹭,結(jié)果婚禮上晒杈,老公的妹妹穿的比我還像新娘嫂伞。我一直安慰自己,他們只是感情好拯钻,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,269評(píng)論 6 389
  • 文/花漫 我一把揭開白布帖努。 她就那樣靜靜地躺著,像睡著了一般粪般。 火紅的嫁衣襯著肌膚如雪拼余。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,215評(píng)論 1 299
  • 那天亩歹,我揣著相機(jī)與錄音匙监,去河邊找鬼。 笑死小作,一個(gè)胖子當(dāng)著我的面吹牛亭姥,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播顾稀,決...
    沈念sama閱讀 40,096評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼达罗,長吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼!你這毒婦竟也來了静秆?” 一聲冷哼從身側(cè)響起粮揉,我...
    開封第一講書人閱讀 38,939評(píng)論 0 274
  • 序言:老撾萬榮一對(duì)情侶失蹤巡李,失蹤者是張志新(化名)和其女友劉穎,沒想到半個(gè)月后扶认,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體侨拦,經(jīng)...
    沈念sama閱讀 45,354評(píng)論 1 311
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,573評(píng)論 2 333
  • 正文 我和宋清朗相戀三年辐宾,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了阳谍。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 39,745評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡螃概,死狀恐怖矫夯,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情吊洼,我是刑警寧澤训貌,帶...
    沈念sama閱讀 35,448評(píng)論 5 344
  • 正文 年R本政府宣布,位于F島的核電站冒窍,受9級(jí)特大地震影響递沪,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜综液,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,048評(píng)論 3 327
  • 文/蒙蒙 一款慨、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧谬莹,春花似錦檩奠、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,683評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至蕉扮,卻和暖如春整胃,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背喳钟。 一陣腳步聲響...
    開封第一講書人閱讀 32,838評(píng)論 1 269
  • 我被黑心中介騙來泰國打工屁使, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人奔则。 一個(gè)月前我還...
    沈念sama閱讀 47,776評(píng)論 2 369
  • 正文 我出身青樓蛮寂,卻偏偏與公主長得像,于是被迫代替她去往敵國和親应狱。 傳聞我的和親對(duì)象是個(gè)殘疾皇子共郭,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,652評(píng)論 2 354

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

  • 發(fā)現(xiàn) 關(guān)注 消息 iOS 第三方庫、插件、知名博客總結(jié) 作者大灰狼的小綿羊哥哥關(guān)注 2017.06.26 09:4...
    肇東周閱讀 12,096評(píng)論 4 62
  • 給我回饋?zhàn)鳛榘参堪?給我擁抱讓我遠(yuǎn)行吧
    彧愔從閱讀 202評(píng)論 0 0
  • 米拉分手了,分手的原因是尉咕,男朋友的媽媽覺得她個(gè)子矮叠蝇。 米拉的男朋友一把眼淚一把鼻涕的跟米拉說:“我是真的愛你,可我...
    柒月記者閱讀 235評(píng)論 0 1
  • 前行至一山頭,手機(jī)信號(hào)在此完結(jié)年缎。放眼望去,但見此后之山換了一副模樣,不再是一塊巨石一座山,代之以斗大石塊堆砌構(gòu)連之...
    dd7901360167閱讀 450評(píng)論 0 0
  • 常憶家鄉(xiāng)日暮悔捶,霞飛馬良山麓。 吆牛喚伴回還单芜,炊煙裊裊歸處蜕该。 身披金色余暉,嬉笑打鬧趕路洲鸠。 未到村頭門口堂淡,饑腸早已轆...
    西江月兒閱讀 202評(píng)論 0 1