我可以手动提示用户登录iOS 7上的Game Center吗? [英] Can I manually prompt the user to log in to Game Center on iOS 7?

查看:170
本文介绍了我可以手动提示用户登录iOS 7上的Game Center吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据Apple的Game Center编程指南,此代码设置了一个身份验证处理程序。如果你在游戏开始时运行它,第一次运行它,它会提示用户登录,如果还没有。

According to Apple's Game Center programming guide, this code sets up an authentication handler. If you run this at the beginning of your game, the first time you run it, it will prompt the user to log in if they haven't yet.

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

假设用户尚未登录,并取消认证屏幕正常玩游戏。

Suppose that the user hasn't logged in yet, and cancels the authentication screen to play the game normally.

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

There is a button for playing a multiplayer match in my game. If the user presses the button, it will attempt to search for other players by presenting a GKMatchmakerViewController instance.

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

Since the player isn't logged in, the player will actually receive an error dialog saying that they aren't logged in. The dialog only has an OK button, which dismisses it.

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

If the player insists on pressing this button, the same dialog will come.

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

However, this is an odd behaviour. It would be more reasonable that if the player wants to play a multiplayer match but isn't logged in yet, the game will prompt the user to log in.

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

The above code sets up a handler, so it really isn't what I'm looking for. However, I made a breakpoint and noticed that viewController is a GKHostedAuthenticateViewController instance. I figured that maybe I could create an instance of that class and present it, which should technically be equivalent to prompting the user to log in.

但是,Xcode没有我写这篇文章时似乎认出了这门课。我的印象是我不允许这样做。

However, Xcode doesn't seem to recognise that class when I write it. I'm under the impression that I'm not allowed to do this.

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

推荐答案

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

You can first check if the player is authenticated or not, by reading the GKLocalPlayer object.

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

If there is not authenticated user, you can open the game centre app. The downside with this method is that after the user authenticates through the game centre app, he is still in the game centre app and has to "switch back" to your app. When he switches back, the authentication handler you defined in your code gets triggered.

-(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的文档中,他们说您不应该提示用户再次登录,或显示登录提示。自动化方式(您的代码已经拥有)应该是可接受的方式。显示我上面描述的警报视图只是帮助用户登录游戏中心,因为您不应强迫应用程序显示对话。

Note, that in Apple's documentation, they say that you shouldn't be prompting the user to log in again, or show a login prompt. The automated way, (which your code already has) is supposed to be the accepted way. Showing the alert view I described above just helps the user log into game centre since you aren't supposed to force the app to show a dialogue.

这篇关于我可以手动提示用户登录iOS 7上的Game Center吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