撤消访问令牌并断开应用

我们强烈建议您让使用 Google 帐号登录的用户能够解除其 Google 帐号与您的应用的关联。如果用户删除其帐号,您必须删除您的应用从 Google API 获取的信息。

以下代码示例演示了如何以程序化方式撤消已代表用户授予您的应用的访问令牌,并取消用户帐号与应用的连接。

Swift

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

Objective-C

[GIDSignIn.sharedInstance disconnectWithCompletion:^(NSError * _Nullable error) {
    if (error) { return; }

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

disconnectWithCompletion: 方法除了会断开用户帐号并撤消令牌,还能使用户退出登录。不得在调用 disconnectWithCompletion: 之前让用户退出登录。

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