0

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

现在我完成了集成 Google 登录部分。但是登录后,视图又回到了最初的 View Controller。

我的故事板看起来像这样,初始视图控制器内的蓝色视图是 Google 登录按钮。

下面是我的didSignInFor功能:

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

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

感谢帮助!

4

3 回答 3

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)

Storyboard ID您还需要从 Identity Inspector分配一件事,即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)
    }
}
于 2019-03-18T06:59:17.210 回答
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 的路径。

于 2019-03-18T06:39:41.223 回答
0

如果有人仍然遇到此问题,我使用此代码重定向到不同的视图控制器:

    // redirects to signed in user view controller
        let mainStoryboard:UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
        let vc = mainStoryboard.instantiateViewController(withIdentifier: "User_Feed_View")
        UIApplication.shared.windows.first?.rootViewController? = vc
        UIApplication.shared.windows.first?.makeKeyAndVisible()

我在 didSignInFor 签名函数内的 appDelegate 中使用了它,它似乎有效。

func sign(_ signIn: GIDSignIn!, didSignInFor user: GIDGoogleUser!,
              withError error: Error!) {
      if let error = error {
        if (error as NSError).code == GIDSignInErrorCode.hasNoAuthInKeychain.rawValue {
          print("The user has not signed in before or they have since signed out.")
        } else {
          print("\(error.localizedDescription)")
        }
        return
      }
      // Perform any operations on signed in user here.
      let userId = user.userID                  // For client-side use only!
      let idToken = user.authentication.idToken // Safe to send to the server
      let fullName = user.profile.name
      let givenName = user.profile.givenName
      let familyName = user.profile.familyName
      let email = user.profile.email
     
        
        
        // redirects to signed in user's view controller
        let mainStoryboard:UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
        let vc = mainStoryboard.instantiateViewController(withIdentifier: "User_Feed_View")
        UIApplication.shared.windows.first?.rootViewController? = vc
        UIApplication.shared.windows.first?.makeKeyAndVisible()
        
    }
于 2021-06-14T16:27:03.867 回答