iOS - 新聞資(zī)訊 - 雲南小程序開發|雲南軟件開發|雲南網站(zhàn)建設-西山區知普網絡科技工作室

159-8711-8523

雲南網建設/小程序開發/軟件開發

知識

不管是網站(zhàn),軟件還是小程序,都要直接或間接能為您産生價值,我們在追求其視覺表現的同時,更側重于功能的便捷,營銷的便利,運營的高效,讓網站(zhàn)成為營銷工具,讓軟件能切實提升企業(yè)内部管理水平和(hé)效率。優秀的程序為後期升級提供便捷的支持!

您當前位置>首頁 » 新聞資(zī)訊 » 技術(shù)分享 >

iOS

發表時間:2020-10-19

發布人:葵宇科技

浏覽次數:37


在xcode的工程琅绫擎打開game center,
ok,就這麼簡單。如(rú)今就大年夜概講講PlayerModel的道理。因為我們在提交的時刻往往會因為統因而掉敗,特别在中(zhōng)國。所以,PlayerModel琅绫擎就提交了一個(gè)機制,如(rú)不雅提交掉敗,就把要提交的數據保存到本地文(wén)件,在合适的時刻再測驗測驗提交。

比來又一次用到game center琅绫擎的leader board。其拭魅這個(gè)工作很簡單,隻是很輕易忘記。所以駒萃C鸫下(xià)來。
iTunes Connect上創建app,然後啟用game center
創建app就省略了,等創建成功後,不須要提交。我們就可(kě)以設置game center了。

起首點擊新建的app,找到Game Center,如(rú)圖
[img]http://img.blog.csdn.net/20150106162538594?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvemo1MTA=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center
header file:
點擊進入具體的game center設置,可(kě)以添加一些項目。很是簡單,根本上都有提示,須要留意的是排行榜id,得搞個(gè)自力的,不要反複。這個(gè)id在代率攀琅绫擎須要應用。
就這麼簡單的搞幾下(xià),game center就啟用了。
[img]http://img.blog.csdn.net/20150106162639590?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvemo1MTA=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center
在代碼中(zhōng)惹人game center
[img]http://img.blog.csdn.net/20150106162957820?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvemo1MTA=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center
膳绫擎這段代碼的意思就是app起來後,authenticationChanged被調用了,如(rú)不雅是登錄的狀況,那麼就會創建一個(gè)PlayerModel對象。如(rú)不雅有須要上傳的數據,那麼就攫取并且測驗測驗上傳。
直接打開就行,感到ios開辟越來越傻瓜了,呵呵。
接下(xià)來就是具體的代碼實現了。
代碼實現
起首在合适的處所添加如(rú)下(xià)代碼:平日是
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
double ver = [[UIDevice currentDevice].systemVersion doubleValue];
    if (ver < 6.0) {
        [[GKLocalPlayer localPlayer] authenticateWithCompletionHandler:^(NSError *error) {
            
        }];
    }
    else
    {
        [[GKLocalPlayer localPlayer] setAuthenticateHandler:(^(UIViewController* viewcontroller, NSError *error) {
            
        })];
    }
    
    NSNotificationCenter* ns = [NSNotificationCenter defaultCenter];
    
    [ns addObserver:self selector:@selector(authenticationChanged) name:GKPlayerAuthenticationDidChangeNotificationName object:nil];

    
我們給game center增長了一個(gè)不雅察者,所以就須要在self琅绫擎供給一個(gè)函數。這是一個(gè)回調函數,如(rú)不雅用戶沒有登錄game center,那麼就會跑到下(xià)面,如(rú)不雅上岸了就會跑到膳绫擎。
- (void) authenticationChanged
{
    if ([GKLocalPlayer localPlayer].isAuthenticated) {
        NSLog(@"authenticationChanged, authenticated");
        
        
        
    }
    else
    {
        
        NSLog(@"authenticationChanged, Not authenticated");
    }
}

