我的应用程序中,用户可以使用Apple登录。创建帐户很好(使用Firebase)。但是,在用户实际登录之前,我想检查用户是否已经注册。为此,我需要电子邮件地址。问题在于,由于出现found nil
错误,我无法正确地访问电子邮件。
这是我的函数
,用于处理检查/登录:
// delegate functions
func authorizationController(controller: ASAuthorizationController, didCompleteWithAuthorization authorization: ASAuthorization) {
if let appleIDCredential = authorization.credential 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 {
print("Unable to fetch identity token")
return
}
guard let idTokenString = String(data: appleIDToken, encoding: .utf8) else {
print("Unable to serialize token string from data: \(appleIDToken.debugDescription)")
return
}
let credential = OAuthProvider.credential(withProviderID: "apple.com", idToken: idTokenString, rawNonce: nonce)
print("email: " + appleIDCredential.email!)
let email = appleIDCredential.email
Auth.auth().fetchSignInMethods(forEmail: email!) { (methods, error) in
if error != nil {
// show error popUp
Utilities.showErrorPopUp(labelContent: "Fehler", description: error!.localizedDescription)
} else {
// no error -> check email adress
// Email is not registered -> sign up
if methods == nil {
print("signed in")
let usernameVC = self.storyboard?.instantiateViewController(withIdentifier: "UsernameVC") as! UserNameVC
usernameVC.credential = credential
usernameVC.signInOption = "apple"
self.navigationController?.pushViewController(usernameVC, animated: true)
}
// Email is registered -> login
else {
Auth.auth().signIn(with: credential, completion: { (user, error) in
if error != nil {
Utilities.showErrorPopUp(labelContent: "Fehler beim Login", description: error!.localizedDescription)
} else {
// set user status to logged-in
UserDefaults.standard.setIsLoggedIn(value: true)
UserDefaults.standard.synchronize()
// stop animation
self.logoAnimation.stop()
//transition to home
self.transitionToHome()
}
})
}
}
}
}
}
就像我所说的,如果用户已经登录,这个不起作用,它只在用户第一次注册时起作用,否则好像无法获取电子邮件。
有人知道解决此问题的变通方法或任何内容吗?
SignUpProcess
将无法完成。通过您提供的解决方案,如果用户使用苹果登录,但取消了用户名进程(例如重新启动应用程序),他仍然可以登录。如何检查用户是否真正创建成功呢? - Chris