0

我想用我自己的设计创建我自己的 SignInWithAppleButton,当然要遵循指导方针。

我在将 startSignInWithAppleFlow 函数从 Swift 转换为 SwiftUI 时遇到问题。

struct ContentView: View {
    
    var body: some View {
        ...
        AuthButton(title: "Connect with Apple", iconName: "appleIcon") {
            authViewModel.signInWithApple()
        }
        ...
    }
}

struct AuthButton: View {
    let title: String
    let iconName: String
    let action: () -> Void

    var body: some View {
        Button {
            action()
        } label: {
            RoundedRectangle(cornerRadius: 16)
                .fill(.clear)
                .frame(height: 80)
                .overlay(
                    Text(title)
                        .font(.regular16)
                        .foregroundColor(.black)
                )
                .overlay(
                    HStack {
                        HorizontalSpacer(width: 32)
                        Image(iconName)
                            .resizable()
                            .frame(width: 30, height: 30, alignment: .center)
                        Spacer()
                    }
                )
        }
        .buttonStyle(AuthButtonStyle())
    }
}

class AuthenticationViewModel: ObservableObject {
    func signInWithApple() {
        Unclear what should be here!
    }

    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 {
                    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
    }
}
4

1 回答 1

1

看起来您正处于文档的第 2 步。

您的函数如下所示:

func signInWithApple() {
  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()
}
于 2022-01-27T21:30:56.877 回答