0

我在我的项目中集成了“使用 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/

问题的图片如下 主屏幕键盘

4

0 回答 0