Sign In with Apple: How to achieve it for existing app?

我只是一个虾纸丫 提交于 2021-02-18 22:01:36

问题


With recent major updates released by Apple on 3rd June 2019, there is one feature Sign In with Apple. Information about 'how to use this' in app is available but I can see any sample source code, how to achieve this feature in your existing iOS app.

I'm looking for a sample source code, as I can't understand how to start with this.

And what I've tried: Sign In with Apple


回答1:


First step is you need to import AuthenticationServices

How to check that user credential state

        let appleIDProvider = ASAuthorizationAppleIDProvider()
        appleIDProvider.getCredentialState(forUserID: KeychainItem.currentUserIdentifier) { (credentialState, error) in
            switch credentialState {
            case .authorized:
                // The Apple ID credential is valid.
                break
            case .revoked:
                // The Apple ID credential is revoked.
                break
            case .notFound:
                // No credential was found, so show the sign-in UI.
               
            default:
                break
            }
        }

How to create Login with Apple

Step1

Prompts the user if an existing iCloud Keychain credential or Apple ID credential is found. implement ASAuthorizationControllerDelegate to

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

extension ViewController: ASAuthorizationControllerDelegate {
    func authorizationController(controller: ASAuthorizationController, didCompleteWithAuthorization authorization: ASAuthorization) {
        if let appleIDCredential = authorization.credential as? ASAuthorizationAppleIDCredential {
         //here is credentials . 
        }
    }
}

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

Step2:

create button

    let authorizationButton = ASAuthorizationAppleIDButton()
    authorizationButton.addTarget(self, action: #selector(handleAuthorizationAppleIDButtonPress), for: .touchUpInside)

Step3

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

Availability : iOS 13 or higher

Demo Application: A complete working demo application with keychain integration available on Github - https://github.com/developerinsider/Sign-In-with-Apple-Demo

Note: I will update answer with more and more useful information soon.

Hope it is helpful .



来源:https://stackoverflow.com/questions/56442928/sign-in-with-apple-how-to-achieve-it-for-existing-app

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!