如何用swift認證本地玩家?

[英]How authenticate a local player with swift?


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.

我已經在iTunes Connect上創建了排行榜,啟用了游戲中心並創建了沙箱測試器。

Despite all I have this error :

盡管我有這個錯誤:

not able to authenticate fail error is nil

無法驗證fail錯誤是nil。

This is my error println (see my code)

這是我的錯誤println(參見我的代碼)

Did I miss something ?

我錯過什么了嗎?

Here the code of my gameViewController:

這里是我的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 个解决方案

#1


2  

I resolve by myself.

我自己解決。

code this : var localPlayer = GKLocalPlayer.localPlayer()

代碼如下:var localPlayer = GKLocalPlayer.localPlayer()

instead of : var localPlayer = GKLocalPlayer()

而不是:var localPlayer = GKLocalPlayer()


注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2014/12/03/72f57405b0ea4d95f21be6e2a1e040f.html



 
粤ICP备14056181号  © 2014-2021 ITdaan.com