iOSでのGoogleログイン
このガイドでは、Capgo Social LoginでiOS用のGoogleログインを設定する方法を学びます。 一般的なセットアップガイドをすでに読んでいることを前提としています。
iOSでのGoogleログインの使用
Section titled “iOSでのGoogleログインの使用”このパートでは、iOSでGoogleログインを設定する方法を学びます。
-
Google consoleでiOS client IDを作成します
-
検索バーをクリックします

-
credentialsを検索し、APIs and Services(スクリーンショットの2番)をクリックします
-
create credentialsをクリックします
-
OAuth client IDを選択します
-
Application typeをiOSに選択します
-
bundle IDを見つけます
-
Xcodeを開きます
-
Appをダブルクリックします
-
Targets -> Appにいることを確認します
-
Bundle Identifierを見つけます
-
Google Consoleに戻り、
Bundle IDにBundle Identifierを貼り付けます
-
-
オプションで、App Storeにアプリを公開している場合は、
App Store IDまたはTeam IDをclient IDに追加します -
すべての詳細を入力したら、
createをクリックします
-
OKをクリックします
-
新しく作成したiOS clientを開きます

-
次のデータをコピーします

-
-
アプリのInfo.plistを変更します
-
Xcodeを開いて
Info.plistファイルを見つけます
-
このファイルを右クリックしてソースコードとして開きます

-
Plistファイルの下部に</dict>タグが表示されます
-
閉じる
</dict>タグの直前に次のフラグメントを挿入します
<key>CFBundleURLTypes</key><array><dict><key>CFBundleURLSchemes</key><array><string>YOUR_DOT_REVERSED_IOS_CLIENT_ID</string></array></dict></array> -
YOUR_DOT_REVERSED_IOS_CLIENT_IDを前のステップでコピーした値に変更します
-
Command + Sでファイルを保存します
-
-
AppDelegate.swiftを変更します-
AppDelegateを開きます

-
ファイルの先頭に
import GoogleSignInを挿入します
-
func application(_ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey: Any] = [:])関数を見つけます
-
関数を次のように変更します
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 callvar handled: Boolhandled = GIDSignIn.sharedInstance.handle(url)if handled {return true}return ApplicationDelegateProxy.shared.application(app, open: url, options: options)}
-
Command + Sでファイルを保存します
-
-
JavaScript/TypeScriptコードでGoogleログインを設定します
-
SocialLoginとCapacitorをインポートしますimport { SocialLogin } from '@capgo/capacitor-social-login';import { Capacitor } from '@capacitor/core'; -
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'}})}) -
ログイン関数を実装します。ボタンを作成し、クリック時に次のコードを実行します
オンラインモードの場合:
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)
-
-
アプリケーションをテストします
-
アプリをビルドして
cap syncを実行します -
すべて正しく行った場合、Googleログインフローが正しく動作することを確認できます

-
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: {}});