How do I authenticate a local player with swift?
I am trying to integrate a game center into my game and create a leaderboard.
I am already creating a leaderboard in iTunes Connect, turning on the game center and creating a sandbox tester.
Despite everything, I have this error:
not able to authenticate fail
error is nil
This is my println error (see my code)
Did I miss something?
Here is the code for 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")
}
}
}
}
}
+3
source to share