티스토리 뷰

IOS/swift

iOS 애플 로그인 연동

내일도이렇게 2024. 1. 21. 14:52

1.  Project > Signining & Capablilities 

Sign in With Apple 추가 

2. Apple Developer - Certificates, Identifiers & Profiles 

애플 로그인 추가할 앱에 Sign In with Apple 추가 

 

3. 애플 로그인 버튼 추가 

 

4. 코드 구현

ASAuthorizationControllerPresentationContextProviding 

- 인증 컨트롤러를 어느 컨트롤러나 뷰에 표시할지 결정하는데 사용 

import AuthenticationServices

extension Root.ViewController: ASAuthorizationControllerPresentationContextProviding {
    func presentationAnchor(for controller: ASAuthorizationController) -> ASPresentationAnchor {
        return self.view.window!
    }
}
// 애플 로그인 버튼 실행 
appleLoginButton.rx.tap
         .bind {
            self.startSignInWithAppleFlow()
         }.disposed(by: disposeBag)

// 애플 로그인 요청 
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()
 }
    
 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
 }
    
  // Adapted from https://auth0.com/docs/api-auth/tutorials/nonce#generate-a-cryptographically-random-nonce
 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 remainingLength == 0 {
                    return
                }
                
                if random < charset.count {
                    result.append(charset[Int(random)])
                    remainingLength -= 1
                }
            }
        }
        
        return result
 }

 

ASAuthorizationControllerDelegate 

 - 사용자 인증에 대한 처리 

 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
            }
            
            let credential = OAuthProvider.credential(withProviderID: "apple.com", idToken: idTokenString, rawNonce: nonce)
            let credentialResult = try await Auth.auth().signIn(with: oauth)
            
            // ...
            
     }
 }

 

'IOS > swift' 카테고리의 다른 글

iOS 구글 로그인 연동  (0) 2024.01.21
[Swift] GCD ( DispatchQueue) 정리  (0) 2022.04.08
[Swift] 클로저와 메모리 관리  (0) 2022.04.06
[Swift] ARC  (0) 2022.04.05
[Swift] Method Dispatch 와 성능 최적화  (0) 2022.04.05
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/09   »
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
29 30
글 보관함