Zum Inhalt springen

Google Login on iOS

Dieser Inhalt ist in Ihrer Sprache noch nicht verfügbar.

Google Login setup for iOS

Introduction

In this guide, you will learn how to setup Google Login with Capgo Social Login for iOS. I assume that you have already read the general setup guide.

Using Google login on iOS

In this part, you will learn how to setup Google login in iOS.

  1. Create an iOS client ID in the Google console

    1. Click on the search bar

      Google Console search bar
    2. Search for credentials and click on the APIs and Services one (number 2 on the screenshot)

      Search results showing credentials option with APIs and Services highlighted
    3. Click on the create credentials

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

      OAuth client ID option in credentials creation menu
    5. Select the Application type to iOS

      Application type selection with iOS option highlighted
    6. Find the bundle ID

      1. Open Xcode

      2. Double click on App

        App target in Xcode project navigator
      3. Ensure that you are on Targets -> App

        Targets section in Xcode with App selected
      4. Find your Bundle Identifier

        Bundle Identifier field in Xcode project settings
      5. Go back to the Google Console and paste your Bundle Identifier into Bundle ID

        Bundle ID field in Google Console iOS client creation form
    7. Optionally, add your App Store ID or Team ID into the client ID if you have published your app to App Store

    8. After filling all the details, click create

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

      OK button on client ID created confirmation dialog
    10. Open the newly created iOS client

      Newly created iOS client in credentials list
    11. Copy the following data

      Client ID details showing Client ID and reversed client ID to copy
  2. Modify your app’s Info.plist

    1. Open Xcode and find the Info.plist file

      Info.plist file in Xcode project navigator
    2. Right click this file and open it as source code

      Right-click menu showing Open As Source Code option
    3. At the bottom of your Plist file, you will see a </dict> tag

      Closing dict tag in Info.plist file
    4. Insert the following fragment just before the closing </dict> tag

      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. Change the YOUR_DOT_REVERSED_IOS_CLIENT_ID to the value copied in the previous step

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

  3. Modify the AppDelegate.swift

    1. Open the AppDelegate

      AppDelegate.swift file in Xcode project navigator
    2. Insert import GoogleSignIn at the top of the file

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

      Original application openURL function in AppDelegate
    4. Modify the function to look like this

      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. Save the file with Command + S

  4. Setup Google login in your JavaScript/TypeScript code

    1. Import SocialLogin and Capacitor

      import { SocialLogin } from '@capgo/capacitor-social-login';
      import { Capacitor } from '@capacitor/core';
    2. Call the initialize method (this should be called only once)

      // onMounted is Vue specific
      onMounted(() => {
      SocialLogin.initialize({
      google: {
      iOSClientId: '673324426943-redacted.apps.googleusercontent.com',
      }
      })
      })
    3. Implement the login function. Create a button and run the following code on click

      const res = await SocialLogin.login({
      provider: 'google',
      options: {}
      })
      // handle the response
      console.log(JSON.stringify(res))
  5. Test your application

    1. Build your app and run cap sync

    2. If you’ve done everything correctly, you should see the Google login flow working properly

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