๐ iOS/SwiftUI
[iOS] SwiftUI - SignInWithAppleButton / ์ ํ ๋ก๊ทธ์ธ ์ปค์คํ ๋ฒํผ
Danna
2023. 10. 22. 18:27
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