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
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.