如何用swift验证本地玩家?

时间:2014-12-03 16:40:44

标签: iphone authentication swift ios8 game-center

我试图将游戏中心整合到我的游戏中并创建排行榜。

我已经在iTunes Connect上创建了排行榜,启用了游戏中心并创建了一个沙箱测试器。

尽管我有这个错误:

not able to authenticate fail error is nil

这是我的错误println(参见我的代码)

我错过了什么吗?

这里是我的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 个答案:

答案 0 :(得分:2)

我自己决定。

代码:var localPlayer = GKLocalPlayer.localPlayer()

代替:var localPlayer = GKLocalPlayer()