Swift:Google登录后转到其他视图控制器

问题描述 投票:0回答:2

用户成功登录后,我希望屏幕自动显示选项卡控制器视图。

现在我完成了Google登录部分的整合。但登录后,视图将返回初始View Controller。

我的故事板看起来像这样,初始View Controller中的蓝色View是Google Sign In按钮。

下面是我的didSignInFor函数:

func sign(_ signIn: GIDSignIn!, didSignInFor user: GIDGoogleUser!,
          withError error: Error!) {
    if let error = error {
        print("\(error.localizedDescription)")
    } else {
        //...
    }
}

我知道我应该在else{}中添加代码,但仍然不知道该怎么做。

感谢帮助!

ios swift google-signin google-login gidsignin
2个回答
2
投票

对于你的情况首先你需要为你的UITabBarController创建一个类,它将确认UITabBarController而不是UIViewController类似于:

class TabBarConroller: UITabBarController {

TabBarConroller是您的新.swift文件。现在转到您的故事板并单击您的TabBarController并单击Identity Inspector并将此新创建的类分配给它。

接下来,如果用户使用以下代码成功进行身份验证,则需要启动该类:

let storyboard = UIStoryboard(name: "Main", bundle: nil)
let tabbarVC = storyboard.instantiateViewController(withIdentifier: "TabbarIdentifier") as! UITabbarController
self.present(tabbarVC, animated: false, completion: nil)

还有一件事你需要在Identity Inspector中分配Storyboard ID,这将是TabbarIdentifier

所以你的代码看起来像:

func sign(_ signIn: GIDSignIn!, didSignInFor user: GIDGoogleUser!,
      withError error: Error!) {
    if let error = error {
        print("\(error.localizedDescription)")
    } else {
        let storyboard = UIStoryboard(name: "Main", bundle: nil)
        let tabbarVC = storyboard.instantiateViewController(withIdentifier: "TabbarIdentifier") as! UITabbarController
        self.present(tabbarVC, animated: false, completion: nil)
    }
}

0
投票
var loginType : String = ""
var tokenFb : String = ""
var email : String = ""
var googleName : String = ""
// Google Integration

func sign(_ signIn: GIDSignIn!, didSignInFor user: GIDGoogleUser!,
          withError error: Error!) {
    if let error = error {
        print("\(error.localizedDescription)")
    } else {

        self.loginType = "gmail"
        // Perform any operations on signed in user here.
        tokenFb = user.userID // For client-side use only!
        let idToken = user.authentication.idToken // Safe to send to the server
        googleName = user.profile.name
        let givenName = user.profile.givenName
        let familyName = user.profile.familyName
        email = user.profile.email

        if user.profile.hasImage
        {
            let imageUrl = signIn.currentUser.profile.imageURL(withDimension: 150)

            image = (imageUrl?.absoluteString)!
            print(" image url: ", image)
            self.profileURL = image
        }
        self.loginSocialAPI()
    }
}

self.logSocialApi()是我可以登录的API,并且api需要设置特定viewController的路径。

© www.soinside.com 2019 - 2024. All rights reserved.