我创建了一个带有谷歌功能的登录,一切都与firebase和身份验证的登录有关。我的问题是,在成功登录后,我希望应用程序移动到与应用程序delegate.
不同的页面/控制器
let storyboard = UIStoryboard(name: "Main", bundle: nil);
let viewController: SigninViewController = storyboard.instantiateViewController(withIdentifier: "GoogleTabController") as! SigninViewController;
// Then push that view controller onto the navigation stack
let rootViewController = self.window!.rootViewController as! UINavigationController;
rootViewController.pushViewController(viewController, animated: true);
给了我一个错误,上面写着";无法将uitabbarcontroller类型的值强制转换为signinViewcontroller";
这是我的完整代码:
func sign(_ signIn: GIDSignIn!, didSignInFor user: GIDGoogleUser!, withError error: Error!) {
guard let authentication = user.authentication else { return }
let credential = GoogleAuthProvider.credential(withIDToken: authentication.idToken,
accessToken: authentication.accessToken)
let number = Int.random(in: 0..<100)
let numberString = String(number)
let db = Firestore.firestore()
let googleName = user.profile.name
let googleEmail = user.profile.email
let googleUsername = user.profile.name+numberString
let country = ""
let bio = ""
let profImg = user.profile.imageURL(withDimension: 100)?.absoluteString
let password = ""
Auth.auth().signInAndRetrieveData(with: credential) { (authResulft, error) in
if let error = error{
print(error)
}else{
let userid = Auth.auth().currentUser!.uid
db.collection("users").whereField("uid", isEqualTo: userid).getDocuments(){querySnapshot, error in
if let error = error {
print(error.localizedDescription)
}else
{
if querySnapshot!.isEmpty{
db.collection("users").document(userid).setData([
"name": googleName!,
"username": googleUsername,
"email": googleEmail!,
"password": password,
"uid": userid,
"country": country,
"bio": bio,
"profileImage": profImg!,
]) { err in
if err != nil {
} else {
print("logged in success and db stored")
//below code doesnt work
let storyboard = UIStoryboard(name: "Main", bundle: nil);
let viewController: SigninViewController = storyboard.instantiateViewController(withIdentifier: "GoogleTabController") as! SigninViewController;
// Then push that view controller onto the navigation stack
let rootViewController = self.window!.rootViewController as! UINavigationController;
rootViewController.pushViewController(viewController, animated: true);
}
}
}else{
//here perform segue
let storyboard = UIStoryboard(name: "Main", bundle: nil);
let viewController: SigninViewController = storyboard.instantiateViewController(withIdentifier: "GoogleTabController") as! SigninViewController;
// Then push that view controller onto the navigation stack
let rootViewController = self.window!.rootViewController as! UINavigationController;
rootViewController.pushViewController(viewController, animated: true);
}
}
}
}
}
}
问题在这里
let viewController: SigninViewController = storyboard.instantiateViewController(withIdentifier: "GoogleTabController") as! SigninViewController;
GoogleTabController
是一个选项卡ViewController而不是SigninViewController
您需要使用
let viewController = storyboard.instantiateViewController(withIdentifier: "GoogleTabController")!