我可以手动提示用户登录iOS 7上的Game Center吗?

根据Apple的Game Center编程指南,此代码设置了一个身份验证处理程序.如果你在游戏开始时运行它,第一次运行它时,它会提示用户登录,如果还没有.
- (void)authenticateLocalPlayer {
    GKLocalPlayer *localPlayer = [GKLocalPlayer localPlayer];
    localPlayer.authenticateHandler = ^(UIViewController *viewController,NSError *error){
        if (viewController != nil) {
            NSLog(@"Player not authenticated.");
        } else if (localPlayer.isAuthenticated) {
            NSLog(@"Authentication successful.");
        } else {
            NSLog(@"Authentication Failed. Error: %@.",error);
        }
    };
}

假设用户尚未登录,并取消认证屏幕以正常播放游戏.

在我的游戏中有一个用于玩多人游戏的按钮.如果用户按下按钮,它将尝试通过呈现gkmatchmakerViewController实例来搜索其他玩家.

由于播放器未登录,播放器实际上会收到一个错误对话框,说明他们没有登录.对话框只有一个OK按钮,可以解除它.

如果玩家坚持按下此按钮,则会出现相同的对话框.

然而,这是一种奇怪的行为.如果玩家想要玩多人游戏但尚未登录则更合理,游戏将提示用户登录.

上面的代码设置了一个处理程序,所以它真的不是我想要的.但是,我做了一个断点,并注意到viewController是一个GKHostedAuthenticateViewController实例.我想也许我可以创建该类的实例并呈现它,这在技术上应该等同于提示用户登录.

但是,当我编写它时,Xcode似乎没有识别出该类.我的印象是我不允许这样做.

如何手动提示用户登录Game Center?

解决方法

您可以通过阅读GKLocalPlayer对象来检查播放器是否已通过身份验证.

如果没有经过身份验证的用户,您可以打开游戏中心应用程序.这种方法的缺点是,在用户通过游戏中心应用程序进行身份验证后,他仍然在游戏中心应用程序中,并且必须“切换回”到您的应用程序.当他切换回来时,您在代码中定义的身份验证处理程序会被触发.

-(void)clickedOnStartGame
{
    if (_signedIn)
    {
        //Do what you need to.
    }
    else if (!_signedIn)
    {
        UIAlertView *alertView = [[UIAlertView alloc]
                              initWithTitle:@"Game Center"
                              message:@"If Game Center is disabled try logging in through the Game Center app"
                              delegate:self
                              cancelButtonTitle:@"OK"
                              otherButtonTitles:@"Open Game Center",nil];
        [alertView show];
    }
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
if (buttonIndex == 1) {
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"gamecenter:"]];
    }
}

编辑:请注意,在Apple的文档中,他们说您不应该提示用户再次登录,或显示登录提示.自动化方式(您的代码已经拥有)应该是可接受的方式.显示我上面描述的警报视图只是帮助用户登录游戏中心,因为您不应该强制应用程序显示对话.

相关文章

当我们远离最新的 iOS 16 更新版本时,我们听到了困扰 Apple...
欧版/美版 特别说一下,美版选错了 可能会永久丧失4G,不过只...
一般在接外包的时候, 通常第三方需要安装你的app进行测...
前言为了让更多的人永远记住12月13日,各大厂都在这一天将应...