Swift:Google登录后转到其他视图控制器 [英] Swift: Go to other view controller after Google Sign In
问题描述
用户成功登录后,我希望屏幕自动显示选项卡控制器视图.
After users successfully signing in, I want the screen to show the tab controller view automatically.
现在,我完成了集成Google Sign In的部分.但是登录后,视图将返回到初始的View Controller.
Now I finished the integrating Google Sign In part. But after signing in, the view return to the initial View Controller.
我的故事板如下所示,初始View Controller中的蓝色View是Google登录按钮.
My storyboard looks like this, the blue View inside the initial View Controller is the Google Sign In button.
下面是我的didSignInFor
函数:
func sign(_ signIn: GIDSignIn!, didSignInFor user: GIDGoogleUser!,
withError error: Error!) {
if let error = error {
print("\(error.localizedDescription)")
} else {
//...
}
}
我知道我应该在else{}
中添加代码,但是仍然不确定该怎么做.
I know I should add codes in else{}
, but still not sure how to do.
感谢帮助!
推荐答案
对于您的情况,首先需要为UITabBarController
创建一个类,该类将确认UITabBarController
而不是UIViewController
之类的东西:
For your case first of all you need to create a class for your UITabBarController
which will confirm UITabBarController
not UIViewController
something like:
class TabBarConroller: UITabBarController {
TabBarConroller
是新的.swift
文件.现在转到情节提要,单击TabBarController
,然后单击Identity Inspector并将新创建的类分配给它.
TabBarConroller
is your new .swift
file. Now go to your storyboard and click on your TabBarController
and click on Identity Inspector and assign this newly created class to it.
接下来,如果用户使用以下代码成功进行身份验证,则需要启动该类:
Next you need to initiate that class if user successfully authenticate with below code:
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
.
And one more thing you need to assign in Storyboard ID
from Identity Inspector which will be TabbarIdentifier
.
因此您的代码将如下所示:
So your code will look like:
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)
}
}
这篇关于Swift:Google登录后转到其他视图控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!