本页向您展示如何基于 UIKit 和UIApplicationDelegate
生命周期将 Google Sign-In 集成到 iOS 应用程序中。如果您的应用程序是基于场景的或使用 SwiftUI,您将需要根据您选择的生命周期调整这些说明。
在你开始之前
1.配置谷歌登录库
使用您在完成入门步骤时创建的客户端 ID 创建
GIDConfiguration
对象。您可以在任何保持应用程序配置状态的地方执行此操作,例如单例对象。迅速
let signInConfig = GIDConfiguration.init(clientID: "YOUR_IOS_CLIENT_ID")
目标-C
signInConfig = [[GIDConfiguration alloc] initWithClientID:@"YOUR_IOS_CLIENT_ID"];
如果您需要获取用户的 ID 令牌以进行后端身份验证,请同时设置
serverClientID
参数。如果要优化 Google Workspace 域的登录流程,或者要指定 OpenID 领域,则可以分别使用hostedDomain
和openIDRealm
参数。在您的 AppDelegate 的
application:openURL:options
方法中,调用GIDSignIn
的handleURL:
方法:迅速
func application( _ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey : Any] = [:] ) -> Bool { var handled: Bool handled = GIDSignIn.sharedInstance.handle(url) if handled { return true } // Handle other custom URL types. // If not handled by this app, return false. return false }
目标-C
- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<UIApplicationOpenURLOptionsKey,id> *)options { BOOL handled; handled = [GIDSignIn.sharedInstance handleURL:url]; if (handled) { return YES; } // Handle other custom URL types. // If not handled by this app, return NO. return NO; }
2.尝试恢复用户登录状态
当您的应用启动时,调用restorePreviousSignInWithCallback
以尝试恢复已使用 Google 登录的用户的登录状态。这样做可确保用户不必在每次打开您的应用时都登录(除非他们已退出)。
应用程序通常在 AppDelegate 的application:didFinishLaunchingWithOptions:
方法中执行此操作,并使用结果来确定向用户呈现哪个视图。例如:
迅速
func application(
_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
) -> Bool {
GIDSignIn.sharedInstance.restorePreviousSignIn { user, error in
if error != nil || user == nil {
// Show the app's signed-out state.
} else {
// Show the app's signed-in state.
}
}
return true
}
目标-C
- (BOOL)application:(UIApplication *)application
didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
[GIDSignIn.sharedInstance restorePreviousSignInWithCallback:^(GIDGoogleUser * _Nullable user,
NSError * _Nullable error) {
if (error) {
// Show the app's signed-out state.
} else {
// Show the app's signed-in state.
}
}];
return YES;
}
3. 添加谷歌登录按钮
在您的登录视图中添加“使用 Google 登录”按钮。您可以使用
GIDSignInButton
类自动生成带有 Google 品牌(推荐)的按钮,或使用自定义样式创建自己的按钮。要将
GIDSignInButton
添加到故事板或 XIB 文件,请添加一个 View 并将其自定义类设置为GIDSignInButton
。请注意,当您将GIDSignInButton
视图添加到故事板时,登录按钮不会在界面构建器中呈现。运行应用程序以查看登录按钮。您可以通过设置
colorScheme
和style
属性来自定义GIDSignInButton
的外观:GIDSignInButton 样式属性 colorScheme
kGIDSignInButtonColorSchemeLight
kGIDSignInButtonColorSchemeDark
style
kGIDSignInButtonStyleStandard
kGIDSignInButtonStyleWide
kGIDSignInButtonStyleIconOnly
将按钮连接到 ViewController 中调用
signInWithConfiguration:
,传递您在步骤 1 中创建的配置对象。例如,使用IBAction
:迅速
@IBAction func signIn(sender: Any) { 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. } }
目标-C
- (IBAction)signIn:(id)sender { [GIDSignIn.sharedInstance signInWithConfiguration:signInConfig presentingViewController:self callback:^(GIDGoogleUser * _Nullable user, NSError * _Nullable error) { if (error) { return; } // If sign in succeeded, display the app's main content View. }]; }
4.添加退出按钮
向您的应用添加一个退出按钮,对登录用户可见。
将按钮连接到 ViewController 中调用
signOut:
。例如,使用IBAction
:迅速
@IBAction func signOut(sender: Any) { GIDSignIn.sharedInstance.signOut() }
目标-C
- (IBAction)signOut:(id)sender { [GIDSignIn.sharedInstance signOut]; }
下一步
既然用户可以使用他们的 Google 帐户登录您的应用,请了解如何:
- 获取用户的 Google 帐户个人资料信息。
- 使用用户的 Google ID 令牌向您的后端进行身份验证。
- 代表用户调用 Google API 。