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

时间:2019-03-18 06:34:51

标签: ios swift google-signin google-login gidsignin

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

现在,我完成了集成Google Sign In的部分。但是登录后,视图将返回到初始的View Controller。

我的故事板如下所示,初始View Controller中的蓝色View是Google登录按钮。

下面是我的didSignInFor函数:

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

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

感谢帮助!

3 个答案:

答案 0 :(得分:1)

对于您的情况,首先需要为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中的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) } } 进行分配。

因此您的代码将如下所示:

function selectElementContents(el) {
  var body = document.body,
    range, sel;
  if (document.createRange && window.getSelection) {
    range = document.createRange();
    sel = window.getSelection();
    sel.removeAllRanges();
    try {
      range.selectNodeContents(el);
      sel.addRange(range);
    } catch (e) {
      range.selectNode(el);
      sel.addRange(range);
    }
  } else if (body.createTextRange) {
    range = body.createTextRange();
    range.moveToElementText(el);
    range.select();
  }
}

答案 1 :(得分: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的路径。

答案 2 :(得分: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()
        
    }