如何验证玩家当前是否已登录iOS Game Center

本文关键字:是否 登录 iOS Center Game 何验证 验证 玩家 | 更新日期: 2023-09-27 18:05:28

谁能告诉我一个用c#编写的示例代码,检查玩家当前是否登录(或登录)到iOS Game Center ?


我所做的如下:

当玩家点击我的游戏上的"Game Center"按钮时,下面我有一个c#方法将弹出iOS Game Center窗口:

    void ShowGameCenter ()
    {
        GKGameCenterViewController controller = new GKGameCenterViewController ();
        controller.Finished += (object sender, EventArgs e) => {
            controller.DismissViewController (true, null);
        };
        AppDelegate.Shared.ViewController.PresentViewController (controller, true, null);
    }

这个方法在以下方面运行良好:如果玩家目前登录Game Center,那么这个方法会弹出真正的iOS"Game Center",如果玩家目前没有登录,那么这个方法会弹出一个警告对话框,说"Game Center Unavailable"。玩家未登录。"

然而,不幸的是,这个方法不返回一些标志或布尔值来指示玩家当前是否登录到Game Center。这个标志或布尔值是我需要在游戏的其他部分使用的。

所以,我谷歌"GKGameCenterViewController",注意到iOS已经定义了这些GKErrorCode常量:

enum {
   GKErrorUnknown = 1,
   GKErrorCancelled = 2,
   GKErrorCommunicationsFailure = 3,
   GKErrorUserDenied = 4,
   GKErrorInvalidCredentials = 5,
   GKErrorNotAuthenticated = 6,
   GKErrorAuthenticationInProgress = 7,
   GKErrorInvalidPlayer = 8,
   GKErrorScoreNotSet = 9,
   GKErrorParentalControlsBlocked = 10,
   GKErrorPlayerStatusExceedsMaximumLength = 11,
   GKErrorPlayerStatusInvalid = 12,
   ......
}

但是,我不知道这些常数中的哪一个对应于"用户未登录到Game Center",我也不知道如何获得此错误标志用于我的应用程序…

这是苹果开发者教程网页的链接,包含有关GKGameCenterViewController:

的信息https://developer.apple.com/library/mac/documentation/GameKit/Reference/GKGameCenterViewController_Ref/

这里是Apple Developer Tutorial网页的链接,该网页描述了GKErrorCode:

https://developer.apple.com/library/mac/documentation/GameKit/Reference/GameKit_ConstantsRef/index.html//apple_ref/c/econst GKErrorNotAuthenticated


实际上,我甚至不认为我上面展示的原始c#方法在我的特定场景中适合我。原因是该方法会弹出真正的iOS Game Center或"Game Center Unavailable"的警告对话框。玩家未登录"。我需要的是一种不同的方法,让NOT在这两个窗口中弹出,并且悄悄地返回一个标志或布尔值来通知我玩家当前是否登录iOS Game Center。

你认为你可以给我看一些其他的代码样本,会做什么我正在寻找?(如果你能给我展示c#代码,我将不胜感激,因为我正在使用Xamarin开发iOS游戏,我不懂Objective-C。)

如何验证玩家当前是否已登录iOS Game Center

在GameKit中总是有一个本地玩家,但他们可能目前没有登录:

var gameStartButton = new UIButton(UIButtonType.System);
gameStartButton.Frame = new CoreGraphics.CGRect(40, 40, 100, 40);
gameStartButton.SetTitle("Start Game", UIControlState.Normal);
gameStartButton.TouchUpInside += (object sender, EventArgs e) =>
{
    GKLocalPlayer.LocalPlayer.AuthenticateHandler = async (UIViewController uiViewController, Foundation.NSError error) =>
    {
        if (uiViewController != null)
        {
            await PresentViewControllerAsync(uiViewController, true);
        }
        if (GKLocalPlayer.LocalPlayer.Authenticated)
        {
            Console.WriteLine($"{GKLocalPlayer.LocalPlayer.PlayerID} : {GKLocalPlayer.LocalPlayer.Authenticated}");
            Console.WriteLine("Call a start game method...");
        }
        else
        {
            // no auth'd user, can your game use an anonymous one?
            var localPlayer = GKLocalPlayer.GetAnonymousGuestPlayer("StackOverflow");
            Console.WriteLine($"{localPlayer.PlayerID}");
        }
    };
};
Add(gameStartButton);

GKLocalPlayer类是GKPlayer的一个特殊子类,它代表了在设备上运行游戏的经过认证的玩家。在任何给定的时间,只有一个玩家可以在设备上进行身份验证;在其他玩家登录之前,这个玩家必须退出。

Ref: Apple's Doc GKLocalPlayer

注意:请记住iOS 10将从手机上删除Game Center App,所以现在开发者有责任呈现GKGameCenterViewController或实现界面并呈现自己的定制版本。