首页 > 解决方案 > Sign In with Apple 不适用于物理设备

问题描述

我在这个论坛上写信是因为我不知道他们是否可以帮助我。以前我写过一个类似的问题,但在探索我的代码时,我发现这是另一个问题,我不知道为什么。

在我的应用程序中,我正在实现使用 Apple 登录的方法,但问题出现在物理设备中,但在模拟器中效果很好。这个细节非常了不起,但我不知道为什么会这样;不知道是不是设备或者项目中的一些配置,如果可能是代码,我不知道。

附件截图

模拟器

设备

在模拟器中,我可以选择是否要与 Apple 共享我的登录电子邮件。

正如我之前所说,我不知道我做错了什么或者这些差异是由于什么造成的。

附上部分代码:

    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!
        }
    }

我希望你能支持我解决这个问题。感谢您的关注。

标签: iosswiftxcodeapple-id

解决方案


当您在模拟器上运行时,您实际上是在 Mac 上运行代码,并且应用程序可以访问您计算机上的所有信息。

在物理设备上运行时,您只能访问物理设备上的信息。

据我了解,使用 Apple 系统进行身份验证时,用户可以决定将他们的个人详细信息中的哪些(如果有)暴露给他们授权的服务。我怀疑在您的 Mac 上,您允许客户访问比您在手机上授权的信息更多的信息。

您的代码应该准备好只接收它在授权特定服务时返回的一些信息。


推荐阅读