반응형

여러번의 구글 로그인을 iOS 에서 구현했으나 할때마다 뭔가 동일하게 구현이 안되는듯 하는 느낌이여서 한번 정리를 해둔다.

 

Google 클라우드 콘솔에서 프로젝트 생성 키 발급 등은 

https://developers.google.com/identity/sign-in/ios/start-integrating?hl=ko 

 

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

이 페이지는 Cloud Translation API를 통해 번역되었습니다. Switch to English 의견 보내기 iOS 및 macOS용 Google 로그인 시작하기 컬렉션을 사용해 정리하기 내 환경설정을 기준으로 콘텐츠를 저장하고 분류

developers.google.com

에서 주는 가이드를 따라하고,

 

SPM 을 통해 패키지를 설치하면 라이브러리 인식이 안되는 이슈가 종종 발생해서 Pod install 을 통해 패키지를 설치했다.

 

  1. pod 'GoogleSignIn'

info.plist에

<key>GIDClientID</key>
<string>YOUR_IOS_CLIENT_ID</string>
<key>CFBundleURLTypes</key>
<array>
  <dict>
    <key>CFBundleURLSchemes</key>
    <array>
      <string>YOUR_DOT_REVERSED_IOS_CLIENT_ID</string>
    </array>
  </dict>
</array>

넣어주고

 

코드상으로는 구글 사인버튼을 만들어서 어쩌니 하는거 다 귀차나서 어차피 커스텀 버튼으로 만들꺼 스토리보드에서 버튼 만들어서 액션 연결.

 

라이브러리 임포트

import GoogleSignIn

 

델리게이트 설정

class SignVC: UIViewController,GIDSignInDelegate{

 

 // MARK: - Google Login
    @IBAction func loginGoogle(_ sender: Any) {
        GIDSignIn.sharedInstance()?.presentingViewController = self
        GIDSignIn.sharedInstance().delegate = self
        GIDSignIn.sharedInstance().signIn()

    }
    
    func sign(_ signIn: GIDSignIn!, didSignInFor user: GIDGoogleUser!, withError error: Error!) {
        if let error = error {
            if (error as NSError).code == GIDSignInErrorCode.hasNoAuthInKeychain.rawValue {
                print("The user has not signed in before or they have since signed out.")
            } else {
                print("\(error.localizedDescription)")
            }
            return
        }
            
        // 사용자 정보 가져오기
        if let userId = user.userID,                  // For client-side use only!
            let idToken = user.authentication.idToken, // Safe to send to the server
            let fullName = user.profile.name,
            let givenName = user.profile.givenName,
            let familyName = user.profile.familyName,
            let email = user.profile.email {
                
            print("Token : \(idToken)")
            print("User ID : \(userId)")
            print("User Email : \(email)")
            print("User Name : \((fullName))")

        } else {
            print("Error : User Data Not Found")
        }
    }
        
    // 구글 로그인 연동 해제했을때 불러오는 메소드
    func sign(_ signIn: GIDSignIn!, didDisconnectWith user: GIDGoogleUser!, withError error: Error!) {
        print("Disconnect")
    }

 

 

해서 액션걸고 로그인하려면 OAuth 어쩌고 저쩌고 하면서 액세스 디나이 뜨는 경우 있는데

 

https://console.cloud.google.com/apis/credentials/consent?project=

 

Google 클라우드 플랫폼

로그인 Google 클라우드 플랫폼으로 이동

accounts.google.com

 

들어가서 사용자유형 외부(난 외부메일로 테스트 할꺼라) 상태 테스트, 하고 테스트 사용자에 로그인 테스트 할 계정 추가해주면 해결.

Posted by npre
,