2015-09-25 27 views
1

我創建了一個類NSObject的和我在它這個身份驗證功能:遊戲時未通過身份驗證玩家中心登錄表單精靈套件

-(void)authenticateLocalUser { 

if(!gameCenterAvailable) { return; } 

GKLocalPlayer *localPlayer = [GKLocalPlayer localPlayer]; 
__weak GKLocalPlayer *blockLocalPlayer = localPlayer; 

localPlayer.authenticateHandler = ^(UIViewController *viewController, NSError *error){ 
    NSLog(@"authenticateHandler"); 
    if (viewController != nil) 
    { 
     NSLog(@"viewController != nil"); 
    } 
    else if (blockLocalPlayer.isAuthenticated) 
    { 
     NSLog(@"localPlayer already authenticated"); 
     //do some stuff 
    } 
    else 
    { 
     NSLog(@"local player not authenticated"); 
     // not logged in 
    } 
}; 

}

它的新版本我擡起頭來,但其不顯示Game Center登錄表單。

如果你需要這個信息,我在我的AppDelegate.m中調用這個方法。

我想我必須把東西放在那裏,當玩家沒有登錄像獲取登錄表單。但我不知道如何。

感謝您的幫助!

+0

正在打印哪些你的NSLog語句? – Thunk

+0

好吧,如果im登錄到遊戲中心 - >玩家被認證,如果不是另一個。他們都正確顯示,我只是想如果玩家沒有通過驗證從遊戲中心 – Robin

回答

1

你需要兩件事。首先,您需要檢查error。如果已設置,viewController通常爲零,並且您的代碼將錯誤地認爲您已登錄並愉快地與Game Center進行通信。您可能會根據緩存的憑據出現登錄,但在大多數情況下,您實際上不會看到服務器中的當前數據,只能訪問上次在設備上緩存的任何數據。

其次,你必須出示的viewController給用戶,如果他們沒有登錄試試這個:

localPlayer.authenticateHandler = ^(UIViewController *viewController, NSError *error) 
{ 
    NSLog(@"authenticateHandler"); 

    if (error) 
    { 
     //Examine the error. IOS may show you as "authenticated" based on 
     //cached credentials, but you probably can't really communicate with 
     //game center in any meaningful manner 
    } 
    else if (viewController != nil) 
    { 
     //You're not logged in, show the login viewController 

     NSLog(@"viewController != nil"); 
     UIViewController *mainVC = [[[[UIApplication sharedApplication]delegate] window] rootViewController]; 
     [mainVC presentViewController:viewController animated:NO completion:^ 
     { 
       //the login view controller has been dismissed. Did it work? 
       if ([GKLocalPlayer localPlayer].authenticated) 
       { 
        //Yes, Login worked. enable whatever comes next after successful login 
       } 
       else 
       { 
        //No, login failed or was canceled. 
       } 
     }]; 
    } 
    else if (blockLocalPlayer.isAuthenticated) 
    { 
     NSLog(@"localPlayer already authenticated"); 
     //do some stuff 
    } 
    else 
    { 
     NSLog(@"local player not authenticated"); 
     // not logged in 
    } 
}; 
+0

作品提交登錄表單,非常感謝! – Robin