brunch

You can make anything
by writing

C.S.Lewis

by Tilltue Aug 23. 2016

Firebase, iOS Auth설정 #Google

Firebase Auth Google Sign In 연동 알아보기.

* 이글은 Swift 3.0 , Firebase 3.12.0, GoogleSignIn 4.0.1 을 기준으로 작성하였습니다.

Firebase Auth 소개 영상


Firebase Auth 구글 Sign in 연동


먼저 Firebase console 에서 Auth 탭 -> Sign In 방법에서 구글을 활성화 하자.

구글 Sign In 사용 활성화


XCode Project로 돌아와서 Podfile 에  pod 'GoogleSignIn'  를 추가하자


platform :ios, '10.0'

source 'https://github.com/CocoaPods/Specs.git'

target 'firebaseTEST' do 

 use_frameworks!

 pod 'Firebase/Database'

    pod 'Firebase/Auth'

    pod 'GoogleSignIn'

    pod 'FacebookCore'

    pod 'FacebookLogin'

    pod 'FacebookShare'

end


Firebase 설정단계에서 프로젝트에 추가한 GoogleService-Info.plist 파일의 내용을 보면

REVERSED_CLIENT_ID 가 있다.


REVERSED_CLIENT_ID 내용 복사

Project 를 선택하고  -> Target -> Info 에서 URL Types 에 위 내용을 추가하자.

URL Types -> URL Schemes 추가.

환경설정은 끝났다.


클라이언트 ID 설정


Appdelegate 에서 구글 SignIn 을 위한 클라이언트 ID를 넣어준다.


class AppDelegate: UIResponder, UIApplicationDelegate {

    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {

        // Override point for customization after application launch.

        FIRApp.configure()

        GIDSignIn.sharedInstance().clientID = FIRApp.defaultApp()?.options.clientID

        return true

    }



Google Sign In openURL 처리

(* facebook Sign In openURL 도 같이 처리했다.)


func application(_ app: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey : Any] = [:]) -> Bool {

        if GIDSignIn.sharedInstance().handle(url,

                                             sourceApplication: options[.sourceApplication] as? String,

                                             annotation: options[.annotation]) {

            return true

        }else if FBSDKApplicationDelegate.sharedInstance().application(app,

                                                                       open: url as URL!,

                                                                       sourceApplication: options[.sourceApplication] as! String,

                                                                       annotation: options[.annotation]) {

            return true

        }

        return false

    }


iOS 10 이하 버전을 위해 아래의 소스도 추가한다.


    func application(_ application: UIApplication, open url: URL, sourceApplication: String?, annotation: Any) -> Bool {

        if GIDSignIn.sharedInstance().handle(url,

                                             sourceApplication: sourceApplication,

                                             annotation: annotation) {

            return true

        }else if FBSDKApplicationDelegate.sharedInstance().application(application,

                                                                       open: url as URL!,

                                                                       sourceApplication: sourceApplication,

                                                                       annotation: annotation) {

            return true

        }

        return false

    }



Google SignIn Test


delegate 를 설정한다.


import GoogleSignIn


class LoginViewController: UIViewController,GIDSignInDelegate, GIDSignInUIDelegate {

override func viewDidLoad() {

        super.viewDidLoad()

        GIDSignIn.sharedInstance().delegate = self

        GIDSignIn.sharedInstance().uiDelegate = self

    }

...


delegate 함수를 아래와 같이 작성.


func sign(_ signIn: GIDSignIn?, didSignInFor user: GIDGoogleUser?, withError error: Error?) {

        if let error = error {

            print(error.localizedDescription)

        }

        guard let authentication = user?.authentication else { return }

        let credential = FIRGoogleAuthProvider.credential(withIDToken: authentication.idToken,

                                                              accessToken: authentication.accessToken)

            FIRAuth.auth()?.signIn(with: credential) { (user, error) in

                if let error = error {

                     print(error.localizedDescription)

                }else {

                }

            }


    }


    /* optional

    func signIn(signIn: GIDSignIn?, didDisconnectWithUser user:GIDGoogleUser?,

                withError error: NSError?) {

    }*/



Sign in Button 생성 및 테스트


        GIDSignIn.sharedInstance().uiDelegate = self

        let signInButton = GIDSignInButton()

        signInButton.center = CGPoint(x: view.center.x,y: view.center.y - 50)

        view.addSubview(signInButton)


*( Custom UIButton 을 사용한 Sign in) 

GIDSignIn.sharedInstance().signIn() 만 불러 주면 된다.


Google Sign in 버튼이 생겼다


하지만 Sign In을 진행해보고 콘솔로그를 자세히 보면,

<FIRAnalytics/INFO> Successfully created Firebase Analytics App Delegate Proxy automatically. To disable the proxy, set the flag FirebaseAppDelegateProxyEnabled to NO in the Info.plist

2016-08-23 20:36:28.090: <FIRInstanceID/WARNING> FIRInstanceID AppDelegate proxy enabled, will swizzle app delegate remote notification handlers. To disable add "FirebaseAppDelegateProxyEnabled" to your Info.plist and set it to NO

위와 같은 에러가 나온다.


에러에서 안내하는 해결방식대로 info.plist 에 아래와 같이 "FirebaseAppDelegateProxyEnabled" , "Boolean" 타입으로 추가하고 값을 "NO"로 설정하자.


자 이제 다시 테스트를 위해 위쪽 signIn 메서드 중간에 아래와 같이 추가하자.

        FIRAuth.auth()?.signIn(with: credential) { [weak self] (user, error) in

            print("google signIn")

            func firebaseWriteTest(){

                let ref = FIRDatabase.database().reference()

                let itemsRef = ref.child("test")

                itemsRef.setValue(["Name":"TilltueGoogle"])

            }

            firebaseWriteTest()

        }


정상적으로 database 에 쓰이는 것을 확인 할 수 있다.



Firebase Console > Auth > 사용자 탭에서 유저를 관리할 수 있다.

매거진의 이전글 Firebase, iOS Auth설정 #Facebook

작품 선택

키워드 선택 0 / 3 0

댓글여부

afliean
브런치는 최신 브라우저에 최적화 되어있습니다. IE chrome safari