Tunko Development Diary

iOS Apple Login (FierBase 연동) 구현 A-Z까지 본문

Development/iOS 개발

iOS Apple Login (FierBase 연동) 구현 A-Z까지

Tunko 2020. 3. 24. 14:56

#iOS

  1. 앱 개발 설정 페이지 접속

  2. Sing in with Apple 체크 후 저장

     

  3. 도메인과 이메일 작성

  4. Firebase 프로젝트 콘솔 이동

  5. 로그인 제공업체에서 Apple 사용으로 설정.

  6. xcode 프로젝트 이동

  7. 애플 로그인 권한 설정

  8. 코드 추가

빌드 환경 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
    }
}
반응형
Comments