SwiftUI Game Center 身份验证不提示用户登录
SwiftUI Game Center authentication doesn't prompt user to login
我有以下代码可以在我的 SwiftUI 应用程序的 Game Center 中验证本地玩家。如果玩家尚未登录,我希望 Game Center 提示用户登录,但这并没有发生。
class AppSettings: UINavigationController {
func authenticateUser() {
let localPlayer = GKLocalPlayer.local
localPlayer.authenticateHandler = { vc, error in
guard error == nil else {
print(error?.localizedDescription ?? "")
return
}
}
}
}
可能是什么问题?我还阅读了有关在我的 class 某处使用 UIViewControllerRepresentable 将 UIKit 的 ViewController 集成到 SwiftUI 中的信息,但我不明白如何使用它。
有人可以帮我吗?
authenticateHandler
returns 一个 UIViewController
还有你没有使用的:
@available(iOS 6.0, *)
open var authenticateHandler: ((UIViewController?, Error?) -> Void)?
您需要出示:
class AppSettings: UINavigationController {
func authenticateUser() {
let localPlayer = GKLocalPlayer.local
localPlayer.authenticateHandler = { vc, error in
guard error == nil else {
print(error?.localizedDescription ?? "")
return
}
if let vc = vc {
self.present(vc, animated: true, completion: nil)
}
}
}
}
我没有让任何人正确回答我的问题,经过几天的挖掘,我找到了解决方案。所以我不得不像下面这样使用 UIKit 实现,并使用 GameCenterManager 结构中的 UIViewControllerRepresentable 围绕它创建一个包装器。之后,我所要做的就是在 ZStack 的 SwiftUI 视图中调用 GameCenterManager(),工作就完成了!
import SwiftUI
import UIKit
import GameKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
authenticateUser()
}
let localPlayer = GKLocalPlayer.local
func authenticateUser() {
localPlayer.authenticateHandler = { vc, error in
guard error == nil else {
print(error?.localizedDescription ?? "")
return
}
if vc != nil {
self.present(vc!, animated: true, completion: nil)
}
if #available(iOS 14.0, *) {
GKAccessPoint.shared.location = .bottomLeading
GKAccessPoint.shared.showHighlights = true
GKAccessPoint.shared.isActive = self.localPlayer.isAuthenticated
// Fallback on earlier versions
}
}
}
}
struct GameCenterManager: UIViewControllerRepresentable {
func makeUIViewController(context: UIViewControllerRepresentableContext<GameCenterManager>) -> ViewController {
let viewController = ViewController()
return viewController
}
func updateUIViewController(_ uiViewController: ViewController, context: UIViewControllerRepresentableContext<GameCenterManager>) {
}
}
我有以下代码可以在我的 SwiftUI 应用程序的 Game Center 中验证本地玩家。如果玩家尚未登录,我希望 Game Center 提示用户登录,但这并没有发生。
class AppSettings: UINavigationController {
func authenticateUser() {
let localPlayer = GKLocalPlayer.local
localPlayer.authenticateHandler = { vc, error in
guard error == nil else {
print(error?.localizedDescription ?? "")
return
}
}
}
}
可能是什么问题?我还阅读了有关在我的 class 某处使用 UIViewControllerRepresentable 将 UIKit 的 ViewController 集成到 SwiftUI 中的信息,但我不明白如何使用它。 有人可以帮我吗?
authenticateHandler
returns 一个 UIViewController
还有你没有使用的:
@available(iOS 6.0, *)
open var authenticateHandler: ((UIViewController?, Error?) -> Void)?
您需要出示:
class AppSettings: UINavigationController {
func authenticateUser() {
let localPlayer = GKLocalPlayer.local
localPlayer.authenticateHandler = { vc, error in
guard error == nil else {
print(error?.localizedDescription ?? "")
return
}
if let vc = vc {
self.present(vc, animated: true, completion: nil)
}
}
}
}
我没有让任何人正确回答我的问题,经过几天的挖掘,我找到了解决方案。所以我不得不像下面这样使用 UIKit 实现,并使用 GameCenterManager 结构中的 UIViewControllerRepresentable 围绕它创建一个包装器。之后,我所要做的就是在 ZStack 的 SwiftUI 视图中调用 GameCenterManager(),工作就完成了!
import SwiftUI
import UIKit
import GameKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
authenticateUser()
}
let localPlayer = GKLocalPlayer.local
func authenticateUser() {
localPlayer.authenticateHandler = { vc, error in
guard error == nil else {
print(error?.localizedDescription ?? "")
return
}
if vc != nil {
self.present(vc!, animated: true, completion: nil)
}
if #available(iOS 14.0, *) {
GKAccessPoint.shared.location = .bottomLeading
GKAccessPoint.shared.showHighlights = true
GKAccessPoint.shared.isActive = self.localPlayer.isAuthenticated
// Fallback on earlier versions
}
}
}
}
struct GameCenterManager: UIViewControllerRepresentable {
func makeUIViewController(context: UIViewControllerRepresentableContext<GameCenterManager>) -> ViewController {
let viewController = ViewController()
return viewController
}
func updateUIViewController(_ uiViewController: ViewController, context: UIViewControllerRepresentableContext<GameCenterManager>) {
}
}