반응형
Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 |
Tags
- subject
- nonisolated
- URL(string:)
- NullObject
- RxSwift
- init
- @State
- dismiss
- swift6
- Operators
- SwiftUI
- graphql
- RFC1738/1808
- Xcode
- IOS
- operator
- typeorm
- @Environment
- init?
- SWIFT
- nestjs
- Creating Operators
- Bug
- RxCocoa
- @Binding
- vim
- Operater
- ios14
- @EnvironmentObject
- NavigationLink
Archives
- Today
- Total
Tunko Development Diary
iOS Apple Login (FierBase 연동) 구현 A-Z까지 본문
#iOS
-
앱 개발 설정 페이지 접속
-
Sing in with Apple 체크 후 저장
-
도메인과 이메일 작성
-
Firebase 프로젝트 콘솔 이동
-
로그인 제공업체에서 Apple 사용으로 설정.
-
xcode 프로젝트 이동
-
애플 로그인 권한 설정
-
코드 추가
빌드 환경 iOS 11.0
// 추가
import AuthenticationServices
import CryptoKit
// 스토리 보드에 만들어둔 UIView! 미리 바인딩
@IBOutlet var appleSignInView: UIView!
override func viewDidLoad()
{
if #available(iOS 13.0, *)
{
appleView.isHidden = false
appleLoginButton.isHidden = false
}
else // 13.0 부터 Apple 로그인을 지원하므로 이부분은 13.0 미만 버전 빌드시 UI 조정을 위한 코드
{
let subHeight : CGFloat = 54
boxHeight.constant = boxHeight.constant - subHeight
boxInViewHeight.constant = boxInViewHeight.constant - subHeight
appleButtonTopHeight.constant = 0
appleButtonHeight.constant = 0
appleView.isHidden = true
appleLoginButton.isHidden = true
}
}
@available(iOS 13.2, *)
private func setupLoginWithAppleButton()
{
let signInWithAppleButton = ASAuthorizationAppleIDButton(authorizationButtonType: .default, authorizationButtonStyle: .black)
signInWithAppleButton.layer.frame = CGRect(x: 0, y: 0, width: facebookButton.layer.preferredFrameSize().width, height: facebookButton.layer.preferredFrameSize().height)
signInWithAppleButton.cornerRadius = 5
signInWithAppleButton.addTarget(self, action: #selector(signInWithAppleButtonPressed), for: .touchUpInside)
appleSignInView.addSubview(signInWithAppleButton)
}
@available(iOS 13, *)
@objc private func signInWithAppleButtonPressed()
{
if !checkLoing()
{
return
}
startSignInWithAppleFlow()
}
@available(iOS 13.0, *)
extension LoginViewController: ASAuthorizationControllerPresentationContextProviding, ASAuthorizationControllerDelegate{
func presentationAnchor(for controller: ASAuthorizationController) -> ASPresentationAnchor {
return view.window!
}
func authorizationController(controller: ASAuthorizationController, didCompleteWithAuthorization authorization: ASAuthorization) {
if let appleIDCredential = authorization.credential as? ASAuthorizationAppleIDCredential {
guard let nonce = currentNonce else {
fatalError("Invalid state: A login callback was received, but no login request was sent.")
}
guard let appleIDToken = appleIDCredential.identityToken else {
print("Unable to fetch identity token")
return
}
guard let idTokenString = String(data: appleIDToken, encoding: .utf8) else {
print("Unable to serialize token string from data: \(appleIDToken.debugDescription)")
return
}
// Initialize a Firebase credential.
let credential = OAuthProvider.credential(withProviderID: "apple.com",
idToken: idTokenString,
rawNonce: nonce)
// Sign in with Firebase.
Auth.auth().signIn(with: credential) { (authResult, error) in
if (error != nil) {
// Error. If error.code == .MissingOrInvalidNonce, make sure
// you're sending the SHA256-hashed nonce as a hex string with
// your request to Apple.
print(error!.localizedDescription)
return
}
// User is signed in to Firebase with Apple.
// ...
let email = Auth.auth().currentUser?.email
//print("Auth.auth().currentUser.email : \(email)")
// 앱서버에서 로그인 처리
self.appleLoginCallback(email: email!)
}
}
}
func authorizationController(controller: ASAuthorizationController, didCompleteWithError error: Error) {
// Handle error.
print("Sign in with Apple errored: \(error)")
}
// fierbase iOS 로그인 가이드
private func randomNonceString(length: Int = 32) -> String
{
precondition(length > 0)
let charset: Array<Character> =
Array(“0123456789ABCDEFGHIJKLMNOPQRSTUVXYZabcdefghijklmnopqrstuvwxyz-._”)
var result = “”
var remainingLength = length
while remainingLength > 0 {
let randoms: [UInt8] = (0 ..< 16).map { _ in
var random: UInt8 = 0
let errorCode = SecRandomCopyBytes(kSecRandomDefault, 1, &random)
if errorCode != errSecSuccess {
fatalError("Unable to generate nonce. SecRandomCopyBytes failed with OSStatus \(errorCode)")
}
return random
}
randoms.forEach { random in
if length == 0 {
return
}
if random < charset.count {
result.append(charset[Int(random)])
remainingLength -= 1
}
}
}
return result
}
@available(iOS 13, *)
func startSignInWithAppleFlow() {
let nonce = randomNonceString()
currentNonce = nonce
let appleIDProvider = ASAuthorizationAppleIDProvider()
let request = appleIDProvider.createRequest()
request.requestedScopes = [.fullName, .email]
request.nonce = sha256(nonce)
let authorizationController = ASAuthorizationController(authorizationRequests: [request])
authorizationController.delegate = self
authorizationController.presentationContextProvider = self
authorizationController.performRequests()
}
@available(iOS 13, *)
private func sha256(_ input: String) -> String {
let inputData = Data(input.utf8)
let hashedData = SHA256.hash(data: inputData)
let hashString = hashedData.compactMap {
return String(format: “%02x”, $0)
}.joined()
return hashString
}
}
반응형
'Development > iOS 개발' 카테고리의 다른 글
[iOS][Swift] 화면 터치 활성화, 비활성화 하기 (0) | 2021.02.13 |
---|---|
pod FirebaseCore 6.6.5 업데이트 이후 발생하는 Messaging 참조에러 (0) | 2020.03.25 |
dyld: Library not loaded: /System/Library/Frameworks/CryptoKit.framework/CryptoKit 에러 (0) | 2020.03.24 |
Xcode 에서 Vim 사용하기[2] (3) | 2020.01.03 |
# Xcode 에서 Vim 사용하기 [1] (0) | 2020.01.03 |
Comments