Game Center used to work perfectly a while ago for a different app that I made. However strange things are happening with my current one for about two weeks now.
Uploading and download report no error and I can see my score fine. But if I stop play my game and then 6 hours later come back to it, my score is no longer there in the game center. I mean, literally no score at all. Downloading from game center which works 6 hours ago now cannot retrieve my score.
Game center status is live. I can see my score and others in "manage score" section. However, I cannot see anything except my score in the leaderboard from GKGameCenterViewController.
As far as I'm concern, everything has been configured correctly. Game Center in app's capability is on.
Code for showing leaderboard:
func showLeaderBoard() {
if GKLocalPlayer.localPlayer().isAuthenticated == false {
self.present(authenticationViewController!, animated: true, completion: nil)
return
}
let gamecenter = GKGameCenterViewController()
gamecenter.gameCenterDelegate = self
gamecenter.viewState = .leaderboards
gamecenter.leaderboardIdentifier = leaderBoardID
present(gamecenter, animated: true, completion: nil)
}
Code for downloading score:
func downloadBestScoreFromGameCenter() {
if GKLocalPlayer.localPlayer().isAuthenticated == false {return}
print("Downloading Score...")
let leaderBoard = GKLeaderboard()
leaderBoard.identifier = leaderBoardID
leaderBoard.loadScores(completionHandler: {
[unowned self] (scores, error) in
print("Download Error: \(error)")
scores?.forEach({print("\($0.player?.displayName!) \($0.value)")})
if let localPlayerScore = leaderBoard.localPlayerScore?.value {
self.bestScore = Int(localPlayerScore)
self.saveGameForCurrentState()
} else {
self.bestScore = 0
}
})
}
And uploading
func uploadBestScoreToGameCenter() {
if GKLocalPlayer.localPlayer().isAuthenticated == false {return}
let scoreItem = GKScore(leaderboardIdentifier: leaderBoardID)
scoreItem.value = Int64(self.bestScore)
GKScore.report([scoreItem], withCompletionHandler: {
(error) in
if let error = error {
print(error)
} else {
print("Upload complete: \(scoreItem.value)")
}
})
}
Ok the leaderboard is now working today. I guess you just have to wait people.
Related
I have a question, help me understand the topic. Honestly, I've spent several days studying the topic, but it's not working yet.
There is a simple game on SpriteKit, Swift 3.
I'm trying to implement Leaderboard Game Center.
iTunes Connect has already set up, the leaderboard has already created, the test user has already created.
When I start the game, the user logs in as they should. The button to display the board created and I can open my leaderboard in the Game Center.
The problem is that the value of the points scored in the leaderboard is not updated.
My game scores are stored in variable "score"
In gamescene.swift, I have two functions for saving and overriding:
func saveHighscore(gameScore: Int) {
if GKLocalPlayer.localPlayer().isAuthenticated {
print("\n Success! Sending highscore of \(score) to leaderboard")
let scoreReporter = GKScore(leaderboardIdentifier: “MY_ID_THERE”)
scoreReporter.value = Int64(gameScore)
let scoreArray: [GKScore] = [scoreReporter]
GKScore.report(scoreArray, withCompletionHandler: {error -> Void in
if error != nil {
print("An error has occured: \(String(describing: error))")
}
})
}
}
func overrideHighestScore(gameScore: Int) {
UserDefaults.standard.integer(forKey: "highestScore")
if gameScore > UserDefaults.standard.integer(forKey: "highestScore") {
UserDefaults.standard.set(gameScore, forKey: "highestScore")
UserDefaults.standard.synchronize()
saveHighscore(gameScore: score)
print(score.hashValue)
}
}
I call both functions when I press the button
saveHighscore (gameScore: score)
overrideHighestScore (gameScore: score)
The console displays output correctly, for example, when collecting five points the output will be a message called:
Success! Sending the highscore of 5 to the leaderboard
but the value of the variable score in the game center is not sent and there are zero values achieved at the first access to the board
I really hope for your help.
Sincerely,
Eugene.
I hope you have properly configured leaderboard at https://itunesconnect.apple.com
Please refer below screen
Also refer below code to save score on leaderboard.
func saveScoreOnGameCenter()
{
let leaderboardID = 111
let sScore = GKScore(leaderboardIdentifier: leaderboardID)
sScore.value = Int64(10)
GKScore.reportScores([sScore], withCompletionHandler: { (error: NSError?) -> Void in
if error != nil {
print(error!.localizedDescription)
} else {
print("Score submitted")
}
})
}
Hope this helps you to figure out problem.
I'm working on an app for a hands free BLE device for changing music and answering phone calls. Everything I'm reading says this can't be done, but figured I'd ask and see if maybe there was a change now with CallKit.
I'm able to see call events using CXCallObserverDelegate, but am unable to answer phone calls with the CXAnswerCallAction.
Here is my current code, I'm using a delay for testing purposes. Eventually the action would be triggered by an action from the BLE device:
self.currentCallUUID = call.uuid
if let callUUID = self.currentCallUUID
{
let answerAction: CXAnswerCallAction = CXAnswerCallAction(call: callUUID)
let transaction: CXTransaction = CXTransaction(action: answerAction)
let when = DispatchTime.now() + 2
DispatchQueue.main.asyncAfter(deadline: when) {
print("answering call")
self.callController.request(transaction, completion: { error in
if (error != nil)
{
print("did answer")
answerAction.fulfill()
}
else
{
print("answer failed: \(String(describing: error?.localizedDescription))")
}
})
}
}
I'm using Game Center in my app and I'm having some problem with the GKLocalPlayer.authenticated attribute. Regardless if the authentication process is successful or not, localPlayer.authenticated always returns false. This also happens if my device is already logged in to Game Center.
I get this both on actual device (iPhone 6s) and simulator (tried several).
The only information I've found about this suggests that there is a problem with the time settings but they seem to be fine.
Is this a bug or am I doing something wrong?
private static let localPlayer = GKLocalPlayer()
static func authenticateLocalPlayer() {
localPlayer.authenticateHandler = { (viewController, error) -> Void in
if let viewController = viewController {
if let rootViewController = UIApplication.sharedApplication().keyWindow?.rootViewController {
rootViewController.presentViewController(viewController, animated: true, completion: nil)
}
} else if localPlayer.authenticated {
gameCenterEnabled = true
let defaultCenter = NSNotificationCenter.defaultCenter()
defaultCenter.postNotificationName("local_player_authenticated", object: nil)
} else {
gameCenterEnabled = false
}
if let error = error {
print(error)
}
}
}
static func isAuthenticated() -> Bool {
return localPlayer.authenticated
}
My bad, looks like I made a little mistake when translating this code from Objective-C. It's supposed to be
GKLocalPlayer.localPlayer()
not
GKLocalPlayer()
I've tried a whole bunch of ways to get Game Center working in my SpriteKit game. Unfortunately the way I've done it in the past using ObjC and ViewControllers don't work because I'm using SKScene/ a GameScene.
This is the swift version of the code (I think):
// MARK: Game Center Integration
//login and make available
func authenticateLocalPlayer(){
let localPlayer = GKLocalPlayer()
print(localPlayer)
localPlayer.authenticateHandler = {(viewController, error) -> Void in
if ((viewController) != nil) {
self.presentViewController(viewController!, animated: true, completion: nil)
}else{
print((GKLocalPlayer.localPlayer().authenticated))
}
}
}
//submit a score to leaderboard
func reportScoreToLeaderboard(thisScore:Int){
if GKLocalPlayer.localPlayer().authenticated {
let scoreReporter = GKScore(leaderboardIdentifier: "LeaderboardID")
scoreReporter.value = Int64(thisScore)
let scoreArray: [GKScore] = [scoreReporter]
GKScore.reportScores(scoreArray, withCompletionHandler: { (error: NSError?) -> Void in
if error != nil {
print(error!.localizedDescription)
} else {
print("Score submitted")
}
})
}
}
//show leaderboard (call from button or touch)
func showLeaderboard() {
let vc = self.view?.window?.rootViewController
let gc = GKGameCenterViewController()
gc.gameCenterDelegate = self
vc?.presentViewController(gc, animated: true, completion: nil)
}
//hides view when finished
func gameCenterViewControllerDidFinish(gameCenterViewController: GKGameCenterViewController){
gameCenterViewController.dismissViewControllerAnimated(true, completion: nil)
}
Unfortunetely, no matter what I try, I either get this error:
Attempting to load the view of a view controller while it is deallocating is not allowed and may result in undefined behavior
... or it just crashes.
I've read that NSNotifications can be used? But how?
I'm guessing the best way is to set it all up in the GameViewController.swift and use NSNotifications to communicate with the RootViewController from the GameScene? I can't seem to find a tutorial or example though.
Use delegation when a view controller needs to change views, do not have the view itself change views, this could cause the view trying to deallocating while the new view is being presented, thus the error you are getting
I'm simply trying to log a high score of a single game mode in a game I'm making. I have a leaderboard setup in Game Center on iTunes Connect.
So, my question is, how do I integrate this into my game? I've seen other solutions but can't seem to figure out how they fit into my project.
Thanks!
In your view controller implement the GKGameCenterControllerDelegate
Create a local player
var localPlayer: GKLocalPlayer = GKLocalPlayer.localPlayer()
Authenticate the player in the viewDidLoad with the game center and present the authentication success
localPlayer.authenticateHandler = {(ViewController, error) -> Void in
if((ViewController) != nil) {
self.presentViewController(ViewController, animated: true, completion: nil)
}
}
Report your score from anywhere in your game
if (GKLocalPlayer.localPlayer().authenticated) {
let gkScore = GKScore(leaderboardIdentifier: "YOUR-LEADERBOARD-ID")
gkScore.value = Int64(YOUR-SCORE)
GKScore.reportScores([gkScore], withCompletionHandler: ( { (error: NSError!) -> Void in
if (error != nil) {
// handle error
println("Error: " + error.localizedDescription);
} else {
println("Score reported: \(gkScore.value)")
}
}))
}