OStack程序员社区-中国程序员成长平台

标题: IOS游戏中心: How to know when default sign in form for game center is finished? [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-13 07:04
标题: IOS游戏中心: How to know when default sign in form for game center is finished?

我想知道如何知道用户何时完成了 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) setLastErrorNSError*)error {
    _lastError = [error copy];
    if (_lastError) {
        NSLog(@"GameKitHelper ERROR: %@", [[_lastError userInfo]
                                           description]);
    }
}
-(UIViewController*) getRootViewController {
    return [UIApplication
            sharedApplication].keyWindow.rootViewController;
}

-(void)presentViewControllerUIViewController*)vc {
    UIViewController* rootVC = [self getRootViewController];
    [rootVC presentViewController:vc animated:YES completion:nil];
}



Best Answer-推荐答案


这是来自 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/






欢迎光临 OStack程序员社区-中国程序员成长平台 (https://ostack.cn/) Powered by Discuz! X3.4