2012-11-18 83 views
0

我在使用iOS 6 SDK的應用程序中整合遊戲中心時遇到問題。 其實我使用蘋果的示例代碼,但它看起來像不完整:iOS 6:遊戲中心身份驗證

我曾嘗試這樣的代碼:

-(void) authenticateLocalPlayer { 

GKLocalPlayer* localPlayer = 
[GKLocalPlayer localPlayer]; 

localPlayer.authenticateHandler = 
^(UIViewController *loginVC, 
    NSError *error) { 

    [self setLastError:error]; 

    if ([GKLocalPlayer localPlayer].authenticated) 
    { 
     // authentication successful 
     [self enableGameCenterForPlayer:[GKLocalPlayer localPlayer]]; 
    } 
    else if (loginVC) 
    { 
     // player not logged in yet, present the vc 
     [self pauseGame]; 
     [self presentLoginVC:loginVC]; 
    } 
    else 
    { 
     // authentication failed, provide graceful fallback 
     [self disableGameCenter]; 
    } 
    }; 

}

但問題是,enableGameCenterForPlayerpauseGamepresentLoginVC, disableGameCenter不是實現方法,它返回:

Instance method '-enableGameCenterForPlayer:' not found (return type defaults to 'id')

我該如何解決這個問題?

由於

+0

或者有人可以從他的項目中發佈他的遊戲中心認證代碼嗎? (使用iOS 6) – user1833903

回答

0

我使用的方法[自presentLoginVC:VC]通過我UITabViewController或的UINavigationController所述的viewController因爲低於該塊不是在主線程上。

localPlayer.authenticateHandler = ^(UIViewController *loginVC, NSError *error) { 

當你在一個塊你應該確保不改變UI元素,因爲你真的不知道什麼時候會完成,否則,你會在你的應用程序。可能有很多方法可以做到這一點,但這是我的解決方案。

下面是我的UITabBarController「類別」 .m文件(方法增加了沒有子類)創建的方法presentLoginVC,只是它已經通過我的UITabBarController稱之爲「showGameCenterViewController」:

#import "UITabBarController+GameKitAdditions.h" 

    @implementation UITabBarController (GameKitAdditions) 

    -(void) showGameCenterViewController: (UIViewController *)VC { 
     [self presentViewController:VC animated:NO completion:nil]; 
    } 

    -(void)dismissGameCenterViewController:(UIViewController *)VC { 
     [self dismissViewControllerAnimated:YES completion:nil]; 
    } 
    @end 

至於其他功能:

-(void) enableGameCenterForPlayer:(GKLocalPlayer *) localPlayer; 
    -(void) disableGameCenter; 
    -(void) pauseGame; 

它們可能很簡單,只需將名爲enableGameCenter的BOOL設置爲YES或NO即可。爲了解決錯誤,你可以將這些原型添加到你的.h文件中,然後編寫函數來輸出NSLog()或其他東西。