Apple SignIn - toant-dev/toandev.github.io GitHub Wiki

APPLE SIGN IN

This is basic instructions to help us integrate Apple SignIn to iOS projects

Todos

I. Configure on Apple Developer

Enable “Sign In with Apple”
  1. Enable “Sign In with Apple” feature for your app bundle N|Solid
  2. Enable as a primary App ID for new app

If you're enabling an App ID for the first time or for a new app, enable the App ID as a primary.

Register domains and emails for communication

Select More section to register new one

In order to contact users that use Apple’s private email relay service, you must register email sources that your organization will use for communication.

Create a key for your API
  1. Select Keys section to register new one
  2. Save and provide some information to API team
  • CLIENT_ID: Bundle Id
  • TEAM_ID: Team Id
  • KEY_ID: View key detail on Developer page
  • PRIVATE_KEY: Downloaded .p8 file

II. Configure on your app

Add "Sign in with Apple" button

There are 2 colors of this button N

There are 2 ways to add this button:

  1. Using Built-in control To add this button, we can use the system class ASAuthorizationAppleIDButton
  2. Using customized button Use can use your owned design for this button but need following Apple Guideline You can download Apple icon at link
Enable Apple Sign In Capabilities

To add a capability to the app target, double-click the capability in the library or drag the capability from the library to the Signing & Capabilities pane. Select Sign In with Apple on the selection list

Make sure you added this capability to all your developing targets

Coding

There are 2 options:

  • Request Authorization with Apple ID (token)
  • Request Existing Credentials (saved username and password)
@available(iOS 13.0, *)
extension LoginPopupViewController: ASAuthorizationControllerDelegate {
    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()
    }
    
    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
            let token = String(data: appleIDCredential.identityToken!, encoding: .utf8)
            DispatchQueue.main.async {
                // TODO: Send token and required information to API
            }
        
        case let passwordCredential as ASPasswordCredential:
            let username = passwordCredential.user
            let password = passwordCredential.password
            DispatchQueue.main.async {
                // TODO: Sign in using an existing iCloud Keychain credential.
            }
            
        default:
            break
        }
    }
    
    func authorizationController(controller: ASAuthorizationController, didCompleteWithError error: Error) {
        print(error.localizedDescription)
    }
}

The user must enable Two-Factor Authentication to use Sign in with Apple so that access to the account is secure. The user's name (fullname, firstname, lastname) can get only one time For testing, we need to remove on device Settings: Apple ID > Password & Security > Apps using Apple ID

More information here