add GameCenterHelper to login to GameCenter and recognize authenticated user
This commit is contained in:
parent
8a3357280e
commit
0a94e64a25
47
GoldWars/GoldWars/GameCenterHelper.swift
Normal file
47
GoldWars/GoldWars/GameCenterHelper.swift
Normal file
@ -0,0 +1,47 @@
|
||||
//
|
||||
// GameCenterHelper.swift
|
||||
// GoldWars
|
||||
//
|
||||
// Created by Jakob Haag on 02.05.20.
|
||||
// Copyright © 2020 SP2. All rights reserved.
|
||||
//
|
||||
|
||||
import GameKit
|
||||
|
||||
final class GameCenterHelper: NSObject {
|
||||
typealias CompletionBlock = (Error?) -> Void
|
||||
|
||||
// 1
|
||||
static let helper = GameCenterHelper()
|
||||
|
||||
// 2
|
||||
var viewController: UIViewController?
|
||||
|
||||
static var isAuthenticated: Bool {
|
||||
return GKLocalPlayer.local.isAuthenticated
|
||||
}
|
||||
|
||||
override init() {
|
||||
super.init()
|
||||
|
||||
GKLocalPlayer.local.authenticateHandler = { gcAuthVC, error in
|
||||
NotificationCenter.default
|
||||
.post(name: .authenticationChanged, object: GKLocalPlayer.local.isAuthenticated)
|
||||
|
||||
if GKLocalPlayer.local.isAuthenticated {
|
||||
print("Authenticated to Game Center!")
|
||||
} else if let vc = gcAuthVC {
|
||||
self.viewController?.present(vc, animated: true)
|
||||
}
|
||||
else {
|
||||
print("Error authentication to GameCenter: " +
|
||||
"\(error?.localizedDescription ?? "none")")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
extension Notification.Name {
|
||||
static let presentGame = Notification.Name(rawValue: "presentGame")
|
||||
static let authenticationChanged = Notification.Name(rawValue: "authenticationChanged")
|
||||
}
|
Loading…
Reference in New Issue
Block a user