• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

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

[复制链接]
菜鸟教程小白 发表于 2022-12-13 07:04:56 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

我想知道如何知道用户何时完成了 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/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap