You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Google iOS Sign-In适配跳转LoginPage的AppDelegate.swift代码实现问题

Google iOS Sign-In AppDelegate 状态填充代码

未登录状态(注释 // Show the app's signed-out state. 位置)

这里需要将根视图切换为登录页,代码如下:

DispatchQueue.main.async {
    let loginVC = LoginViewController()
    self.window?.rootViewController = UINavigationController(rootViewController: loginVC)
    self.window?.makeKeyAndVisible()
}

加DispatchQueue.main.async是为了避免UI操作不在主线程执行的崩溃问题,如果你项目的登录页已经配置了导航栏,可去掉外层的UINavigationController包裹

已登录状态(注释 // Show the app's signed-in state. 位置)

这里可以直接取到恢复的登录用户信息,再跳转到主业务页,代码如下:

DispatchQueue.main.async {
    guard let validUser = user, let profile = validUser.profile else {
        // 信息获取异常 fallback 到登录态
        let loginVC = LoginViewController()
        self.window?.rootViewController = UINavigationController(rootViewController: loginVC)
        self.window?.makeKeyAndVisible()
        return
    }
    // 提取你需要的基础Profile信息
    let userEmail = profile.email
    let userFullName = profile.name
    let userGivenName = profile.givenName
    let userFamilyName = profile.familyName
    let userAvatarUrl = profile.imageURL(withDimension: 320)
    // 提取身份验证token
    let userIdToken = validUser.authentication.idToken ?? ""
    let userAccessToken = validUser.authentication.accessToken
    
    // 你可以在此处将信息存入全局变量、UserDefaults,或者传递给主界面
    // 示例:跳转到你自己的主业务页,替换为项目实际的主控制器类
    let mainVC = YourMainViewController() 
    self.window?.rootViewController = UINavigationController(rootViewController: mainVC)
    self.window?.makeKeyAndVisible()
}

注意事项

  • 请将代码中的YourMainViewController替换为你项目实际的主界面控制器类
  • 如果你不需要用导航栏包裹控制器,删除对应UINavigationController的包裹代码即可
  • 提取到的用户信息可根据业务需求自行处理,比如直接在主界面展示、传给后端做鉴权等

内容的提问来源于stack exchange,提问作者ehounder

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.03 08:57:02