๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๐ŸŽ iOS/SwiftUI

[iOS] SwiftUI - SignInWithAppleButton / ์• ํ”Œ ๋กœ๊ทธ์ธ ์ปค์Šคํ…€ ๋ฒ„ํŠผ

by Danna 2023. 10. 22.
728x90
728x90

structure SignInWithAppleButton

https://developer.apple.com/documentation/authenticationservices/signinwithapplebutton
SwiftUI / iOS 14.0 +

์„ค์ •ํ•  ์ˆ˜ ์žˆ๋Š” ๊ฒƒ

  • Labels
    • continue
    • signIn
    • signUp

  • Styles
    • black
    • white
    • whiteOutline

Sample code

SignInWithAppleButton(.continue) { request in
    request.requestedScopes = [.fullName, .email]
} onCompletion: { result in
    switch result {
        case .success(let authResults):
        print("Authorisation successful: \(authResults.credential)")
        case .failure(let error):
            print("Authorisation failed: \(error.localizedDescription)")
    }
}
.signInWithAppleButtonStyle(.black)
 

๐Ÿ™ˆ TMI

๋””์ž์ธ์ด๋‚˜ ๋ฌธ๊ตฌ๋ฅผ ๋งž์ถ”๊ธฐ ์œ„ํ•ด์„œ ๊ฒฐ๊ตญ ์ปค์Šคํ…€ ๋ฒ„ํŠผ์„ ์ด์šฉํ–ˆ๋‹ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค
์ฐธ๊ณ ํ•œ ์‚ฌ์ดํŠธ : https://www.kodeco.com/4875322-sign-in-with-apple-using-swiftui#toc-anchor-003

  • View
CommonButton(
    style: .login(backgroundColor: .black),
    imageName: "ic_apple",
    title: "Apple๋กœ ๊ณ„์†ํ•˜๊ธฐ"
) {
    interactor.requestAppleLogin()
}
.onReceive(userState.$isLoggedIn) { isLoggedIn in
    self.isLoggedIn = isLoggedIn
}
 
  • Interactor (๋กœ์ง)
import Foundation
import NaverThirdPartyLogin
import AuthenticationServices

final class DefaultLoginInteractor: NSObject, LoginInteractor {

    func requestAppleLogin() {
        let request = ASAuthorizationAppleIDProvider().createRequest()
        request.requestedScopes = [.fullName, .email]
      
        let authorizationController = ASAuthorizationController(authorizationRequests: [request])
        authorizationController.delegate = self
        authorizationController.performRequests()
    }
    
    private func processAppleLogin(_ authorization: ASAuthorization) {
        switch authorization.credential {
        case let appleIDCredential as ASAuthorizationAppleIDCredential:
            guard let authorizationCode = appleIDCredential.authorizationCode else { return }
            let fullName = appleIDCredential.fullName
            let name = "\(fullName?.familyName ?? "") \(fullName?.givenName ?? "")"
            let request = AppleAuthRequest(name: name, authorizationCode: authorizationCode)
            Task {
                let response = await repository.postAppleLogin(request)
                await userState.save(accessToken: response?.accessToken)
            }
        default:
            return
        }
    }
}

extension DefaultLoginInteractor: ASAuthorizationControllerDelegate {
    func authorizationController(controller: ASAuthorizationController, didCompleteWithAuthorization authorization: ASAuthorization) {
        processAppleLogin(authorization)
    }
    
    func authorizationController(controller: ASAuthorizationController, didCompleteWithError error: Error) {
        // ์—๋Ÿฌ์ฒ˜๋ฆฌ
    }
}
728x90
728x90