我想知道如何知道用户何时完成Game Center登录表单。我正在自动登录Facebook,但我需要等待Game Center登录才能完成。有没有办法知道这个?
-(void) authenticateLocalPlayer {
GKLocalPlayer* localPlayer = [GKLocalPlayer localPlayer];
localPlayer.authenticateHandler =
^(UIViewController *viewController,
NSError *error) {
[self setLastError:error];
if (localPlayer.authenticated) {
_gameCenterFeaturesEnabled = YES;
NSLog(@"local Player Info: %@",localPlayer);
[[UserManager sharedInstance] setGameCenterId:localPlayer.playerID];
[[UserManager sharedInstance] setUserName:localPlayer.alias];
[self retrieveFriends];
} else if(viewController) {
[self presentViewController:viewController];
} else {
_gameCenterFeaturesEnabled = NO;
}
};
}
-(void) setLastError:(NSError*)error {
_lastError = [error copy];
if (_lastError) {
NSLog(@"GameKitHelper ERROR: %@", [[_lastError userInfo]
description]);
}
}
-(UIViewController*) getRootViewController {
return [UIApplication
sharedApplication].keyWindow.rootViewController;
}
-(void)presentViewController:(UIViewController*)vc {
UIViewController* rootVC = [self getRootViewController];
[rootVC presentViewController:vc animated:YES completion:nil];
}
答案 0 :(得分:1)
以下是authenticateHandler
文档
只要进行身份验证,就会调用身份验证处理程序 流程完成或需要显示UI。可以调用处理程序 多次。身份验证将自动发生 处理程序首先设置,每当应用程序返回前台时 如果验证过程需要显示UI的viewController 财产将是非零的。您的应用程序应提供此视图 控制器并继续等待另一个电话 authenticateHandler。视图控制器将被关闭 自动。
错误的可能原因:
1.通讯问题
2.用户凭证无效
3.用户取消了
所以块会被多次调用, 例如,如果用户未登录,则视图控制器将被传递到此块,在您呈现它并且用户提交表单后,将再次执行该块。 以下是处理身份验证的代码:
- (void)authenticateLocalPlayer
{
GKLocalPlayer *localPlayer = [GKLocalPlayer localPlayer];
__weak typeof(localPlayer) weakLocalPlayer = localPlayer;
localPlayer.authenticateHandler =
^(UIViewController *viewController, NSError *error)
{
if (error) {
// Something happened, handle it...
return;
}
__strong typeof(weakLocalPlayer) strongLocalPlayer = weakLocalPlayer;
if (viewController) {
// Just show it, user needs to submit the form
return;
}
if (strongLocalPlayer.isAuthenticated) {
// User completed login, do FB login
} else {
// GameKit is disabled, show guide to enable it
}
};
}