Sure, Why not?

Google 로그인 본문

💻

Google 로그인

joho2022 2025. 3. 21. 12:00

 

iOS 디자인 가이드

 

 

 

 

iOS 및 macOS용 Google 로그인 시작하기  |  Authentication  |  Google for Developers

Important: Starting May 1, 2024, Apple requires Privacy Manifests and signatures for iOS applications that use commonly-used SDKs, including GoogleSignIn-iOS. Upgrade to GoogleSignIn-iOS v7.1.0+ before May 1, 2024. Follow our upgrade guide. 이 페이지

developers.google.com

 

GoogleSignIn 종속성 설치

https://github.com/google/GoogleSignIn-iOS

 

SPM로 설치하였다.

 

 

OAuth 클라이언트 ID 가져오기

 

파란색 버튼 눌러서 ID 생성해준다.

그리고 credentials.plist 다운받아서

클라이언트 ID와 스키마 주소를 확인한다.

 

(혹여나 다운 못받아도 흰색버튼 누르고 클라이언트에서 OAuth 클라이언트 다운로드에서 plist파일 받을 수 있음)

 

역시나 노출이 안되는 정보이기 때문에 환경변수로 관리를 한다. 

 

URL 스키마 설정

 

그리고 Info.plist에서는

key값인 GIDClientID와

value값인 client ID를 넣어준다.

Info.plist 설정

 

 

이러면 초기세팅은 끝났다!

 


 

이제 코드를 작성해준다!

onOpenURL 추가

카카오로그인 때처럼 추가를 해줘야 한다.

@main
struct MyApp: App {

  var body: some Scene {
    WindowGroup {
      ContentView()
        // ...
        .onOpenURL { url in
          GIDSignIn.sharedInstance.handle(url)
        }
    }
  }
}

 

결국 URL 스키마 설정과 onOpenURL 추가는 모두 딥링크 처리를 위한 작업이다.

URL 스키마는 앱 외부에서 특정 화면으로 이동하거나, 소셜 로그인 후 앱으로 돌아오는 경로를 지정하는 딥링크 방식인 것.

onOpenURL 은 해당 URL를 감지하고 알맞은 화면이나 로직을 실행해준다.

 

두 가지 설정을 통해 앱이 외부에서 전달된 URL을 통해 원하는 화면으로 이동하도록 돕는 핵심 요소이다 ~

 

 

GoogleLoginAdapter

final class GoogleLoginAdapter: SocialLoginService {
    
    @MainActor
    func login() async throws -> UserInfo {
        return try await withCheckedThrowingContinuation { continuation in
            guard let windowScene = UIApplication.shared.connectedScenes.first as? UIWindowScene,
                  let presentingViewController = windowScene.windows.first?.rootViewController else {
                continuation.resume(throwing: AuthError.loginFailed(service: .google))
                return
            }
            
            GIDSignIn.sharedInstance.signIn(withPresenting: presentingViewController) { signInResult, error in
                if let error = error {
                    print("==== Google Login Error ====")
                    print("Error: \(error.localizedDescription)")
                    print("==============================")
                    
                    continuation.resume(throwing: AuthError.loginFailed(service: .google))
                    return
                }
                
                guard let user = signInResult?.user else {
                    continuation.resume(throwing: AuthError.userInfoNotFound(service: .google))
                    return
                }
                
                let userInfo = UserInfo(
                    id: user.userID ?? "",
                    name: user.profile?.name ?? "Unknown",
                    email: user.profile?.email ?? "Unknown"
                )
                continuation.resume(returning: userInfo)
            }
        }
    }
    
    @MainActor
    func logout() async throws {
        GIDSignIn.sharedInstance.signOut()
    }
    
    func getServiceName() -> String {
        return "Google Auth"
    }
}

 

 

결과

 

Good~ gle

 


 

확실히 초기세팅하고

어댑터만 구현해놓으면 소셜로그인을 추가하는 작업이 

정말 손 쉽게 완성되는 것 같다.

 

 

 

GitHub - joho2022/LoginPractice

Contribute to joho2022/LoginPractice development by creating an account on GitHub.

github.com

 

'💻' 카테고리의 다른 글

SwiftUI) Custom TextField Placeholder  (2) 2025.04.01
Naver 로그인  (0) 2025.03.22
Apple 로그인  (0) 2025.03.19
kakao 로그인  (0) 2025.03.18
Deadlock  (1) 2025.03.02