Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 658
Krunal Avatar asked Jun 04 '19 11:06

Krunal


People also ask

How do I allow Apple to sign in to my apps?

On your iPhone, iPad, iPod touch, or Apple WatchOpen the Settings app, then tap your name. Tap Password & Security. Tap Apps Using Apple ID.

What does it mean to sign into an app with Apple?

About Sign in with Apple When you see a Sign in with Apple button on a participating app or website, it means you can set up an account using your Apple ID. No need to use a social media account, fill out forms, or choose another new password.

How do I turn on Sign in with Apple?

Choose Apple menu  > System Preferences. Click Sign In. Enter your Apple ID and password. If prompted, enter the six-digit verification code sent to your trusted device or phone number and complete sign in.

Which apps have Sign in with Apple?

To use Sign in with Apple, download and open an app that supports the feature. A few apps you can try are Adobe Reader, Airbnb, Dropbox, eBay, Groupon, Instacart, Kayak, The New York Times, Squarespace, Spotify, TikTok, and WordPress.


1 Answers

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 .

like image 99
Prashant Tukadiya Avatar answered Nov 15 '22 05:11

Prashant Tukadiya