gpt4 book ai didi

IOS 游戏中心 : How to know when default sign in form for game center is finished?

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:15:40 31 4
gpt4 key购买 nike

我想知道如何知道用户何时完成 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];
}

最佳答案

这里引用自 authenticateHandler 文档

The authenticate handler will be called whenever the authentication process finishes or needs to show UI. The handler may be called multiple times. Authentication will happen automatically when the handler is first set and whenever the app returns to the foreground.
If the authentication process needs to display UI the viewController property will be non-nil. Your application should present this view controller and continue to wait for another call of the authenticateHandler. The view controller will be dismissed automatically.

Possible reasons for error:
1. Communications problem
2. User credentials invalid
3. User cancelled

所以这个block会被多次调用,例如,如果用户没有登录,一个 View Controller 将被传递到这个 block ,在你呈现它并且用户提交表单之后,他的 block 将再次执行。
这是处理身份验证的代码:

- (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
}
};
}

关于IOS 游戏中心 : How to know when default sign in form for game center is finished?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30129062/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com