xcode Game Center 未使用 Swift 进行身份验证
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24045244/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Game Center not authenticating using Swift
提问by Andy Heard
I'm trying to authenticate the local player using swift, but every time I get a false value for the .authenticated property. Here is the code I'm using, it is called by the main view controller when the app starts.
我正在尝试使用 swift 对本地播放器进行身份验证,但每次我得到 .authenticated 属性的错误值时。这是我正在使用的代码,它在应用程序启动时由主视图控制器调用。
func authenticateLocalPlayer(){
var localPlayer = GKLocalPlayer()
localPlayer.authenticateHandler = {(viewController, error) -> Void in
if viewController {
self.presentViewController(viewController, animated: true, completion: nil)
}else{
println((GKLocalPlayer().authenticated))
}
}
}
It brings up the log in view just fine, but when I enter a test account login, it just returns the GKLocalPlayer().authenticated
as false. The bundle identifier in iTunes Connect and the info.plist are exactly the same, as is the version and the app name. Everything is enabled for Game Center on iTunes Connect and in Xcode, but I have a feeling it's not a coding error, it's a setup error in the app record somewhere but I can't for the life of me find where.
它可以很好地显示登录视图,但是当我输入测试帐户登录时,它只返回GKLocalPlayer().authenticated
false。iTunes Connect 中的包标识符和 info.plist 完全相同,版本和应用程序名称也是如此。在 iTunes Connect 和 Xcode 中,Game Center 的所有功能都已启用,但我感觉这不是编码错误,而是应用程序记录中某处的设置错误,但我终生无法找到哪里。
After further tinkering, I'm getting this error:
进一步修补后,我收到此错误:
Error Domain=GKErrorDomain Code=15 "The requested operation could not be completed because this application is not recognized by Game Center." UserInfo=0x17006b300 {NSLocalizedDescription=The requested operation could not be completed because this application is not recognized by Game Center.}
错误域=GKErrorDomain 代码=15“请求的操作无法完成,因为游戏中心无法识别此应用程序。” UserInfo=0x17006b300 {NSLocalizedDescription=请求的操作无法完成,因为游戏中心无法识别此应用程序。}
I have no idea why this is the case, the bundle ID, name and versions all match...
我不知道为什么会这样,包 ID、名称和版本都匹配......
Any help would be greatly appreciated.
任何帮助将不胜感激。
回答by marmph
This issue has been resolved by Apple - just call:
此问题已由 Apple 解决 - 只需致电:
GKLocalPlayer.localPlayer()
Previously, the issue was that GKLocalPlayer()
does not return the GKLocalPlayer singleton, but instead returns a new GKLocalPlayer instance.
以前,问题是GKLocalPlayer()
不返回 GKLocalPlayer 单例,而是返回一个新的 GKLocalPlayer 实例。
If you were on the Xcode 6 BETA, you could add a C function or Objective-C method that returns the real GKLocalPlayer singleton, then use this in Swift. This is the gist of my workaround (with bad naming conventions):
如果您使用的是 Xcode 6 BETA,您可以添加一个 C 函数或 Objective-C 方法来返回真正的 GKLocalPlayer 单例,然后在 Swift 中使用它。这是我的解决方法的要点(命名约定不好):
In an Objective-C header:
在 Objective-C 标头中:
GKLocalPlayer *getLocalPlayer(void);
In an Objective-C implementation:
在 Objective-C 实现中:
GKLocalPlayer *getLocalPlayer(void) {
return [GKLocalPlayer localPlayer];
}
In your bridging header:
在您的桥接头中:
#import "ThatHeader.h"
Then whenever you need to access the GKLocalPlayer singleton in Swift, you can just use getLocalPlayer()
instead of GKLocalPlayer()
. It's probably a better idea to stick that in an method of a GKLocalPlayer category.
然后,每当您需要在 Swift 中访问 GKLocalPlayer 单例时,您都可以使用getLocalPlayer()
代替GKLocalPlayer()
. 将其保留在 GKLocalPlayer 类别的方法中可能是一个更好的主意。
However, this is no longer necessary as detailed above.
然而,这不再是必要的,如上所述。
回答by Raz
Even with Xcode 6 Beta 6, on a device using iOS 8 beta 5, making GKLocalPlayer.localPlayer() available, I was still getting the error:
即使使用 Xcode 6 Beta 6,在使用 iOS 8 beta 5 的设备上,使 GKLocalPlayer.localPlayer() 可用,我仍然收到错误:
"NSLocalizedDescription=The requested operation could not be completed because this application is not recognized by Game Centre"
“NSLocalizedDescription=请求的操作无法完成,因为游戏中心无法识别此应用程序”
The solution(discovered through Apple's Dev forum) was to go to "Settings" on the device, and then into "Game Centre" and enable "Sandbox" under the developer section.
该解决方案(通过苹果开发论坛发现的)是去到“设置”设备上,然后进入“游戏中心”,并在显影部启用了“沙盒”。
回答by YannSteph
You can use that, I create a simple class for iOS game center in github Easy Class Game Center Swift
你可以使用它,我在 github Easy Class Game Center Swift 中为 iOS 游戏中心创建了一个简单的类
https://github.com/DaRkD0G/Easy-Game-Center-Swift
https://github.com/DaRkD0G/Easy-Game-Center-Swift
Message from France, Merry Christmas
来自法国的消息,圣诞快乐