我目前可以在iOS目标中使用Game Center,但不能在tvOS目标中使用Game Center。

我已经添加了排行榜图像并将标识符放在Xcode中:

swift - tvOS中的游戏中心未显示-LMLPHP

这是我用来提出排行榜和启动玩家的类(class)。它称为GameKitHelper.swift:

import UIKit
import Foundation
import GameKit

let PresentAuthenticationViewController = "PresentAuthenticationViewController"

class GameKitHelper: NSObject {

  static let sharedInstance = GameKitHelper()


  var authenticationViewController: UIViewController?
  var gameCenterEnabled = false

  func authenticateLocalPlayer() {

    //1
    let localPlayer = GKLocalPlayer()
    localPlayer.authenticateHandler = {(viewController, error) in

        if viewController != nil {
            //2
            self.authenticationViewController = viewController

                 NSNotificationCenter.defaultCenter().postNotificationName(PresentAuthenticationViewController, object: self)
        }
    else if error == nil {
            //3
            self.gameCenterEnabled = true
        }
    }

 }

 func reportAchievements(achievements: [GKAchievement], errorHandler: ((NSError?)->Void)? = nil) {
    guard gameCenterEnabled else {
        return
    }

    GKAchievement.reportAchievements(achievements, withCompletionHandler: errorHandler)
 }

 func showGKGameCenterViewController(viewController: UIViewController) {
    guard gameCenterEnabled else {
        return
    }

    //1
    let gameCenterViewController = GKGameCenterViewController()

    //2
    gameCenterViewController.gameCenterDelegate = self

    //3
    viewController.presentViewController(gameCenterViewController, animated: true, completion: nil)
}

   func saveHighScore(identifier: String, score: Int) {

     if (GKLocalPlayer.localPlayer().authenticated) {

        let scoreReporter = GKScore(leaderboardIdentifier: identifier)
        scoreReporter.value = Int64(score)

        let scoreArray:[GKScore] = [scoreReporter]

        GKScore.reportScores(scoreArray, withCompletionHandler: {

            error -> Void in

            if (error != nil) {

                print("error")
            }
            else {

                print("Posted score of \(score)")
            }
        })
    }
  }
 }

extension GameKitHelper: GKGameCenterControllerDelegate {
  func gameCenterViewControllerDidFinish(gameCenterViewController:   GKGameCenterViewController) {
    gameCenterViewController.dismissViewControllerAnimated(true, completion: nil)
 }
}

NavigationController类:
override func viewDidLoad() {
    super.viewDidLoad()

    NSNotificationCenter.defaultCenter().addObserver(self,
                                            selector: Selector("showAuthenticationViewController"),
                                                name: PresentAuthenticationViewController,
                                              object: nil)
    GameKitHelper.sharedInstance.authenticateLocalPlayer()
 }

 func showAuthenticationViewController() {
    let gameKitHelper = GameKitHelper.sharedInstance

    if let authenticationViewController = gameKitHelper.authenticationViewController {
        topViewController?.presentViewController(authenticationViewController,
                                                animated: true,
                                              completion: nil)
     }
  }


  deinit {
        NSNotificationCenter.defaultCenter().removeObserver(self)
  }

最佳答案

确保您的应用程序 Assets 目录中具有“Apple TV仪表盘图像”。仪表板图稿应为具有透明性的923x150,并且不需要分层,因为用户无法选择它。

tvOS Human Interface Guidelines声称仪表板插图是可选的,但以我的经验,如果您没有GKGameCenterViewController,它将拒绝出现。

关于swift - tvOS中的游戏中心未显示,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/34406727/

10-13 03:51