コンテンツへスキップ

iOSでのFirebase Googleログイン

このガイドでは、iOSでFirebase AuthenticationとGoogleサインインを統合する方法を説明します。一般的なFirebase Googleの設定をすでに完了していることを前提としています。

  1. console.cloud.google.comでプロジェクト概要に移動します

    Firebase Project Overview
  2. Add appボタンをクリックします

    Firebase Add App Button Firebase Add App Button
  3. iOSを選択します

    Firebase Add App iOS Button
  4. フォームの最初の部分を入力します

    1. Apple bundle IDを入力します
      1. npx cap open iosを使用してXcodeでアプリを開きます
      2. Appをダブルクリックします App target in Xcode project navigator
      3. Targets -> Appにいることを確認します Targets section in Xcode with App selected
      4. Bundle Identifierを見つけます Bundle Identifier field in Xcode project settings
      5. Bundle IdentifierをコピーしてFirebase consoleに貼り付けます Firebase Add App iOS Bundle ID Field
    2. Register appボタンをクリックします Firebase Add App iOS Register Button
  5. Download config fileステップをスキップします

    Firebase Add App iOS Skip Download Button
  6. Add firebase SDKステップをスキップします

    Firebase Add App iOS Skip Download Firebase SDK Button
  7. Add initialization codeステップをスキップします

    Firebase Add App iOS Skip Add Initialization Code Button
  8. Continue to consoleボタンをクリックします

    Firebase Add App iOS Continue to Console Button
  9. iOS client IDとYOUR_DOT_REVERSED_IOS_CLIENT_IDを取得します

    1. console.cloud.google.comのGoogle Cloud Consoleにアクセスします

    2. プロジェクトを見つけます

      1. プロジェクトセレクターをクリックします Google Cloud Console Project Selector
      2. Firebaseプロジェクトの正確な名前でプロジェクトを検索してクリックします。私の場合はsociallogin-tutorial-appです。 Firebase Project Selector Project
    3. 検索バーを開いてcredentialsを開きます

      1. 検索バーを開きます Google Cloud Console Search Bar
      2. credentialsを検索し、APIs and Services(スクリーンショットの2番)をクリックします Google Cloud Console Credentials Search
    4. iOS client for [YOUR_APP_ID] (auto created by Google Service)をクリックします。私の場合はsociallogin-tutorial-appです。

      Google Cloud Console Credentials iOS Client ID
    5. Client IDiOS URL schemeをコピーします。これらがそれぞれiOSClientIdYOUR_DOT_REVERSED_IOS_CLIENT_IDになります。

      Google Cloud Console Credentials iOS Client ID Copy
  10. Web client IDを取得します

    1. Firebase consoleに戻り、Build -> Authenticationに移動します Firebase Authentication Menu
    2. Sign-in methodボタンをクリックします Firebase Authentication Sign-in Method Button
    3. Googleプロバイダーをクリックします Firebase Authentication Sign-in Method Google Provider
    4. Web SDK configurationボタンをクリックします Firebase Authentication Sign-in Method Web SDK Configuration Button
    5. Web client IDをコピーします。これがプラグインのinitializeメソッドのwebClientIdになります。 Firebase Authentication Sign-in Method Web SDK Configuration Web Client ID
  11. アプリの Info.plistを変更します

    1. Xcodeを開いてInfo.plistファイルを見つけます

      Info.plist file in Xcode project navigator
    2. このファイルを右クリックしてソースコードとして開きます

      Right-click menu showing Open As Source Code option
    3. Plistファイルの下部に</dict>タグが表示されます

      Closing dict tag in Info.plist file
    4. 閉じる</dict>タグの直前に次のフラグメントを挿入します

      Info.plist with URL schemes code inserted before closing dict tag
      <key>CFBundleURLTypes</key>
      <array>
      <dict>
      <key>CFBundleURLSchemes</key>
      <array>
      <string>YOUR_DOT_REVERSED_IOS_CLIENT_ID</string>
      </array>
      </dict>
      </array>
      <key>GIDClientID</key>
      <string>YOUR_IOS_CLIENT_ID.apps.googleusercontent.com</string>
    5. YOUR_DOT_REVERSED_IOS_CLIENT_IDをステップ9でコピーした値(iOS URL scheme)に変更します

      Info.plist with actual reversed client ID inserted in URL schemes
  12. YOUR_IOS_CLIENT_IDをステップ9でコピーしたiOS Client IDに変更します

  13. Command + Sでファイルを保存します

  14. AppDelegate.swiftを変更します

    1. AppDelegateを開きます

      AppDelegate.swift file in Xcode project navigator
    2. ファイルの先頭にimport GoogleSignInを挿入します

      AppDelegate.swift with GoogleSignIn import added
    3. func application(_ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey: Any] = [:])関数を見つけます

      Original application openURL function in AppDelegate
    4. 関数を次のように変更します

      func application(_ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey: Any] = [:]) -> Bool {
      // Called when the app was launched with a url. Feel free to add additional processing here,
      // but if you want the App API to support tracking app url opens, make sure to keep this call
      var handled: Bool
      handled = GIDSignIn.sharedInstance.handle(url)
      if handled {
      return true
      }
      return ApplicationDelegateProxy.shared.application(app, open: url, options: options)
      }
      Modified application openURL function with GoogleSignIn handling
    5. Command + Sでファイルを保存します

  15. アプリでGoogleログインを使用する

    この時点で、アプリでGoogleログインを使用する準備が整いました。 サンプルアプリのauthUtils.tsファイルを使用してGoogleで認証してください。

ユーザーは初回サインイン時にFirebase Authで自動的に作成されます

認証がハングまたは失敗する場合:

  • idTokenのaudienceがFirebaseのWeb client IDと一致していることを確認してください
  • Firebase ConsoleでGoogleサインインが有効になっていることを確認してください
  • Info.plistに正しいURL schemesとGIDClientIDがあることを確認してください
  • iOSServerClientIdがWeb client IDと一致していることを確認してください
  • 参考としてサンプルアプリコードを確認してください