コンテンツへスキップ

iOSでのGoogleログイン

このガイドでは、Capgo Social LoginでiOS用のGoogleログインを設定する方法を学びます。 一般的なセットアップガイドをすでに読んでいることを前提としています。

このパートでは、iOSでGoogleログインを設定する方法を学びます。

  1. Google consoleでiOS client IDを作成します

    1. 検索バーをクリックします

      Google Console search bar
    2. credentialsを検索し、APIs and Services(スクリーンショットの2番)をクリックします

      Search results showing credentials option with APIs and Services highlighted
    3. create credentialsをクリックします

      Create credentials button in Google Console
    4. OAuth client IDを選択します

      OAuth client ID option in credentials creation menu
    5. Application typeiOSに選択します

      Application type selection with iOS option highlighted
    6. bundle IDを見つけます

      1. 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. Google Consoleに戻り、Bundle IDBundle Identifierを貼り付けます

        Bundle ID field in Google Console iOS client creation form
    7. オプションで、App Storeにアプリを公開している場合は、App Store IDまたはTeam IDをclient IDに追加します

    8. すべての詳細を入力したら、createをクリックします

      Create button at bottom of iOS client creation form
    9. OKをクリックします

      OK button on client ID created confirmation dialog
    10. 新しく作成したiOS clientを開きます

      Newly created iOS client in credentials list
    11. 次のデータをコピーします

      Client ID details showing Client ID and reversed client ID to copy
  2. アプリの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>
    5. YOUR_DOT_REVERSED_IOS_CLIENT_IDを前のステップでコピーした値に変更します

      Info.plist with actual reversed client ID inserted in URL schemes
    6. Command + Sでファイルを保存します

  3. 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でファイルを保存します

  4. JavaScript/TypeScriptコードでGoogleログインを設定します

    1. SocialLoginCapacitorをインポートします

      import { SocialLogin } from '@capgo/capacitor-social-login';
      import { Capacitor } from '@capacitor/core';
    2. initializeメソッドを呼び出します(これは一度だけ呼び出す必要があります)

      基本設定(オンラインモード - ほとんどのアプリに推奨):

      // onMountedはVue固有のものです
      onMounted(() => {
      SocialLogin.initialize({
      google: {
      iOSClientId: '673324426943-redacted.apps.googleusercontent.com',
      mode: 'online' // デフォルトモード
      }
      })
      })

      追加のclient IDを使用した高度な設定:

      onMounted(() => {
      SocialLogin.initialize({
      google: {
      webClientId: 'YOUR_WEB_CLIENT_ID', // オプション: Webプラットフォームサポート用
      iOSClientId: 'YOUR_IOS_CLIENT_ID', // 必須: ステップ1から
      iOSServerClientId: 'YOUR_WEB_CLIENT_ID', // オプション: webClientIdと同じ、一部の高度な機能に必要
      mode: 'online' // 'online'または'offline'
      }
      })
      })
    3. ログイン関数を実装します。ボタンを作成し、クリック時に次のコードを実行します

      オンラインモードの場合:

      const res = await SocialLogin.login({
      provider: 'google',
      options: {}
      })
      // レスポンスを処理 - ユーザーデータが含まれます
      console.log(JSON.stringify(res))

      オフラインモードの場合:

      const res = await SocialLogin.login({
      provider: 'google',
      options: {
      forceRefreshToken: true // オフラインモードに推奨
      }
      })
      // resにはserverAuthCodeが含まれ、ユーザーデータは含まれません
      // serverAuthCodeをバックエンドに送信してユーザー情報を取得します
      console.log('Server auth code:', res.result.serverAuthCode)
  5. アプリケーションをテストします

    1. アプリをビルドしてcap syncを実行します

    2. すべて正しく行った場合、Googleログインフローが正しく動作することを確認できます

      Demo of Google login flow on iOS showing sign-in process and successful authentication

Privacy Screenプラグインとの非互換性

Section titled “Privacy Screenプラグインとの非互換性”

Googleログインプラグインは@capacitor/privacy-screenと互換性がありません。両方のプラグインを一緒に使用すると、Googleログインwebviewがプライバシー画面によって中断されます。

回避策: ログイン関数を呼び出す前にawait PrivacyScreen.disable();を呼び出します:

import { PrivacyScreen } from '@capacitor/privacy-screen';
import { SocialLogin } from '@capgo/capacitor-social-login';
await PrivacyScreen.disable();
await SocialLogin.login({
provider: 'google',
options: {}
});