Currently, my app prompts a user to log into Game Center as soon as they open the app. Once logged in, they can view their achievements and leaderboards. However; if the user declines to log into game center and then presses the leaderboard or achievement button, the entire app crashes. What the app should do in this situation is re-prompt the user to log in. Any suggestions would be greatly appreciated.
class viewController: UIViewController, GKGameCenterControllerDelegate {
var highscore = NSUserDefaults.standardUserDefaults().integerForKey("highscore")
var loggedin = 1
override func viewDidLoad() {
super.viewDidLoad()
login()
}
func login() {
println("Game Center Login Called")
let localPlayer = GKLocalPlayer.localPlayer()
loggedin = 2
// Handle the authentication
localPlayer.authenticateHandler = {(Home: UIViewController!, error: NSError!) -> Void in
if Home != nil {
println("Authentication is being processed.")
self.presentViewController(Home, animated: true, completion: nil)
} else {
println("Player has been successfully authenticated.")
}
}
}
func showLeaderboard() {
let gkScore = GKScore(leaderboardIdentifier: "high_Score_Leader_Board")
gkScore.value = Int64(highscore)
GKScore.reportScores([gkScore], withCompletionHandler: ( { (error: NSError!) -> Void in
if (error != nil) {
// handle error
println("Error: " + error.localizedDescription);
} else {
println("Score reported: \(gkScore.value)")
}
}))
var gcViewController: GKGameCenterViewController = GKGameCenterViewController()
gcViewController.gameCenterDelegate = self
gcViewController.viewState = GKGameCenterViewControllerState.Leaderboards
gcViewController.leaderboardIdentifier = "high_Score_Leader_Board"
self.showViewController(gcViewController, sender: self)
self.presentViewController(gcViewController, animated: true, completion: nil)
}
@IBAction func gameCenterButtoPressed(sender: AnyObject) {
if loggedin == 2 {
showLeaderboard()
}
else {
login()
}
}
@IBAction func pointButtonScored(sender: AnyObject) {
ReportAchievment("testbutton", percentComplete: 100)
}
func gameCenterViewControllerDidFinish(gcViewController: GKGameCenterViewController!)
{
self.dismissViewControllerAnimated(true, completion: nil)
}
func showAchievements() {
var gcViewController: GKGameCenterViewController = GKGameCenterViewController()
gcViewController.gameCenterDelegate = self
gcViewController.viewState = GKGameCenterViewControllerState.Achievements
self.showViewController(gcViewController, sender: self)
self.presentViewController(gcViewController, animated: true, completion: nil)
}
func ReportAchievment(identifier : String, percentComplete : Double)
{
var achievement = GKAchievement(identifier: identifier)
if(achievement != nil)
{
achievement.percentComplete = percentComplete;
achievement.showsCompletionBanner = true
GKAchievement.reportAchievements([achievement], withCompletionHandler: { (error : NSError!) -> Void in
println("Achievement reported")
})
}
}
@IBAction func achievementButtonPressed(sender: AnyObject) {
showAchievements()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}