接下(xià)來就是提交和(hé)顯示leader board了。具體的解釋蘋不雅的官網上說的很清跋扈。我這裡在網上找到一個(gè)封裝的源代碼,本身稍微修改了一下(xià)。具體代碼看最後面(附)。這裡重要介紹應用流程。先增長一個(gè)屬性。
@property (readwrite, retain) PlayerModel * player;
再增長一個(gè)函數,如(rú):
- (void) updatePlayer
{
    
    if (!self.player || ![self.player.currentPlayerID isEqualToString:[GKLocalPlayer localPlayer].playerID]) {
        
        [self.player release];
        
        self.player = [[PlayerModel alloc] init];
    }
    [[self player] loadStoredScores];
}
這個(gè)函數會在authenticationChanged琅绫擎被調到。
- (void) authenticationChanged
{
    if ([GKLocalPlayer localPlayer].isAuthenticated) {
        NSLog(@"authenticationChanged, authenticated");
        
        [self updatePlayer];
    }
    else
    {
        NSLog(@"authenticationChanged, Not authenticated");
    }
}
updatePlayer這個(gè)函數比較關(guān)鍵。
它支撐多用戶,如(rú)不雅是第一次上岸game center,那麼就創建一個(gè)對象,如(rú)不雅是換了個(gè)用戶登錄,那麼就把之前鄧晔着,然後創建一個(gè)新的對象。然後調用loadStoredScore.
loadStoredScore會大年夜本地文(wén)件琅绫擎攫取須要傳送的分數,并且往game center辦事器(qì)傳。
其拭魅這是個(gè)保護辦法,後面會講到為什麼須要這麼做。
接下(xià)來就看看如(rú)不雅在遊戲中(zhōng)即時上傳數據。
起首增長一個(gè)函數,這個(gè)函數就是往辦事器(qì)發送數據。self.player submitScore,這個(gè)函數會在後面看到。有了這個(gè)函數,我們在遊戲或者應用的某個(gè)處所可(kě)聲調用往辦事器(qì)發送數據了。LEADERBOARD_DISTANCE的值就是膳绫擎connect琅绫擎創建的那個(gè)排行榜id。
- (void) storeScore:(NSNumber *)distance
{
    if (!self.player)
        return;
    
    int64_t score64 =  [distance longLongValue];
    GKScore * submitScore = [[GKScore alloc] initWithCategory:LEADERBOARD_DISTANCE];
    [submitScore setValue:score64];
    [self.player submitScore:submitScore];
    [submitScore release];
}

- (void)submitScore:(GKScore *)score 
{
    if ([GKLocalPlayer localPlayer].authenticated) {
        if (!score.value) {
            // Unable to validate data. 
            return;
        }
        
        // Store the scores if there is an error. 
        [score reportScoreWithCompletionHandler:^(NSError *error){
            if (!error || (![error code] && ![error domain])) {
                // Score submitted correctly. Resubmit others
                [self resubmitStoredScores];
            } else {
                // Store score for next authentication. 
                [self storeScore:score];
            }
        }];
    } 
}
這個(gè)函數的重要意思就是,先測驗測驗提交數據,如(rú)不雅成功,那麼随便提交一下(xià)其他的數據(可(kě)能之前提交掉敗了)。如(rú)不雅掉敗,那麼就把數據保存下(xià)來[self storeScore: score],保存到一個(gè)array,并且寫入本地文(wén)件。如(rú)許就有機會在其他處所再提交一次。完全代碼看後面。
如(rú)今就看看如(rú)不雅在app琅绫擎顯示leader board。看下(xià)面的代碼gameCenterAuthenticationComplete是我内部應用的一個(gè)bool,用來标記用戶是否登錄了game center。調用一下(xià)這個(gè)代碼,就會顯示iOS的game center。
- (void) showGameCenter
{
    if (gameCenterAuthenticationComplete) {
        GKLeaderboardViewController * leaderboardViewController = [[GKLeaderboardViewController alloc] init];
        [leaderboardViewController setCategory:LEADERBOARD_DISTANCE];
        [leaderboardViewController setLeaderboardDelegate:_viewController];
        [self.viewController presentModalViewController:leaderboardViewController  animated:YES];
        [leaderboardViewController release];
    }
}

