iOS Google 登錄最詳細(xì)

開發(fā)者平臺(tái):https://firebase.google.com/docs/auth/ios/google-signin?authuser=0#swift_3

將 Google Sign-In 集成到您的 iOS 應(yīng)用程序中

1. 開發(fā)者平臺(tái) - 登錄/注冊賬號(hào) - 添加項(xiàng)目 - 輸入項(xiàng)目名稱 - 點(diǎn)擊繼續(xù)…

image

2. 點(diǎn)擊iOS - 進(jìn)入注冊應(yīng)用頁面

image

3. 點(diǎn)擊注冊應(yīng)用

image

4. 下載plist文件,并拖入項(xiàng)目中

注:plist文件中已包含Google 登錄ID與URL Types

image

以下參考:在 iOS 上使用 Google 登錄服務(wù)進(jìn)行身份驗(yàn)證

準(zhǔn)備工作

  1. 將 Firebase 添加到您的 iOS 項(xiàng)目。在您的 Podfile 中添加以下 Pod:
// Use Firebase library to configure APIs
FirebaseApp.configure()
  1. 如果您尚未將您的應(yīng)用與 Firebase 項(xiàng)目相關(guān)聯(lián),請?jiān)?Firebase 控制臺(tái)中進(jìn)行關(guān)聯(lián)。
  2. 在 Firebase 控制臺(tái)中啟用 Google 登錄機(jī)制:
    1. Firebase 控制臺(tái)中,打開 Auth 部分。
    2. 登錄方法標(biāo)簽頁中,啟用 Google 登錄方法并點(diǎn)擊保存。

實(shí)現(xiàn) Google 登錄

按下列步驟實(shí)現(xiàn) Google 登錄。如需詳細(xì)了解如何在 iOS 設(shè)備上使用 Google 登錄服務(wù),請參閱 Google 登錄開發(fā)者文檔。

  1. 將自定義網(wǎng)址方案添加到您的 Xcode 項(xiàng)目中:

      1. 打開項(xiàng)目配置:在左側(cè)的樹狀視圖中雙擊項(xiàng)目名稱。在目標(biāo)部分中選擇您的應(yīng)用,然后選擇信息標(biāo)簽頁,并展開網(wǎng)址類型部分。
      1. 點(diǎn)擊 + 按鈕,并為您的倒序客戶端 ID 添加一個(gè)網(wǎng)址架構(gòu)。要找到這個(gè)值,請打開 <nobr style="box-sizing: inherit;">GoogleService-Info.plist</nobr> 配置文件,然后查找 REVERSED_CLIENT_ID 鍵。復(fù)制該鍵的值,并將其粘貼到配置頁面上的網(wǎng)址架構(gòu)框中。將其他字段留空。

    完成上述操作后,您的配置應(yīng)顯示如下(但其中的值應(yīng)替換為您的應(yīng)用的值):

image.png
  1. 在應(yīng)用委托的 application:didFinishLaunchingWithOptions: 方法中,配置 FirebaseApp 對象。
// Use Firebase library to configure APIs
FirebaseApp.configure()
  1. 實(shí)現(xiàn)您的應(yīng)用委托中的 application:openURL:options: 方法。此方法應(yīng)該調(diào)用 GIDSignIn 實(shí)例的 handleURL 方法,該方法將對您的應(yīng)用在身份驗(yàn)證過程結(jié)束時(shí)收到的網(wǎng)址進(jìn)行適當(dāng)處理。
@available(iOS 9.0, *)
func application(_ application: UIApplication, open url: URL,
                 options: [UIApplication.OpenURLOptionsKey: Any])
  -> Bool {
  return GIDSignIn.sharedInstance.handle(url)
}
  1. 將應(yīng)用的演示視圖控制器和客戶端 ID 傳遞給“Google 登錄”登錄方法,并根據(jù)生成的 Google 身份驗(yàn)證令牌創(chuàng)建 Firebase 身份驗(yàn)證憑據(jù):
    tip:設(shè)置clientID:GIDSignIn.sharedInstance()?.clientID = "打開項(xiàng)目中的 GoogleService-Info.plist,復(fù)制CLIENT_ID Value"
guard let clientID = FirebaseApp.app()?.options.clientID else { return }

// Create Google Sign In configuration object.
let config = GIDConfiguration(clientID: clientID)

// Start the sign in flow!
GIDSignIn.sharedInstance.signIn(with: config, presenting: self) { [unowned self] user, error in

  if let error = error {
    // ...
    return
  }

  guard
    let authentication = user?.authentication,
    let idToken = authentication.idToken
  else {
    return
  }

  let credential = GoogleAuthProvider.credential(withIDToken: idToken,
                                                 accessToken: authentication.accessToken)

  // ...
}

以下參考:將 Google Sign-In 集成到您的 iOS 應(yīng)用程序中

//點(diǎn)擊時(shí)候調(diào)用
GIDSignIn.sharedInstance.signIn(with: signInConfig, presenting: self) { user, error in
    guard error == nil else { return }

    // If sign in succeeded, display the app's main content View.
            let idToken = user?.authentication.idToken
            if (idToken != nil) {
                var param : [String:Any]
                param = [
                    "email":user?.profile?.email ?? "",
                    "user_id":user?.userID ?? "",
                    "id_token":idToken ?? "",
                ]
            }
  }

以上可以在user里面獲取email,accessToken,idToken,userId

獲取個(gè)人資料信息


撤銷訪問令牌并斷開應(yīng)用程序的連接
撤銷代表用戶授予您的應(yīng)用的訪問令牌,以及如何斷開用戶帳戶與您的應(yīng)用的連接

GIDSignIn.sharedInstance.disconnect { error in
    guard error == nil else { return }

    // Google Account disconnected from your app.
    // Perform clean-up actions, such as deleting data associated with the
    //   disconnected account.
}

disconnectWithCallback:方法除了斷開用戶的帳戶和撤銷令牌之外,還使用戶退出。在調(diào)用disconnectWithCallback:之前,您不得將用戶注銷。

然后,您可以響應(yīng)回調(diào)塊中的成功斷開連接并觸發(fā)您的應(yīng)用程序或后端代碼中的任何適當(dāng)邏輯。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容