Langsung ke konten

Google Login di iOS

Dalam panduan ini, Anda akan mempelajari cara mengatur Google Login dengan Capgo Social Login untuk iOS. Saya menganggap bahwa Anda telah membaca panduan pengaturan umum.

Dalam bagian ini, Anda akan mempelajari cara mengatur Google login di iOS.

  1. Buat client ID iOS di konsol Google

    1. Klik pada bilah pencarian

      Google Console search bar
    2. Cari credentials dan klik pada yang APIs and Services (nomor 2 pada tangkapan layar)

      Search results showing credentials option with APIs and Services highlighted
    3. Klik pada create credentials

      Create credentials button in Google Console
    4. Pilih OAuth client ID

      OAuth client ID option in credentials creation menu
    5. Pilih Application type menjadi iOS

      Application type selection with iOS option highlighted
    6. Temukan bundle ID

      1. Buka Xcode

      2. Klik dua kali pada App

        App target in Xcode project navigator
      3. Pastikan bahwa 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. Kembali ke Google Console dan tempelkan Bundle Identifier Anda ke Bundle ID

        Bundle ID field in Google Console iOS client creation form
    7. Secara opsional, tambahkan App Store ID atau Team ID Anda ke client ID jika Anda telah mempublikasikan aplikasi Anda ke App Store

    8. Setelah mengisi semua detail, klik create

      Create button at bottom of iOS client creation form
    9. Klik OK

      OK button on client ID created confirmation dialog
    10. Buka client iOS yang baru dibuat

      Newly created iOS client in credentials list
    11. Salin data berikut

      Client ID details showing Client ID and reversed client ID to copy
  2. 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 kode sumber

      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>
    5. Ubah YOUR_DOT_REVERSED_IOS_CLIENT_ID ke nilai yang disalin pada langkah sebelumnya

      Info.plist with actual reversed client ID inserted in URL schemes
    6. Simpan file dengan Command + S

  3. 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

  4. Atur Google login di kode JavaScript/TypeScript Anda

    1. Impor SocialLogin dan Capacitor

      import { SocialLogin } from '@capgo/capacitor-social-login';
      import { Capacitor } from '@capacitor/core';
    2. Panggil metode initialize (ini harus dipanggil hanya sekali)

      Pengaturan dasar (mode online - direkomendasikan untuk sebagian besar aplikasi):

      // onMounted adalah spesifik Vue
      onMounted(() => {
      SocialLogin.initialize({
      google: {
      iOSClientId: '673324426943-redacted.apps.googleusercontent.com',
      mode: 'online' // Mode default
      }
      })
      })

      Pengaturan lanjutan dengan client ID tambahan:

      onMounted(() => {
      SocialLogin.initialize({
      google: {
      webClientId: 'YOUR_WEB_CLIENT_ID', // Opsional: untuk dukungan platform web
      iOSClientId: 'YOUR_IOS_CLIENT_ID', // Wajib: dari langkah 1
      iOSServerClientId: 'YOUR_WEB_CLIENT_ID', // Opsional: sama dengan webClientId, diperlukan untuk beberapa fitur lanjutan
      mode: 'online' // 'online' atau 'offline'
      }
      })
      })
    3. Implementasikan fungsi login. Buat tombol dan jalankan kode berikut saat diklik

      Untuk mode online:

      const res = await SocialLogin.login({
      provider: 'google',
      options: {}
      })
      // menangani respons - berisi data pengguna
      console.log(JSON.stringify(res))

      Untuk mode offline:

      const res = await SocialLogin.login({
      provider: 'google',
      options: {
      forceRefreshToken: true // Direkomendasikan untuk mode offline
      }
      })
      // res berisi serverAuthCode, bukan data pengguna
      // Kirim serverAuthCode ke backend Anda untuk mendapatkan informasi pengguna
      console.log('Server auth code:', res.result.serverAuthCode)
  5. Uji aplikasi Anda

    1. Build aplikasi Anda dan jalankan cap sync

    2. Jika Anda telah melakukan semuanya dengan benar, Anda harus melihat alur Google login berfungsi dengan baik

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

Plugin Google Login tidak kompatibel dengan @capacitor/privacy-screen. Ketika menggunakan kedua plugin bersama-sama, webview Google login akan terganggu oleh layar privasi.

Solusi: Panggil await PrivacyScreen.disable(); sebelum memanggil fungsi login:

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