我正试图在我的 Swift游戏中实现Game Center.我有一个菜单视图控制器,用户可以按下“SCORES”按钮,它应该将它们带到Game Center视图控制器. 当按下按钮时,这是在菜单vc中运行的代码: var g
当按下按钮时,这是在菜单vc中运行的代码:
var gcViewController: GKGameCenterViewController = GKGameCenterViewController() gcViewController.gameCenterDelegate = self gcViewController.viewState = GKGameCenterViewControllerState.Leaderboards gcViewController.leaderboardIdentifier = "VHS" self.presentViewController(gcViewController, animated: true, completion: nil)
我在Game Center vc中有代码,但我认为它没有机会运行.应用程序在此代码后停止执行(没有断点或错误,只是不允许我点击任何内容)并显示一条弹出消息,内容如下:
Game Center Unavailable Player is not signed in
我得到的唯一其他响应是在Xcode中,其中以下行打印到日志:
2014-08-29 14:10:33.157 Valley[2291:304785] 17545849:_UIScreenEdgePanRecognizerEdgeSettings.edgeRegionSize=13.000000
我不知道这意味着什么或为什么Game Center不工作.任何人都可以帮忙吗?
假设您已在应用中启用了Game Center并在iTunes Connect中添加了排行榜,那么您需要在显示GC之前对您的播放器进行身份验证.此外,请确保您已在iTunes Connect中创建了一个测试用户,您可以在出现提示时使用该用户登录Game Center.您的MenuViewController应该在viewDidLoad中验证本地播放器,如下所示:
class MenuViewController: UIViewController, GKGameCenterControllerDelegate { var leaderboardIdentifier: String? = nil var gameCenterEnabled: Bool = false override func viewDidLoad() { super.viewDidLoad() //Your code that sets up your scene or other set up code //HERE IS WHERE YOU AUTHENTICATE authenticateLocalPlayer() } func authenticateLocalPlayer() { var localPlayer = getLocalPlayer() // see GKLocalPlayerHack.h localPlayer.authenticateHandler = { (viewController : UIViewController!, error : NSError!) -> Void in if viewController != nil { self.presentViewController(viewController, animated:true, completion: nil) } else { if localPlayer.authenticated { self.gameCenterEnabled = true localPlayer.loadDefaultLeaderboardIdentifierWithCompletionHandler { (leaderboardIdentifier, error) -> Void in if error != nil { print("error") } else { self.leaderboardIdentifier = leaderboardIdentifier print("\(self.leaderboardIdentifier)") //in your example "VHS" should be returned } } } else { print("not able to authenticate fail") self.gameCenterEnabled = false if error { print("\(error.description)") } else { print( "error is nil") } } } } } func gameCenterViewControllerDidFinish(gameCenterViewController: GKGameCenterViewController!) { gameCenterViewController.dismissViewControllerAnimated(true, completion: nil) } }
在您成功通过身份验证后,您应该可以使用Game Center.
注意这一行:
var localPlayer = getLocalPlayer()//参见GKLocalPlayerHack.h
为了让它工作,你需要做一点点破解让GKLocalPlayer在Swift中正确实例化.
在Objective-C中创建一个新类,并将文件命名为GKLocalPlayerHack.h / m
在标题中:
// GKLocalPlayerHack.h // Issue with GameKit and Swift // https://stackoverflow.com/questions/24045244/game-center-not-authenticating-using-swift #import <GameKit/GameKit.h> @interface GKLocalPlayerHack : NSObject GKLocalPlayer *getLocalPlayer(void); @end
在执行文件中:
// GKLocalPlayerHack.m // Issue with GameKit and Swift // https://stackoverflow.com/questions/24045244/game-center-not-authenticating-using-swift #import "GKLocalPlayerHack.h" @implementation GKLocalPlayerHack GKLocalPlayer *getLocalPlayer(void) { return [GKLocalPlayer localPlayer]; } @end
一定要添加:
#import "GKLocalPlayerHack.h"
你的桥头.
感谢@marmph在这个问题上的答案:Game Center not authenticating using Swift