附,完全PlayerModle代碼:
#import <Foundation/Foundation.h>
#import <GameKit/GameKit.h>

@interface PlayerModel : NSObject {
	NSLock *writeLock;
}

@property (readonly, nonatomic) NSString* currentPlayerID;
@property (readonly, nonatomic) NSString *storedScoresFilename;
@property (readonly, nonatomic) NSMutableArray * storedScores;

// Store score for submission at a later time.
- (void)storeScore:(GKScore *)score ;

// Submit stored scores and remove from stored scores array.
- (void)resubmitStoredScores;

// Save store on disk. 
- (void)writeStoredScore;

// Load stored scores from disk.
- (void)loadStoredScores;

// Try to submit score, store on failure.
- (void)submitScore:(GKScore *)score ;

@end

m file:
#import "PlayerModel.h"

@implementation PlayerModel

@synthesize storedScores,
            currentPlayerID,
            storedScoresFilename;

- (id)init
{
    self = [super init];
    if (self) {
        currentPlayerID = [[NSString stringWithFormat:@"%@", [GKLocalPlayer localPlayer].playerID] retain];
        NSString* path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
        storedScoresFilename = [[NSString alloc] initWithFormat:@"%@/%@.storedScores.plist",path, currentPlayerID];
        writeLock = [[NSLock alloc] init];
    }
    return self;
}

- (void)dealloc
{
    [storedScores release];
    [writeLock release];
    [storedScoresFilename release];
    [currentPlayerID release];
    
    [super dealloc];
}

// Attempt to resubmit the scores.
- (void)resubmitStoredScores
{
    if (storedScores) {
        // Keeping an index prevents new entries to be added when the network is down 
        int index = (int)[storedScores count] - 1;
        while( index >= 0 ) {
            GKScore * score = [storedScores objectAtIndex:index];
            [self submitScore:score];
            [storedScores removeObjectAtIndex:index];
            index--;
        }
        [self writeStoredScore];
    }
}

// Load stored scores from disk.
- (void)loadStoredScores
{
    NSArray *  unarchivedObj = [NSKeyedUnarchiver unarchiveObjectWithFile:storedScoresFilename];
    
    if (unarchivedObj) {
        storedScores = [[NSMutableArray alloc] initWithArray:unarchivedObj];
        [self resubmitStoredScores];
    } else {
        storedScores = [[NSMutableArray alloc] init];
    }
}


// Save stored scores to file. 
- (void)writeStoredScore
{
    [writeLock lock];
    NSData * archivedScore = [NSKeyedArchiver archivedDataWithRootObject:storedScores];
    NSError * error;
    [archivedScore writeToFile:storedScoresFilename options:NSDataWritingFileProtectionNone error:&error];
    if (error) {
        //  Error saving file, handle accordingly 
    }
    [writeLock unlock];
}

// Store score for submission at a later time.
- (void)storeScore:(GKScore *)score 
{
    [storedScores addObject:score];
    [self writeStoredScore];
}

// Attempt to submit a score. On an error store it for a later time.
- (void)submitScore:(GKScore *)score 
{
    if ([GKLocalPlayer localPlayer].authenticated) {
        if (!score.value) {
            // Unable to validate data. 
            return;
        }
        
        // Store the scores if there is an error. 
        [score reportScoreWithCompletionHandler:^(NSError *error){
            if (!error || (![error code] && ![error domain])) {
                // Score submitted correctly. Resubmit others
                [self resubmitStoredScores];
            } else {
                // Store score for next authentication. 
                [self storeScore:score];
            }
        }];
    } 
}

@end

相關(guān)案例查看更多