【iOS】FireBase を使って、Google SignIn 機能を実装して、ユーザー情報を取得してみる

・firebase の ログイン機能を使ってみる。

qiita.com

・rbenv をインストールして管理する。

qiita.com

・pod がうまくいかない場合は、↓こちらを参照。

stackoverflow.com

1: sudo gem uninstall cocoapods

2: sudo gem install -n /usr/local/bin cocoapods

3: pod install

-Bridging-Header.h を追加する。

qiita.com

Podfile の中身

pod 'Firebase'
pod 'Firebase/Database’
pod 'Firebase/Storage'
pod 'Firebase/Auth’
pod 'GoogleSignIn', '~> 5.0'

LoginViewController.swift

import UIKit
import GoogleSignIn
import Firebase
class LoginViewController: UIViewController{
override func viewDidLoad() {
super.viewDidLoad()
GIDSignIn.sharedInstance().presentingViewController = self
GIDSignIn.sharedInstance().delegate = self
}
@IBAction func signIn(_ sender: Any) {
GIDSignIn.sharedInstance().signIn()
}
@IBAction func signOut(_ sender: AnyObject) {
GIDSignIn.sharedInstance().signOut()
}
}
extension LoginViewController: GIDSignInDelegate{
func sign(_ signIn: GIDSignIn!, didSignInFor user: GIDGoogleUser!, withError error: Error!) {
if let error = error {
print(error.localizedDescription)
return
}
guard let authentication = user.authentication else { return }
let credential = GoogleAuthProvider.credential(withIDToken: authentication.idToken,accessToken: authentication.accessToken)
Auth.auth().signIn(with: credential) { (user, error) in
if let error = error {
print("Error: \(error.localizedDescription)")
return
}
print(user?.user.uid)
print(user?.user.displayName!)
}
}
func sign(_ signIn: GIDSignIn!, didDisconnectWith user: GIDGoogleUser!, withError error: Error!) {
// Perform any operations when the user disconnects from app here.
// ...
}

feliz.work

返信を残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です

CAPTCHA