当按下按钮时,这是在菜单vc中运行的代码:
var gcVIEwController: GKGameCenterVIEwController = GKGameCenterVIEwController()gcVIEwController.gameCenterDelegate = selfgcVIEwController.vIEwState = GKGameCenterVIEwControllerState.leaderboardsgcVIEwController.leaderboardIDentifIEr = "VHS"self.presentVIEwController(gcVIEwController,animated: true,completion: nil)
我在Game Center vc中有代码,但我认为它没有机会运行.应用程序在此代码后停止执行(没有断点或错误,只是不允许我点击任何内容)并显示一条d出消息,内容如下:
Game Center UnavailablePlayer 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 : NSObjectGKLocalPlayer *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 GKLocalPlayerHackGKLocalPlayer *getLocalPlayer(voID){ return [GKLocalPlayer localPlayer];}@end
一定要添加:
#import "GKLocalPlayerHack.h"
你的桥头.
感谢@marmph在这个问题上的答案:Game Center not authenticating using Swift
以上是内存溢出为你收集整理的ios – Swift – 游戏中心不可用全部内容,希望文章能够帮你解决ios – Swift – 游戏中心不可用所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)