How authenticate a local player with swift?

大憨熊 提交于 2019-12-22 10:53:22

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!