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
'๐ iOS > SwiftUI' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[iOS] SwiftUI - GeometryReader / View ํฌ๊ธฐ์ ์ขํ ๊ณ์ฐํ๊ธฐ (1) | 2023.10.21 |
---|---|
[iOS/SwiftUI] @Environment ์์๋ณด๊ธฐ / ๋ชจ๋ฌ ๋ทฐ ๋ซ๊ธฐ dismiss ํ์ฉ (0) | 2023.06.15 |
[iOS/SwiftUI] @ViewBuilder, @resultBuilder ๊ฐ ๋ฌด์์ผ๊น? (0) | 2023.06.07 |
[iOS] SwiftUI LazyVStack LazyHStack :: lazy load views (1) | 2022.06.07 |
[iOS] SwiftUI property wrapper @State @Binding (0) | 2022.06.02 |