Overview

AuthenticationServices 프레임워크를 사용해서 Apple ID 를 사용한 회원가입, 로그인을 가능하게 하는 UI 구성

Add a Sign in with Apple Button

sample app 에서 LoginViewControllerASAuthorizationAppleIDButton 프로퍼티를 사용해서 Sign in with Apple 버튼 추가

func setupProviderLoginView() {
    let authorizationButton = ASAuthorizationAppleIDButton()
    authorizationButton.addTarget(self, action: #selector(handleAuthorizationAppleIDButtonPress), for: .touchUpInside)
    self.loginProviderStackView.addArrangedSubview(authorizationButton)
}

Request Authorization with Apple ID

위 코드대로 구현했을 때, 버튼을 누르면 handleAuthorizationAppleIDButtonPress 메소드가 실행됨

이 메소드는 유저의 이름과 이메일을 묻는 authentication request 를 요구하고, 유저가 Apple ID 에 로그인 되어 있는지 확인함

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

그 후 authorization controller 가 presentationAnchor(for:) 메소드를 호출해 애플로그인 모달을 띄움

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

기기에 Apple ID 로 로그인 되어 있으면 인증 절차만 띄우고, 로그인 되어 있지 않다면 로그인 절차 모두 띄움