-3

I write in this forum because I do not know if they can help me. Previously I wrote a similar question, but exploring my code I identify that it is another problem and I do not know why.

In my application I am implementing the method of logging in with Apple but the problem occurs in the physical device but in the simulator it works very well. This detail is very remarkable but I don't know why it is; I do not know if it is because of some configuration in the device or in the project, if maybe it can be the code, I do not know.

Attachment screenshots

Simulator

Device

In the simulator I have the options to choose whether or not I want to share my email for the Sign In with Apple.

As I said before, I don't know what I'm doing wrong or what these differences are due to.

Attached part of the code:

    import UIKit
    
    import AuthenticationServices
    
    @available(iOS 13.0, *)
    class ViewController: UIViewController {
    
       @IBOutlet weak var loginProviderStackView: UIStackView!
    
       override func viewDidLoad() {
        setupProviderLoginView()
         
        super.viewDidLoad()
       
      }
    
       @available(iOS 13.0, *)
      func setupProviderLoginView(){
         
        let authorizationButton = ASAuthorizationAppleIDButton()
        authorizationButton.addTarget(self, action: #selector(handleAuthorizationAppleIDButtonPress), for: .touchUpInside)
        self.loginProviderStackView.addArrangedSubview(authorizationButton)
         
      }
       
      @available(iOS 13.0, *)
      func performExistingAccountSetupFlows(){
         
        // Prepare requests for both Apple ID and password providers.
        let requests = [ASAuthorizationAppleIDProvider().createRequest(),
                ASAuthorizationPasswordProvider().createRequest()]
         
        // Create an authorization controller with the given requests.
        let authorizationController = ASAuthorizationController(authorizationRequests: requests)
        authorizationController.delegate = self
        authorizationController.presentationContextProvider = self
        authorizationController.performRequests()
         
      }
       
      @objc
      func handleAuthorizationAppleIDButtonPress() {
         
        let appleIDProvider = ASAuthorizationAppleIDProvider()
        let request = appleIDProvider.createRequest()
        request.requestedScopes = [.fullName, .email]
         
        let authorizationController = ASAuthorizationController(authorizationRequests: [request])
        authorizationController.delegate = self
        authorizationController.presentationContextProvider = self
        authorizationController.performRequests()
         
      }
    
    
    
    }
    
@available(iOS 13.0, *)
extension ViewController: ASAuthorizationControllerDelegate{
        
func authorizationController(controller: ASAuthorizationController, didCompleteWithAuthorization authorization: ASAuthorization) {
            
            switch authorization.credential{
            case let appleIDCredential as ASAuthorizationAppleIDCredential:
                
                let userIdentifier = appleIDCredential.user
                let fullName = appleIDCredential.fullName
                let email = appleIDCredential.email
                
                self.saveUserInKeychain(userIdentifier)
                self.showResultViewController(userIdentifier: userIdentifier, fullName: fullName, email: email)
                
            case let passwordCredential as ASPasswordCredential:
                
                let username = passwordCredential.user
                let password = passwordCredential.password
                
                DispatchQueue.main.async {
                    
                    self.showPasswordCredentialAlert(username: username, password: password)
            
                }
                
            default:
                break
            }
            
        }
        
private func saveUserInKeychain(_ userIdentifier: String) {
            do {
                try KeychainItem(service: "com.Gents", account: "userIdentifier").saveItem(userIdentifier)
            } catch {
                print("Unable to save userIdentifier to keychain.")
            }
        }
        
        
private func showResultViewController(userIdentifier: String, fullName: PersonNameComponents?, email: String?){
            
            DispatchQueue.main.async { [self] in
                
               //self.camposAppleID(mail: email!, nombre: fullName!, apellido: userIdentifier)
                
                let givenName = fullName?.givenName
                let familyName = fullName?.familyName
                let Mail = email
               // print("El givenName: ", givenName!)
                
             camposAppleID(mail: Mail!, nombre: givenName!, apellido: familyName!)
    
            }
            
        }
        
        private func showPasswordCredentialAlert(username: String, password: String) {
            let message = "The app has received your selected credential from the keychain. \n\n Username: \(username)\n Password: \(password)"
            let alertController = UIAlertController(title: "Keychain Credential Received",
                                                    message: message,
                                                    preferredStyle: .alert)
            alertController.addAction(UIAlertAction(title: "Dismiss", style: .cancel, handler: nil))
            self.present(alertController, animated: true, completion: nil)
        }
        
        func authorizationController(controller: ASAuthorizationController, didCompleteWithError error: Error) {
            print("Falló")
        }
        
    }
    
@available(iOS 13.0, *)
extension ViewController: ASAuthorizationControllerPresentationContextProviding {
        /// - Tag: provide_presentation_anchor
        func presentationAnchor(for controller: ASAuthorizationController) -> ASPresentationAnchor {
            return self.view.window!
        }
    }

I hope you can support me with this problem. Thanks for your attention.

Rodrigo
  • 17
  • 6
  • 1
    why did you create a duplicate of your own question? – Evgeny Karkan Oct 17 '21 at 19:33
  • 1
    Exact duplicate of your earlier question: [login with Apple ID: does not provide me with user data on a physical device](https://stackoverflow.com/questions/69591144/login-with-apple-id-does-not-provide-me-with-user-data-on-a-physical-device) – esqew Oct 17 '21 at 19:59
  • 1
    Edit your earlier question or ask a new user in that shows how you are actually requesting Sign In with Apple: e.g. show the scopes you are requesting. Is this an initial authentication or a subsequent authentication? You code doesn't appear to save the user identifier that will be needed for a subsequent authentication. – Paulw11 Oct 17 '21 at 20:05
  • I duplicated it because I thought the error was about the unwrapping of variables, but I saw that it was not. I see this is about using Apple ID login on physical device – Rodrigo Nov 10 '21 at 00:14

1 Answers1

0

When you are running on the Simulator, you are actually running the code on your Mac and the application has access to all the information on your computer.

When you run on a physical device, you only have access to the information on the physical device.

As I understand authentication using Apple's system, the user gets to decide what, if any, of their personal details are exposed to the service they are authorizing against. I suspect that on your Mac you have give permission for a client to have access to more information than you have authorized on your phone.

Your code should be prepared to receive only some of the information that it gets back when authorizing against a particular service.

Scott Thompson
  • 20,714
  • 4
  • 30
  • 32