Search code examples
iosswiftkeychainuser-dataapple-sign-in

How to get credentials on signIn with Apple in swift


I know this question is not asked first time but I am stuck... I tried sign in with apple and having problem getting user data, my code is as following

import UIKit
import AuthenticationServices
class SignInWithAppleVC: UIViewController {
        
    override func viewDidLoad() {
        super.viewDidLoad()
        setupProviderLoginView()
    }
    
    func setupProviderLoginView() {
        let appleButton = ASAuthorizationAppleIDButton()
        
        appleButton.frame = CGRect(x: 20, y: (Int(UIScreen.main.bounds.size.height) - 150), width: (Int(UIScreen.main.bounds.size.width) - 40), height: 50)
        appleButton.addTarget(self, action: #selector(signInWithApple), for: .touchUpInside)
        self.view.addSubview(appleButton)
            }
    
    @objc func signInWithApple() {
        print("sign in with apple clicked")
        let appleIdProvider = ASAuthorizationAppleIDProvider()
        let request = appleIdProvider.createRequest()
        request.requestedScopes = [.email,.fullName]
        let authorizationController = ASAuthorizationController(authorizationRequests: [request])
        authorizationController.delegate = self
        authorizationController.presentationContextProvider = self
        authorizationController.performRequests()
    }
}

extension SignInWithAppleVC : ASAuthorizationControllerDelegate {
    
    func authorizationController(controller: ASAuthorizationController, didCompleteWithError error: Error) {
        let alert = UIAlertController(title: "ERROR", message: "\(error.localizedDescription)", preferredStyle: .alert)
        alert.addAction(UIAlertAction(title: "OK", style: .cancel, handler: nil))
        present(alert, animated: true, completion: nil)
    }
    
    func authorizationController(controller: ASAuthorizationController, didCompleteWithAuthorization authorization: ASAuthorization) {
        switch authorization.credential {
        case let credentials as ASAuthorizationAppleIDCredential:
            print(credentials.user)             // Here we get results only first time
            print(credentials.fullName?.givenName!)//
            print(credentials.fullName?.familyName)//
        
        //MARK: - Use credentials
        case let credintials as ASPasswordCredential :
            print(credintials.password)
        default:
            let alert = UIAlertController(title: "Apple SgnIn", message: "Something went wrong", preferredStyle: .alert)
            alert.addAction(UIAlertAction(title: "OK", style: .cancel, handler: nil))
            present(alert, animated: true, completion: nil)
        }
    }
}

extension SignInWithAppleVC : ASAuthorizationControllerPresentationContextProviding {
    
    func presentationAnchor(for controller: ASAuthorizationController) -> ASPresentationAnchor {
        return view.window!
    }
}

The problem is, I gets email, given name, family name only on first attempt. but on each next time I am getting nil in response. Do I need to access Keychain for user date,(if yes then How plz) or any other solution?. Any help, link, example code will be warmly appreciated. Thanks in advance


Solution

  • As per the Apple Documentation, you will get user identifier, name and email address info on first attempt only and you need to store that information either on the server or in your local database to access it next time.

    New time, when you login with the same account then you will get user identifier only and you need to fetch other information from database.

    Reference Apple Documentation : Link