如何解决仅当我使用内置的SignInWithAppleButton时才会调用“使用Apple登录”对话框
我正在尝试使用自己的自定义样式来实现一个Sign in with Apple
按钮。我遵循与every tutorial相同的代码-但是使用自定义按钮时不会出现对话框。
有效的代码:
SignInWithAppleButton(
//Request
onRequest: { request in
let nonce = randomNonceString()
currentNonce = nonce
request.requestedScopes = [.fullName,.email]
request.nonce = sha256(nonce)
},//Completion
onCompletion: { result in
switch result {
case .success(let authResults):
switch authResults.credential {
case let appleIDCredential as ASAuthorizationAppleIDCredential:
guard let nonce = currentNonce else {
fatalError("Invalid state: A login callback was received,but no login request was sent.")
}
guard let appleIDToken = appleIDCredential.identityToken else {
fatalError("Invalid state: A login callback was received,but no login request was sent.")
}
guard let idTokenString = String(data: appleIDToken,encoding: .utf8) else {
print("Unable to serialize token string from data: \(appleIDToken.debugDescription)")
return
}
//Creating a request for firebase
let credential = OAuthProvider.credential(withProviderID: "apple.com",idToken: idTokenString,rawNonce: nonce)
//Sending Request to Firebase
Auth.auth().signIn(with: credential) { (authResult,error) in
if (error != nil) {
// Error. If error.code == .MissingOrInvalidNonce,make sure
// you're sending the SHA256-hashed nonce as a hex string with
// your request to Apple.
print(error?.localizedDescription as Any)
return
}
// User is signed in to Firebase with Apple.
print("SUCCESS")
}
//Prints the current userID for firebase
print("\(String(describing: Auth.auth().currentUser?.uid))")
default:
break
}
default:
break
}
}
)
不起作用的代码:
查看 AppleSignInButton() .onTapGesture { SignInWithAppleCoordinator()。getAppleRequest() }
协调员
final class SignInWithAppleCoordinator: NSObject {
fileprivate var currentNonce: String?
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 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 {
return String(format: "%02x",$0)
}.joined()
return hashString
}
func getAppleRequest(){
print("Begin request...")
let appleIDProvider = ASAuthorizationAppleIDProvider()
let nonce = randomNonceString()
currentNonce = nonce
let request = appleIDProvider.createRequest()
request.requestedScopes = [.fullName,.email]
request.nonce = sha256(nonce)
let authorizationController = ASAuthorizationController(authorizationRequests: [request])
authorizationController.delegate = self
authorizationController.performRequests()
print("Perform request...")
}
}
extension SignInWithAppleCoordinator: ASAuthorizationControllerDelegate {
func authorizationController(controller: ASAuthorizationController,didCompleteWithAuthorization authorization: ASAuthorization) {
print("Sign in to APPLE success") // THIS DOES NOT GET CALLED
}
func authorizationController(controller: ASAuthorizationController,didCompleteWithError error: Error) {
print("Sign in error APPLE ID") // THIS ALSO DOES NOT GET CALLED
}
}
知道什么问题吗?
我只是想复制内置按钮(SignInWithAppleButton
)的成功登录信息,但是我需要在我的界面上自定义样式,因此需要对我的按钮执行action {}
自定义样式的按钮。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。