如何解决如何将登录与 Google 和 Firebase 登录故事板、xcode合并
我有一个带有 Firebase 登录系统的应用。现在使用 Firebase 添加 Login with Google 非常容易。但是当我想要两者时并不容易,因此用户可以选择使用 Google 登录或使用电子邮件登录(Firebase)。有人可以帮我吗?我当前的代码如下:
import UIKit
import FirebaseAuth
import GoogleSignIn
//adding padding to textfields
extension UITextField {
func setpadding() {
let paddingView = UIView(frame: CGRect(x: 0,y: 0,width: 10,height: self.frame.height))
self.leftView = paddingView
self.leftviewmode = .always
}
}
//functie aanmaken voor keyboard verdwijnen als je klikt naast het tekst veld
extension UIViewController {
func hideKeyboardWhenTappedAround() {
let tap = UITapGestureRecognizer(target: self,action: #selector(UIViewController.dismissKeyboard))
tap.cancelstouchesInView = false
view.addGestureRecognizer(tap)
}
@objc func dismissKeyboard() {
view.endEditing(true)
}
}
class LoginViewController: UIViewController {
//
// adding fields,buttons and labels for the email sign in
//
//Label
private let label: UILabel = {
let label = UILabel()
label.textAlignment = .center
label.text = "Log in"
label.textColor = .white
label.font = UIFont(name: "Poppins-Bold",size: 30)
return label
}()
//Email
private let emailField: UITextField = {
let emailField = UITextField()
emailField.placeholder = "Email Address"
emailField.autocapitalizationType = .none
emailField.backgroundColor = .white
emailField.layer.cornerRadius = 13
emailField.setpadding()
return emailField
}()
//Password
private let passwordField: UITextField = {
let passwordField = UITextField()
passwordField.backgroundColor = .white
passwordField.placeholder = "Password"
passwordField.autocapitalizationType = .none
passwordField.isSecureTextEntry = true
passwordField.layer.cornerRadius = 13
passwordField.setpadding()
return passwordField
}()
//Button
private let button: UIButton = {
let button = UIButton()
button.backgroundColor = .white
button.setTitleColor(UIColor.Blue.darkBlue,for: .normal)
button.setTitle("Lets Get Started",for: .normal)
button.titleLabel?.font = UIFont(name: "Poppins-Bold",size: 20)
button.layer.cornerRadius = 13
return button
}()
//Button ga verder
private let GaVerderButton: UIButton = {
let button = UIButton()
button.backgroundColor = .white
button.setTitleColor(UIColor.Blue.darkBlue,for: .normal)
button.setTitle("Proceed to app",size: 35)
button.layer.cornerRadius = 13
return button
}()
//!!!!login with google button!!!!!
@IBOutlet var signInButton: GIDSignInButton!
//when view has loaded
override func viewDidLoad() {
super.viewDidLoad()
//add fields etc.
view.addSubview(label)
view.addSubview(emailField)
view.addSubview(passwordField)
view.addSubview(button)
//functie oproepen voor keyboard verdwijnen als je klikt naast het tekst veld
self.hideKeyboardWhenTappedAround()
//!!!!!!!!sign in with google button!!!!!!!!
GIDSignIn.sharedInstance()?.presentingViewController = self
//button voor aanmelden met email oproepen door functie uit te voeren als je klikt
button.addTarget(self,action: #selector(didTapButton),for: .touchUpInside)
if FirebaseAuth.Auth.auth().currentUser != nil {
label.isHidden = true
emailField.isHidden = true
passwordField.isHidden = true
button.isHidden = true
signInButton.isHidden = true
//add proceed button when logged in
view.addSubview(GaVerderButton)
GaVerderButton.frame = CGRect(x: 20,y: 300,width: view.frame.size.width-30,height: 100)
//call function if button proceed is tapped
GaVerderButton.addTarget(self,action: #selector(ProceedTapped),for: .touchUpInside)
}
}
//make function if button proceed is tapped
@objc private func ProceedTapped(){
//Door naar storyboard van de daadwerkelijke app
print("Proceed clicked")
performSegue(withIdentifier: "goToAppIdentifier",sender: self)
}
//subiews (places etc)
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
label.frame = CGRect(x: 0,y: 100,width: view.frame.size.width,height: 80)
emailField.frame = CGRect(x: 35,y: label.frame.origin.y+label.frame.size.height+20,width: view.frame.size.width-70,height: 50)
passwordField.frame = CGRect(x: 35,y: emailField.frame.origin.y+emailField.frame.size.height+20,height: 50)
button.frame = CGRect(x: 35,y: passwordField.frame.origin.y+passwordField.frame.size.height+20,height: 50)
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
}
@objc private func didTapButton(){
//test voor onszelf of de button activity is doorgekomen
print("Continue button tapped")
guard let email = emailField.text,!email.isEmpty,let password = passwordField.text,!password.isEmpty
else{
print("Email or Password is empty")
return
}
//login to account
FirebaseAuth.Auth.auth().signIn(withEmail: email,password: password,completion: { [weak self] result,error in
guard let strongSelf = self else{
return
}
guard error == nil else{
strongSelf.showCreateAccount(email: email,password: password)
return
}
print("Already signed In")
strongSelf.label.isHidden = true
strongSelf.emailField.isHidden = true
strongSelf.passwordField.isHidden = true
strongSelf.button.isHidden = true
strongSelf.signInButton.isHidden = true
strongSelf.emailField.resignFirstResponder()
strongSelf.passwordField.resignFirstResponder()
})
}
//pop-up if you dont have an account
func showCreateAccount(email: String,password: String){
let alert = UIAlertController(title: "Create account",message: "Would you like to create an account?",preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "Continue",style: .default,handler: {_ in
FirebaseAuth.Auth.auth().createuser(withEmail: email,error in
guard let strongSelf = self else{
return
}
guard error == nil else{
print("Account creation Failed")
return
}
print("Signed In")
strongSelf.label.isHidden = true
strongSelf.emailField.isHidden = true
strongSelf.passwordField.isHidden = true
strongSelf.button.isHidden = true
strongSelf.signInButton.isHidden = true
strongSelf.emailField.resignFirstResponder()
strongSelf.passwordField.resignFirstResponder()
})
}))
alert.addAction(UIAlertAction(title: "Cancel",style: .cancel,handler: {_ in }))
present(alert,animated: true)
}
}
我在 AppDelegate 中还有一些其他关于“使用 Google 登录”的代码
func application(_ application: UIApplication,didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
FirebaseApp.configure()
GIDSignIn.sharedInstance()?.clientID = "........................."
GIDSignIn.sharedInstance()?.delegate = self
return true
}
func application(_ app: UIApplication,open url: URL,options: [UIApplication.OpenURLOptionsKey : Any] = [:]) -> Bool {
return GIDSignIn.sharedInstance().handle(url)
}
func application(_ application: UIApplication,configurationForConnecting connectingScenesession: UIScenesession,options: UIScene.Connectionoptions) -> UISceneConfiguration {
// Called when a new scene session is being created.
// Use this method to select a configuration to create the new scene with.
return UISceneConfiguration(name: "Default Configuration",sessionRole: connectingScenesession.role)
}
func application(_ application: UIApplication,diddiscardScenesessions scenesessions: Set<UIScenesession>) {
// Called when the user discards a scene session.
// If any sessions were discarded while the application was not running,this will be called shortly after application:didFinishLaunchingWithOptions.
// Use this method to release any resources that were specific to the discarded scenes,as they will not return.
}
func sign(_ signIn: GIDSignIn!,didSignInFor user: GIDGoogleUser!,withError error: Error!) {
if (error == nil) {
print("Logged in with Google")
///////dont kNow what susposed to be here (thats my question)
/////// because i want login with google aside login with email
} else {
print("\(error.localizedDescription)")
}
}
}
如果有人有想法,我会很高兴!
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。