Contents

iOS App Integration with Apple ID Login (Sign in with Apple) Complete Guide

iOS App Integration with Apple ID Login (Sign in with Apple) Complete Guide

Apple ID Login (Sign in with Apple) provides users with a secure and convenient identity authentication method. This article will take a professional perspective, combined with practical development experience, to organize the integration process and key configurations, helping you efficiently complete the Apple ID login functionality.


I. Overall Process Overview

  1. Enable Sign in with Apple capability in Apple Developer portal
  2. Configure related Capability in Xcode project
  3. Implement Apple ID login code logic
  4. (Optional) Server-side verification and user information management

II. Detailed Configuration Steps

1. Apple Developer Portal Configuration

  • Log in to Apple Developer.
  • Go to “Certificates, Identifiers & Profiles”.
  • Find your App Identifier (Bundle ID) and click to enter details.
  • In the Capabilities list, check Sign In with Apple.
  • Save settings and regenerate/download the Provisioning Profile.

Analogy:
Like registering with property management first to get access permissions.


2. Xcode Project Configuration

  • Open Xcode and select your project Target.
  • Switch to the Signing & Capabilities tab.
  • Click the “+ Capability” button in the top left corner and add Sign In with Apple.
  • Confirm that the Bundle Identifier matches the developer portal.
  • Sign with the newly downloaded provisioning profile.

Analogy:
Installing access control equipment at your front door, matching the key with the house number.


3. Info.plist Configuration

  • Generally no need to manually add special fields, Xcode will handle it automatically.
  • If there are special requirements, you can add permission descriptions in Info.plist (such as NSFaceIDUsageDescription).

4. Code Implementation

  • Import the AuthenticationServices framework.
  • Add an ASAuthorizationAppleIDButton button to the login interface.
  • Implement ASAuthorizationControllerDelegate and ASAuthorizationControllerPresentationContextProviding protocols to handle login callbacks.

Sample code snippet:

import AuthenticationServices

// Add Apple login button
let appleIDButton = ASAuthorizationAppleIDButton()
view.addSubview(appleIDButton)
appleIDButton.addTarget(self, action: #selector(handleAuthorizationAppleIDButtonPress), for: .touchUpInside)

// Handle button click
@objc func handleAuthorizationAppleIDButtonPress() {
    let request = ASAuthorizationAppleIDProvider().createRequest()
    request.requestedScopes = [.fullName, .email]
    let controller = ASAuthorizationController(authorizationRequests: [request])
    controller.delegate = self
    controller.presentationContextProvider = self
    controller.performRequests()
}

// Implement delegate methods to handle login results
extension ViewController: ASAuthorizationControllerDelegate, ASAuthorizationControllerPresentationContextProviding {
    func authorizationController(controller: ASAuthorizationController, didCompleteWithAuthorization authorization: ASAuthorization) {
        if let credential = authorization.credential as? ASAuthorizationAppleIDCredential {
            // Get user unique identifier, Token and other information
            let userID = credential.user
            let email = credential.email
            // ...subsequent logic
        }
    }
    func presentationAnchor(for controller: ASAuthorizationController) -> ASPresentationAnchor {
        return self.view.window!
    }
}

5. (Optional) Server-side Verification

  • After obtaining the identityToken returned by Apple, it’s recommended to send it to your own server for verification to ensure security.
  • The server can use JWT toolkit to decrypt and verify the Token.

III. Common Issues and Considerations

  • Must use real device for debugging, simulators do not support Apple ID login.
  • App must enable HTTPS to ensure data transmission security.
  • Information obtained from first login and subsequent logins differs, first time you can get email and name, subsequent times only userID.
  • Recommend guiding users to bind in-app accounts to avoid data loss.

IV. Summary

The integration process for Apple ID login includes enabling capabilities in the developer portal, Xcode configuration, code implementation, and (optional) server-side verification. Each step is like the interlocking components of an access control system - only when all are configured correctly can users successfully “swipe in”.

If you need to learn more about advanced topics like server-side verification and user data synchronization, feel free to leave a comment for discussion!