欧美三级国产三级日韩三级_亚洲熟妇丰满大屁股熟妇_欧美亚洲成人一区二区三区_国产精品久久久久久模特

iOS - 新聞資訊 - 云南小程序開發(fā)|云南軟件開發(fā)|云南網(wǎng)站建設(shè)-昆明葵宇信息科技有限公司

159-8711-8523

云南網(wǎng)建設(shè)/小程序開發(fā)/軟件開發(fā)

知識

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

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

iOS

發(fā)表時間:2020-10-19

發(fā)布人:葵宇科技

瀏覽次數(shù):58


在xcode的工程瑯綾擎打開game center,
ok,就這么簡單。如今就大年夜概講講PlayerModel的道理。因為我們在提交的時刻往往會因為統(tǒng)因而掉敗,特別在中國。所以,PlayerModel瑯綾擎就提交了一個機制,如不雅提交掉敗,就把要提交的數(shù)據(jù)保存到本地文件,在合適的時刻再測驗測驗提交。

比來又一次用到game center瑯綾擎的leader board。其拭魅這個工作很簡單,只是很輕易忘記。所以駒萃C鶇下來。
iTunes Connect上創(chuàng)建app,然后啟用game center
創(chuàng)建app就省略了,等創(chuàng)建成功后,不須要提交。我們就可以設(shè)置game center了。

起首點擊新建的app,找到Game Center,如圖
[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設(shè)置,可以添加一些項目。很是簡單,根本上都有提示,須要留意的是排行榜id,得搞個自力的,不要反復(fù)。這個id在代率攀瑯綾擎須要應(yīng)用。
就這么簡單的搞幾下,game center就啟用了。
[img]http://img.blog.csdn.net/20150106162639590?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvemo1MTA=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center
在代碼中惹人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被調(diào)用了,如不雅是登錄的狀況,那么就會創(chuàng)建一個PlayerModel對象。如不雅有須要上傳的數(shù)據(jù),那么就攫取并且測驗測驗上傳。
直接打開就行,感到ios開辟越來越傻瓜了,呵呵。
接下來就是具體的代碼實現(xiàn)了。
代碼實現(xiàn)
起首在合適的處所添加如下代碼:平日是
- (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增長了一個不雅察者,所以就須要在self瑯綾擎供給一個函數(shù)。這是一個回調(diào)函數(shù),如不雅用戶沒有登錄game center,那么就會跑到下面,如不雅上岸了就會跑到膳綾擎。
- (void) authenticationChanged
{
    if ([GKLocalPlayer localPlayer].isAuthenticated) {
        NSLog(@"authenticationChanged, authenticated");
        
        
        
    }
    else
    {
        
        NSLog(@"authenticationChanged, Not authenticated");
    }
}

接下來就是提交和顯示leader board了。具體的解釋蘋不雅的官網(wǎng)上說的很清跋扈。我這里在網(wǎng)上找到一個封裝的源代碼,本身稍微修改了一下。具體代碼看最后面(附)。這里重要介紹應(yīng)用流程。先增長一個屬性。
@property (readwrite, retain) PlayerModel * player;
再增長一個函數(shù),如:
- (void) updatePlayer
{
    
    if (!self.player || ![self.player.currentPlayerID isEqualToString:[GKLocalPlayer localPlayer].playerID]) {
        
        [self.player release];
        
        self.player = [[PlayerModel alloc] init];
    }
    [[self player] loadStoredScores];
}
這個函數(shù)會在authenticationChanged瑯綾擎被調(diào)到。
- (void) authenticationChanged
{
    if ([GKLocalPlayer localPlayer].isAuthenticated) {
        NSLog(@"authenticationChanged, authenticated");
        
        [self updatePlayer];
    }
    else
    {
        NSLog(@"authenticationChanged, Not authenticated");
    }
}
updatePlayer這個函數(shù)比較關(guān)鍵。
它支撐多用戶,如不雅是第一次上岸game center,那么就創(chuàng)建一個對象,如不雅是換了個用戶登錄,那么就把之前鄧曄著,然后創(chuàng)建一個新的對象。然后調(diào)用loadStoredScore.
loadStoredScore會大年夜本地文件瑯綾擎攫取須要傳送的分數(shù),并且往game center辦事器傳。
其拭魅這是個保護辦法,后面會講到為什么須要這么做。
接下來就看看如不雅在游戲中即時上傳數(shù)據(jù)。
起首增長一個函數(shù),這個函數(shù)就是往辦事器發(fā)送數(shù)據(jù)。self.player submitScore,這個函數(shù)會在后面看到。有了這個函數(shù),我們在游戲或者應(yīng)用的某個處所可聲調(diào)用往辦事器發(fā)送數(shù)據(jù)了。LEADERBOARD_DISTANCE的值就是膳綾擎connect瑯綾擎創(chuàng)建的那個排行榜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];
            }
        }];
    } 
}
這個函數(shù)的重要意思就是,先測驗測驗提交數(shù)據(jù),如不雅成功,那么隨便提交一下其他的數(shù)據(jù)(可能之前提交掉敗了)。如不雅掉敗,那么就把數(shù)據(jù)保存下來[self storeScore: score],保存到一個array,并且寫入本地文件。如許就有機會在其他處所再提交一次。完全代碼看后面。
如今就看看如不雅在app瑯綾擎顯示leader board??聪旅娴拇agameCenterAuthenticationComplete是我內(nèi)部應(yīng)用的一個bool,用來標記用戶是否登錄了game center。調(diào)用一下這個代碼,就會顯示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)案例查看更多