OGeek|极客世界-中国程序员成长平台

标题: ios - Game Center 验证本地玩家 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-12 13:46
标题: ios - Game Center 验证本地玩家

我正在尝试使用以下代码对本地播放器进行身份验证:

GKLocalPlayer *localPlayer = [GKLocalPlayer localPlayer];
if(!localPlayer.authenticated) {
    localPlayer.authenticateHandler = ^(UIViewController* controller, NSError *error) {
        if(controller != nil)
            [self presentModalViewController:controller animated:YES];
    };
}

但问题是即使 [GKLocalPlayer localPlayer].authenticated 为假,在 authenticationHandler block 中返回的 Controller 始终为零。因此,玩家中心似乎总是被禁用。



Best Answer-推荐答案


我发现此验证码在 iOS 8 中运行良好。

- (void)authenticateLocalPlayer
    GKLocalPlayer *localPlayer = [GKLocalPlayer localPlayer];
    if (localPlayer.isAuthenticated) {
        [[NSNotificationCenter defaultCenter] postNotificationNameocalPlayerIsAuthenticated object:nil];
        return;
    }

    localPlayer.authenticateHandler  =
^(UIViewController *viewController, NSError *error) {
        [self setLastError:error];

        if(viewController != nil){
            [self setAuthenticationViewController:viewController];
        } else if([GKLocalPlayer localPlayer].isAuthenticated) {
            NSLog(@"connected to gk");
            _GKConnected = YES;
            [[NSNotificationCenter defaultCenter] postNotificationNameocalPlayerIsAuthenticated object:nil];
        }
        else {
            _GKConnected = NO;
        }
    };

    // REGISTER LISTENER FOR INVITES
    [localPlayer registerListener:self];
}

通知字符串可以是任何常量字符串(最好为反向 DNS 加上前缀以与 NotificationCenter org.myapp.notificationname 一起使用)。

在原始 OP 问题中,检索 GKPlayer 实例中 authenticated 属性的 getter 时出错:属性名称为 authenticated 但getter 必须是 isAuthenticated

找出这些语义拼写错误可能很简单,而编译器不会警告您,因为它可能是使用点符号访问的简单属性后面的更多代码,并且默认情况下,setter 以 is 开头。

顺便说一句,我可以通过工作示例教程添加指向知名教程的链接:

http://www.raywenderlich.com/60998/game-center-tutorial-how-to-make-a-simple-multiplayer-game-with-sprite-kit-part-2

关于ios - Game Center 验证本地玩家,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18432333/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://ogeek.cn/) Powered by Discuz! X3.4