Langsung ke konten

Firebase Google Login di iOS

Panduan ini akan membantu Anda mengintegrasikan Google Sign-In dengan Firebase Authentication di iOS. Saya berasumsi Anda telah menyelesaikan pengaturan Firebase Google umum.

  1. Buka project overview Anda di console.cloud.google.com

    Firebase Project Overview
  2. Klik tombol Add app

    Firebase Add App Button Firebase Add App Button
  3. Pilih iOS

    Firebase Add App iOS Button
  4. Isi bagian pertama formulir

    1. Isi Apple bundle ID
      1. Buka Xcode di aplikasi Anda menggunakan npx cap open ios
      2. Klik dua kali pada App App target in Xcode project navigator
      3. Pastikan Anda berada di Targets -> App Targets section in Xcode with App selected
      4. Temukan Bundle Identifier Anda Bundle Identifier field in Xcode project settings
      5. Salin Bundle Identifier dan tempelkan di Firebase console Firebase Add App iOS Bundle ID Field
    2. Klik tombol Register app Firebase Add App iOS Register Button
  5. Lewati langkah Download config file

    Firebase Add App iOS Skip Download Button
  6. Lewati langkah Add firebase SDK

    Firebase Add App iOS Skip Download Firebase SDK Button
  7. Lewati langkah Add initialization code

    Firebase Add App iOS Skip Add Initialization Code Button
  8. Klik tombol Continue to console

    Firebase Add App iOS Continue to Console Button
  9. Dapatkan iOS client ID dan YOUR_DOT_REVERSED_IOS_CLIENT_ID Anda

    1. Buka Google Cloud Console di console.cloud.google.com

    2. Temukan proyek Anda

      1. Klik pada pemilih proyek Google Cloud Console Project Selector
      2. Cari proyek Anda dengan nama persis dari proyek Firebase Anda dan klik di atasnya. Dalam kasus saya, ini adalah sociallogin-tutorial-app. Firebase Project Selector Project
    3. Buka search bar dan buka credentials

      1. Buka search bar Google Cloud Console Search Bar
      2. Cari credentials dan klik yang APIs and Services (nomor 2 pada screenshot) Google Cloud Console Credentials Search
    4. Klik pada iOS client for [YOUR_APP_ID] (auto created by Google Service). Dalam kasus saya, ini adalah sociallogin-tutorial-app.

      Google Cloud Console Credentials iOS Client ID
    5. Salin Client ID serta iOS URL scheme. Ini akan masing-masing menjadi iOSClientId dan YOUR_DOT_REVERSED_IOS_CLIENT_ID Anda.

      Google Cloud Console Credentials iOS Client ID Copy
  10. Dapatkan web client ID Anda

    1. Kembali ke Firebase console dan buka Build -> Authentication Firebase Authentication Menu
    2. Klik tombol Sign-in method Firebase Authentication Sign-in Method Button
    3. Klik provider Google Firebase Authentication Sign-in Method Google Provider
    4. Klik tombol Web SDK configuration Firebase Authentication Sign-in Method Web SDK Configuration Button
    5. Salin Web client ID. Ini akan menjadi webClientId Anda dalam metode initialize plugin. Firebase Authentication Sign-in Method Web SDK Configuration Web Client ID
  11. Modifikasi Info.plist aplikasi Anda

    1. Buka Xcode dan temukan file Info.plist

      Info.plist file in Xcode project navigator
    2. Klik kanan file ini dan buka sebagai source code

      Right-click menu showing Open As Source Code option
    3. Di bagian bawah file Plist Anda, Anda akan melihat tag </dict>

      Closing dict tag in Info.plist file
    4. Masukkan fragmen berikut tepat sebelum tag </dict> penutup

      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. Ubah YOUR_DOT_REVERSED_IOS_CLIENT_ID ke nilai yang disalin di langkah 9 (iOS URL scheme)

      Info.plist with actual reversed client ID inserted in URL schemes
  12. Ubah YOUR_IOS_CLIENT_ID ke iOS Client ID yang Anda salin di langkah 9

  13. Simpan file dengan Command + S

  14. Modifikasi AppDelegate.swift

    1. Buka AppDelegate

      AppDelegate.swift file in Xcode project navigator
    2. Masukkan import GoogleSignIn di bagian atas file

      AppDelegate.swift with GoogleSignIn import added
    3. Temukan fungsi func application(_ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey: Any] = [:])

      Original application openURL function in AppDelegate
    4. Modifikasi fungsi agar terlihat seperti ini

      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. Simpan file dengan Command + S

  15. Menggunakan login Google di aplikasi Anda

    Pada langkah ini, Anda siap menggunakan login Google di aplikasi Anda. Silakan gunakan file authUtils.ts dari aplikasi contoh untuk mengautentikasi dengan Google.

Pengguna akan secara otomatis dibuat di Firebase Auth pada sign-in pertama

Jika autentikasi macet atau gagal:

  • Verifikasi audience idToken cocok dengan Firebase web client ID Anda
  • Periksa bahwa Google Sign-In diaktifkan di Firebase Console
  • Pastikan Info.plist memiliki URL scheme dan GIDClientID yang benar
  • Verifikasi iOSServerClientId cocok dengan web client ID Anda
  • Tinjau kode aplikasi contoh sebagai referensi