问题
I'm trying to integrate game center to my game and create a leaderboard.
I already create the leaderboard on iTunes Connect, enable the game center and create a sandbox tester.
Despite all I have this error :
not able to authenticate fail
error is nil
This is my error println (see my code)
Did I miss something ?
Here the code of my gameViewController:
import UIKit
import SpriteKit
import AVFoundation
import iAd
import GameKit
var defaults = NSUserDefaults()
var highscore = defaults.integerForKey("highscore")
class GameViewController: UIViewController, ADBannerViewDelegate, GKGameCenterControllerDelegate {
var adBannerView: ADBannerView!
var leaderboardIdentifier: String? = nil
var gameCenterEnabled: Bool = false
override func viewDidLoad() {
super.viewDidLoad()
var skView = self.originalContentView as SKView
var scene:SKScene = GameScene(size: skView.bounds.size)
skView.presentScene(scene)
loadAds()
}
func authenticateLocalPlayer()
{
var localPlayer = GKLocalPlayer()
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
println("\(self.leaderboardIdentifier)") //in your example "VHS" should be returned
}
}
}
else
{
println("not able to authenticate fail")
self.gameCenterEnabled = false
if (error != nil)
{
println("\(error.description)")
}
else
{
println( "error is nil")
}
}
}
}
}
回答1:
I resolve by myself.
code this : var localPlayer = GKLocalPlayer.localPlayer()
instead of : var localPlayer = GKLocalPlayer()
来源:https://stackoverflow.com/questions/27277011/how-authenticate-a-local-player-with-swift