0

我已经实现了自己的 AppleSignUpButton。它调用这段代码来执行实际的 Apple sig 微调器只是保持旋转。

当我按下取消时,此函数按预期调用:

func authorizationController(controller _: ASAuthorizationController, didCompleteWithError error: Error) {..}

所以我唯一的问题是这个:

func authorizationController(controller _: ASAuthorizationController, didCompleteWithAuthorization authorization: ASAuthorization) {..}

这是我处理 Apple Sign Up 的课程:

import AuthenticationServices
import CryptoKit
import Firebase
import FirebaseAuth
import Foundation

@objc class AppleAuthProvider: NSObject, AuthProvider {
    
    @Published var loggedInUser: FIRUser?
    
    var completion: ((Result<User, Error>) -> Void)?

    private var currentNonce: String?

    func signIn(completion: @escaping (Result<User, Error>) -> Void) {
        self.completion = completion

        startSignInWithAppleFlow()
    }

    func signOut(completion _: @escaping (Result<Void, Error>) -> Void) {
        ()
    }

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

        let authorizationController = ASAuthorizationController(authorizationRequests: [request])
        authorizationController.delegate = self
        authorizationController.performRequests()
    }

    private func randomNonceString(length: Int = 32) -> String {
        precondition(length > 0)
        let charset: [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 {
                    completion?(.failure(fatalError(
                        "Unable to generate nonce. SecRandomCopyBytes failed with OSStatus \(errorCode)"
                    )))
                }
                return random
            }

            randoms.forEach { random in
                if remainingLength == 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 {
            String(format: "%02x", $0)
        }.joined()

        return hashString
    }
}

extension AppleAuthProvider: ASAuthorizationControllerDelegate {
    func authorizationController(controller _: ASAuthorizationController, didCompleteWithAuthorization authorization: ASAuthorization) {
        if let appleIDCredential = authorization.credential as? ASAuthorizationAppleIDCredential {
            guard let nonce = currentNonce else {
                completion?(.failure(fatalError(
                    "Invalid state: A login callback was received, but no login request was sent."
                )))
                return
            }
            guard let appleIDToken = appleIDCredential.identityToken else {
                completion?(.failure(fatalError(
                    "Unable to fetch identity token"
                )))

                return
            }
            guard let idTokenString = String(data: appleIDToken, encoding: .utf8) else {
                completion?(.failure(fatalError(
                    "Unable to serialize token string from data: \(appleIDToken.debugDescription)"
                )))
                return
            }
            // Initialize a Firebase credential.
            let credential = OAuthProvider.credential(withProviderID: "apple.com",
                                                      idToken: idTokenString,
                                                      rawNonce: nonce)
            // Sign in with Firebase.
            Auth.auth().signIn(with: credential) { [weak self] _, error in
                if let error = error {
                    self?.completion?(.failure(error))
                } 
               // User is signed in to Firebase with Apple.
               // ...
            }
        }
    }

    func authorizationController(controller _: ASAuthorizationController, didCompleteWithError error: Error) {
        completion?(.failure(fatalError(
            "Sign in with Apple errored: \(error)"
        )))
    }
}
4

1 回答 1

0

它在模拟器上不起作用,但在真实设备上正常工作。

于 2022-01-28T22:06:30.440 回答