我在项目中集成了“使用Apple登录” 。但是在集成之后,尝试使用Apple ID登录时出现了奇怪的问题。主要问题是登录完成后出现键盘。
当我尝试登录时,要求进行两次身份验证,然后出现。
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
self.setupSOAppleSignIn()
}
func setupSOAppleSignIn() {
let btnAuthorization = ASAuthorizationAppleIDButton()
btnAuthorization.frame = CGRect(x: 0, y: 0, width: 200, height: 40)
btnAuthorization.center = self.view.center
btnAuthorization.addTarget(self, action: #selector(actionHandleAppleSignin), for: .touchUpInside)
self.view.addSubview(btnAuthorization)
}
@objc func actionHandleAppleSignin() {
let appleIDProvider = ASAuthorizationAppleIDProvider()
let request = appleIDProvider.createRequest()
request.requestedScopes = [.fullName, .email]
let authorizationController = ASAuthorizationController(authorizationRequests: [request])
authorizationController.delegate = self
authorizationController.presentationContextProvider = self
authorizationController.performRequests()
}
extension ViewController: ASAuthorizationControllerDelegate {
// ASAuthorizationControllerDelegate function for authorization failed
func authorizationController(controller: ASAuthorizationController, didCompleteWithError error: Error) {
print(error.localizedDescription)
}
// ASAuthorizationControllerDelegate function for successful authorization
func authorizationController(controller: ASAuthorizationController, didCompleteWithAuthorization authorization: ASAuthorization) {
if let appleIDCredential = authorization.credential as? ASAuthorizationAppleIDCredential {
// Create an account as per your requirement
let appleId = appleIDCredential.user
let appleUserFirstName = appleIDCredential.fullName?.givenName
let appleUserLastName = appleIDCredential.fullName?.familyName
let appleUserEmail = appleIDCredential.email
//Write your code
} else if let passwordCredential = authorization.credential as? ASPasswordCredential {
let appleUsername = passwordCredential.user
let applePassword = passwordCredential.password
//Write your code
}
}
}
extension ViewController: ASAuthorizationControllerPresentationContextProviding {
//For present window
func presentationAnchor(for controller: ASAuthorizationController) -> ASPresentationAnchor {
return self.view.window!
}
}
我用这两个教程来学习。
https://www.raywenderlich.com/4875322-sign-in-with-apple-using-swiftui
https://www.spaceotechnologies.com/sign-in-with-apple-ios-tutorial/