iOS Google 登录最详细

2021-08-12  本文已影响0人  KingWorld

开发者平台:https://firebase.google.com/docs/auth/ios/google-signin?authuser=0#swift_3

将 Google Sign-In 集成到您的 iOS 应用程序中

1. 开发者平台 - 登录/注册账号 - 添加项目 - 输入项目名称 - 点击继续…

image

2. 点击iOS - 进入注册应用页面

image

3. 点击注册应用

image

4. 下载plist文件,并拖入项目中

注:plist文件中已包含Google 登录ID与URL Types

image

以下参考:在 iOS 上使用 Google 登录服务进行身份验证

准备工作

  1. 将 Firebase 添加到您的 iOS 项目。在您的 Podfile 中添加以下 Pod:
// Use Firebase library to configure APIs
FirebaseApp.configure()
  1. 如果您尚未将您的应用与 Firebase 项目相关联,请在 Firebase 控制台中进行关联。
  2. 在 Firebase 控制台中启用 Google 登录机制:
    1. Firebase 控制台中,打开 Auth 部分。
    2. 登录方法标签页中,启用 Google 登录方法并点击保存

实现 Google 登录

按下列步骤实现 Google 登录。如需详细了解如何在 iOS 设备上使用 Google 登录服务,请参阅 Google 登录开发者文档

  1. 将自定义网址方案添加到您的 Xcode 项目中:

      1. 打开项目配置:在左侧的树状视图中双击项目名称。在目标部分中选择您的应用,然后选择信息标签页,并展开网址类型部分。
      1. 点击 + 按钮,并为您的倒序客户端 ID 添加一个网址架构。要找到这个值,请打开 <nobr style="box-sizing: inherit;">GoogleService-Info.plist</nobr> 配置文件,然后查找 REVERSED_CLIENT_ID 键。复制该键的值,并将其粘贴到配置页面上的网址架构框中。将其他字段留空。

    完成上述操作后,您的配置应显示如下(但其中的值应替换为您的应用的值):

image.png
  1. 在应用委托的 application:didFinishLaunchingWithOptions: 方法中,配置 FirebaseApp 对象。
// Use Firebase library to configure APIs
FirebaseApp.configure()
  1. 实现您的应用委托中的 application:openURL:options: 方法。此方法应该调用 GIDSignIn 实例的 handleURL 方法,该方法将对您的应用在身份验证过程结束时收到的网址进行适当处理。
@available(iOS 9.0, *)
func application(_ application: UIApplication, open url: URL,
                 options: [UIApplication.OpenURLOptionsKey: Any])
  -> Bool {
  return GIDSignIn.sharedInstance.handle(url)
}
  1. 将应用的演示视图控制器和客户端 ID 传递给“Google 登录”登录方法,并根据生成的 Google 身份验证令牌创建 Firebase 身份验证凭据:
    tip:设置clientID:GIDSignIn.sharedInstance()?.clientID = "打开项目中的 GoogleService-Info.plist,复制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 应用程序中

//点击时候调用
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

获取个人资料信息


撤销访问令牌并断开应用程序的连接
撤销代表用户授予您的应用的访问令牌,以及如何断开用户帐户与您的应用的连接

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:方法除了断开用户的帐户和撤销令牌之外,还使用户退出。在调用disconnectWithCallback:之前,您不得将用户注销。

然后,您可以响应回调块中的成功断开连接并触发您的应用程序或后端代码中的任何适当逻辑。

上一篇下一篇

猜你喜欢

热点阅读