当前位置:网站首页>Sign in with apple

Sign in with apple

2020-11-10 10:44:16 Landen

   stay iOS13 in , If Apple developers provide any other third-party login , You have to provide “ Apple login ” Options . in other words , If the software requires “ Wechat login ” or “QQ Sign in ” when , It has to be provided at the same time “ Apple login ” The user can choose by themselves . According to Apple's latest guidelines , Ask developers to log in to the app interface of Apple terminal , take “ Apple login ” Options are listed above any other third-party login options .

   notes : The following is from the official document translation Sign in with Apple

  1、 Add login button

   In the example application ,LoginViewController Show a login form and a use in its view hierarchy Apple Button (ASAuthorizationAppleIDButton) Login form . The view controller also adds itself to the target of the button , And pass an operation , Call when the button receives a decoration event .
func setupProviderLoginView() {
    let authorizationButton = ASAuthorizationAppleIDButton()
    authorizationButton.addTarget(self, action: #selector(handleAuthorizationAppleIDButtonPress), for: .touchUpInside)
    self.loginProviderStackView.addArrangedSubview(authorizationButton)
}

   Tips : When you add the apple button to the storyboard , You also have to be in Xcode The class value of the control is set to ASAuthorizationAppleIDButton.

  2、 request Apple ID to grant authorization

   When the user clicks Apple Button login , View controller calls handleAuthorizationAppleIDButtonPress() function , This function starts the authentication process by executing authorization requests to the full name and email address of the user . then , The system checks to see if the user is using Apple ID Sign in . If the user is not logged in at the system level , The application will display a warning , Instructs users to use their Apple ID Sign in .

@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()
}

   Tips : The user is using “ Login with apple ”(Sign in with Apple) when , Dual authentication must be enabled , So that you can safely access the account .

   Authorization controller calls presentationAnchor(for:) Function to get the window from the application , In the window , It will show users a login with apple content in the form of a modal form .

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

   If the user uses Apple ID Log in at the system level , The description of the use of Apple Function login worksheet , And then there's another worksheet , Allow users to edit information in their accounts . Users can edit their first and last names , Choose another email address as their contact information , And hide their email address from the application . If the user chooses to hide their email address from the application , Apple will generate a proxy email address to forward the email to the user's private email address . Last , User input Apple ID Password , And then click Continue Create an account .

  3、Handle User Credentials

   If the validation is successful , Authorization controller calls authorizationController(controller:didCompleteWithAuthorization:) Delegate function , The application uses this function to store user data in the key chain .

func authorizationController(controller: ASAuthorizationController, didCompleteWithAuthorization authorization: ASAuthorization) {
    switch authorization.credential {
    case let appleIDCredential as ASAuthorizationAppleIDCredential:
        
        // Create an account in your system.
        let userIdentifier = appleIDCredential.user
        let fullName = appleIDCredential.fullName
        let email = appleIDCredential.email
        
        // For the purpose of this demo app, store the `userIdentifier` in the keychain.
        self.saveUserInKeychain(userIdentifier)
        
        // For the purpose of this demo app, show the Apple ID credential information in the `ResultViewController`.
        self.showResultViewController(userIdentifier: userIdentifier, fullName: fullName, email: email)
    
    case let passwordCredential as ASPasswordCredential:
    
        // Sign in using an existing iCloud Keychain credential.
        let username = passwordCredential.user
        let password = passwordCredential.password
        
        // For the purpose of this demo app, show the password credential as an alert.
        DispatchQueue.main.async {
            self.showPasswordCredentialAlert(username: username, password: password)
        }
        
    default:
        break
    }
}

   In your implementation ,ASAuthorizationControllerDelegate.authorizationController(controller:didCompleteWithAuthorization:) The delegate function should create an account in your system using the data contained in the user identifier .

   If authentication fails , Authorization controller calls authorizationController(controller:didCompleteWithError:) Delegate functions to handle errors .

func authorizationController(controller: ASAuthorizationController, didCompleteWithError error: Error) {
    // Handle error.
}

   Once the system has verified the user , The application will show ResultViewController, It will display the user information requested by the framework , Include the full name and email address provided by the user . The view controller also displays a Sign Out Button , And store the user data in the key chain . When the user clicks Sign Out Button , The application removes user information from the view controller and key chain , And will LoginViewController Present to the user .

  4、 Ask for existing credentials

  performexistingaccountsetupflows() The delta function is going to be : By asking for Apple ID and iCloud Key chain password to check whether the user has an existing account . Be similar to handleAuthorizationAppleIDButtonPress(), The authorization controller sets its presentation content provider and LoginViewController Delegation of objects .

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()
}

 

版权声明
本文为[Landen]所创,转载请带上原文链接,感谢