gpt4 book ai didi

ios - 新的 Xcode UIViewControllerRepresentables 必须是模拟器的值类型而不是真实设备

转载 作者:行者123 更新时间:2023-12-05 04:23:17 25 4
gpt4 key购买 nike

在 xcode 以某种方式更新后我遇到了一个问题(猜测它是一个新的 xcode 版本)。当我尝试在新模拟器中运行该应用程序时,出现错误 Thread 1: Fatal error: UIViewControllerRepresentables must be value types: SignInWithAppleToFirebase 但当我在物理设备上运行时,一切仍按预期运行.当我四处寻找错误时,我发现 some explanations 有点有意义并适用于 SignInWithAppleToFirebase 类。 SignInWithAppleToFirebase 在下面,但我有点想解释为什么在我将这个类重新加工成一个结构之前我的物理设备上工作(如果这只是 xcode 的一些错误,我会遇到一些我不想处理的问题) ).

   final class SignInWithAppleToFirebase: UIViewControllerRepresentable {
private var appleSignInDelegates: SignInWithAppleDelegates! = nil
private let onLoginEvent: ((SignInWithAppleToFirebaseResponse) -> ())?
private var currentNonce: String?

init(_ onLoginEvent: ((SignInWithAppleToFirebaseResponse) -> ())? = nil) {
self.onLoginEvent = onLoginEvent
}

func makeUIViewController(context: Context) -> UIViewController {
let vc = UIHostingController(rootView: SignInWithAppleButton().onTapGesture(perform: showAppleLogin))
return vc as UIViewController
}

func updateUIViewController(_ uiView: UIViewController, context: Context) {

}

private func showAppleLogin() {
let nonce = randomNonceString()
currentNonce = nonce
let appleIDProvider = ASAuthorizationAppleIDProvider()
let request = appleIDProvider.createRequest()
request.requestedScopes = [.fullName, .email]
request.nonce = sha256(nonce)

performSignIn(using: [request])
}

private func performSignIn(using requests: [ASAuthorizationRequest]) {
guard let currentNonce = self.currentNonce else {
return
}
appleSignInDelegates = SignInWithAppleDelegates(window: nil, currentNonce: currentNonce, onLoginEvent: self.onLoginEvent)

let authorizationController = ASAuthorizationController(authorizationRequests: requests)
authorizationController.delegate = appleSignInDelegates
authorizationController.presentationContextProvider = appleSignInDelegates
authorizationController.performRequests()
}



// Adapted from https://auth0.com/docs/api-auth/tutorials/nonce#generate-a-cryptographically-random-nonce
private func randomNonceString(length: Int = 32) -> String {
precondition(length > 0)
let charset: Array<Character> =
Array("0123456789ABCDEFGHIJKLMNOPQRSTUVXYZabcdefghijklmnopqrstuvwxyz-._")
var result = ""
var remainingLength = length

while remainingLength > 0 {
let randoms: [UInt8] = (0 ..< 16).map { _ in
var random: UInt8 = 0
let errorCode = SecRandomCopyBytes(kSecRandomDefault, 1, &random)
if errorCode != errSecSuccess {
fatalError("Unable to generate nonce. SecRandomCopyBytes failed with OSStatus \(errorCode)")
}
return random
}

randoms.forEach { random in
if length == 0 {
return
}

if random < charset.count {
result.append(charset[Int(random)])
remainingLength -= 1
}
}
}

return result
}

private func sha256(_ input: String) -> String {
let inputData = Data(input.utf8)
let hashedData = SHA256.hash(data: inputData)
let hashString = hashedData.compactMap {
return String(format: "%02x", $0)
}.joined()

return hashString
}
}

最佳答案

这实际上非常简单,只需将 appleSignInDelegates 和 currentNonce 切换为 @State 变量并将其从类切换为结构。

struct SignInWithAppleToFirebase: UIViewControllerRepresentable {
@State var appleSignInDelegates: SignInWithAppleDelegates! = nil
private let onLoginEvent: ((SignInWithAppleToFirebaseResponse) -> ())?
@State var currentNonce:String?

关于ios - 新的 Xcode UIViewControllerRepresentables 必须是模拟器的值类型而不是真实设备,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/73739109/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com