2013-04-10 97 views
1

我是iOS新手,嘗試編寫一款有趣的基於回合的iOS遊戲。我試圖現在驗證本地用戶,並且在構建代碼時(儘管存在保留週期警告),但它始終無法通過GameCenter進行身份驗證。setAuthenticateHandler未能通過遊戲中心進行身份驗證

// Authenticate the local user. 
- (void)authenticateLocalUser 
{ 
if (!gameCenterAvailable) return; 

GKLocalPlayer *localPlayer = [GKLocalPlayer localPlayer]; 
[localPlayer setAuthenticateHandler:(^(UIViewController* viewcontroller, NSError *error) 
{ 
    //[localPlayer authenticateWithCompletionHandler:^(NSError *error) { OLD CODE! 
    if (localPlayer.isAuthenticated) 
    { 
     // Do some stuff. 
    } 
    else 
    { 
     UIAlertView *alertView = [[UIAlertView alloc] 
            initWithTitle:@"NOT AUTHORISED" 
            message:@"YOUR'RE NOT LOGGED INTO GC." 
            delegate:self 
            cancelButtonTitle:@"OK" 
            otherButtonTitles:nil]; 
     [alertView show]; 
    } 
} 
)]; 

}

我的舊代碼仍然有效,但在貶值iOS6的:

NSLog(@"Authenticating local user..."); 
if ([GKLocalPlayer localPlayer].authenticated == NO) 
{ 
    [[GKLocalPlayer localPlayer] 
    authenticateWithCompletionHandler:nil]; 
} 
else 
{ 
    NSLog(@"Already authenticated!"); 
} 

有什麼建議?

+0

可能的重複http://stackoverflow.com/questions/13511030/login-in-iphone-app-via-gamekit – lostInTransit 2013-04-10 05:27:53

+0

這不是一回事,可悲的是。 – 2013-04-10 05:40:51

回答

2

該處理程序在播放器未通過身份驗證時傳遞UIViewController。如果接收到視圖控制器,則顯示視圖控制器是您的責任。

localPlayer.authenticateHandler = ^(UIViewController *viewController, NSError *error){ 
    if (viewController != nil) { 
     [self presentViewController:viewController animated:YES completion:nil]; 
    } else if (instance.player.isAuthenticated) { 
     //Your handler will be called a second time once the user authenticates GC 
     //using the view controller above ^^^^^ 
    } else if (error != nil) { 
     //If all else fails, you'll have an error. Handle it 
    } 
}; 
0

我發現localPlayer,設置外/沒有經過認證結束之前,而如果我檢索一個新GKLocalPlayer.localPlayer()對象,它會被認證。

func authenticatePlayer() { 
    local_player.authenticateHandler = {(view_controller, error) -> Void in 

    // Note that self.local_player.authenticated and 
    // GKLocalPlayer.localPlayer().authenticated give different 
    // results 

    self.local_player = GKLocalPlayer.localPlayer() 
    if (self.local_player.authenticated) { 
     // Successfully authenticated 
    } else if (view_controller != nil) { 
     // Not yet authenticated 
    } else { 
     // authentication failed 
    } 
    } 
} 

在這種情況下,local_player在周圍設置類。

注意:我不確定爲什麼這些給出不同的結果。

相關問題