I am trying to have my login function for firebase run after updating it, but the function is nil and I am getting the following messages:
Cannot create a URL to fetch a configuration with empty app instance ID or empty platform (app instance, platform): (nil), ios
[Firebase/Analytics][I-ACS023125] Unable to get configuration. Invalid server URL.
My code is as follows:
#IBAction func Login(sender: AnyObject) {
let email = self._Email.text!
let password = self._Password.text!
print("below is the email")
print(email)
print("below is the password")
print(password)
Auth.auth().createUser(withEmail: email, password: password) { (user, error) in
if error == nil {
print("Successful login")
if user!.isEmailVerified{
let vc = self.storyboard!.instantiateViewController(withIdentifier: "ProfileView") as! ProfileView
self.present(vc, animated: true, completion: nil)
} else {
print("nil is hitting")
}
} else {
print("Some nil error")
}
}
}
Related
I have question about window hierarchy. When I try to login or sign up and open the home page of app, I can't present other pages console error give that:
Warning: Attempt to present on whose view is not in the window hierarchy!
But when I re-open my app with same user everything working correctly. Here the code which is after create user process:
func createUser(withEmail email: String, password: String, username: String) {
Auth.auth().createUser(withEmail: email, password: password) { (result, error) in
if let error = error {
print("Failed to sign user up with error: ", error.localizedDescription)
return
}
guard let uid = result?.user.uid else { return }
let values = ["E-mail": email, "Kullanıcı Adı": username]
Database.database().reference().child("users").child(uid).updateChildValues(values, withCompletionBlock: { (error, ref) in
if let error = error {
print("Failed to update database values with error: ", error.localizedDescription)
return
}
let layout = UICollectionViewFlowLayout()
let homeController = HomeController(collectionViewLayout: layout)
let navController = UINavigationController(rootViewController: homeController)
self.present(navController, animated: false, completion: nil)
})
}
}
here the function which call top function:
#objc func handleSignUp() {
guard let email = emailTextField.text else { return }
guard let password = passwordTextField.text else { return }
guard let username = usernameTextField.text else { return }
createUser(withEmail: email, password: password, username: username)
}
Maybe your rootViewController is presenting another ViewController.
You can try :
if let presentedVC = self.presentedViewController {
presentedVC.present(navController, animated: false, completion: nil)
} else {
self.present(navController, animated: false, completion: nil)
}
My rootViewController works and loads fine. However, when I call it using the present(viewControllerToPresent: UIViewController, animated: true, completion: nil) from another viewController I get nothing but a black screen.
I looked all over the stackOverflow but only found solutions for storyboard users. I am doing this programmatically
#objc func handleLogin() {
print("LOGIN BUTTON TOUCHED")
guard let email = emailTextField.text, let password = passwordTextField.text else {
print("Form is not valid.")
return
}
Auth.auth().signIn(withEmail: email, password: password) { (user, error) in
if error != nil {
print(error!)
return
}
let viewController = RootViewController()
*** self.present(viewController, animated: true, completion: nil)
print("Logged in")
}
}
There is nothing wrong with self.present(viewController, animated: true, completion: nil)
I think your RootViewController() is already presented. The black screen you see is might be the one without any data? I am not sure, will need your code for that class.
Another approach you can consider is to replace the real rootViewController from current uiWindow like this
#objc func handleLogin() {
print("LOGIN BUTTON TOUCHED")
guard let email = emailTextField.text, let password = passwordTextField.text else {
print("Form is not valid.")
return
}
Auth.auth().signIn(withEmail: email, password: password) { [weak self] (user, error) in
guard let strongSelf = self else { return }
guard error == nil else { return }
guard let user = user else { return }
UIApplication.shared.keyWindow?.rootViewController = RootViewController()
print("Logged in")
}
}
you've created a new instance(RootViewController) and it must be black .. your not referring to your RootViewController
I am trying to run the login function for firebase, which worked fine, but after updating swift version it doesn't work. When I check both if the error is nil and not nil neither print statement runs, but the prints before and after does. Does anyone know why this would not be running, but also not throwing error?
Below is the code:
FIRAuth.auth()?.signIn(withEmail: email, password: password, completion: { user, error in
if error == nil {
print("Successful login")
if user!.isEmailVerified {
let vc = self.storyboard!.instantiateViewController(withIdentifier: "ProfileView") as! ProfileView
self.present(vc, animated: true, completion: nil)
} else {
print("nil is hitting")
}
}
})
print("done running login")
Use this Auth instead of FIRAuth.
Auth.auth().createUser(withEmail: email, password: password) { (user, error) in
// ...
}
To know more read
Firebase Documention
To signIn in Firebase, you need to use Auth instead FIRAuth
Auth.auth().signIn(withEmail: "email#email.com",
password: "123456") { (user, error) in
if error == nil {
print("successful login")
}
}
Auth.auth().signIn(withEmail: email!, password: password!) { (user, error) in
if let error = error { print("Enter Valid email and password")
}else{
if Auth.auth().currentUser!.isEmailVerified == true{
let vc = self.storyboard!.instantiateViewController(withIdentifier: "ProfileView") as! ProfileView
self.present(vc, animated: true, completion: nil)}}}
I currently have login authentication working, as well as registration with firebase. I now wanted to keep my user logged in, which is working. The problem I am facing is when I log out, I try to log back in and I receive this error:
// Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Receiver () has no segue with identifier 'chatRoom''\
Which does not make sense, because when I first log in, it brings me to the ChatRoom Viewcontroller using the chatRoom segue.
This is my code below:
func handleLogin() {
guard let email = emailTextField.text, let password = passwordTextField.text
else {
print("Form is not valid")
return
}
Auth.auth().signIn(withEmail: email, password: password, completion: { (user, error) in
if error != nil {
print(error!.localizedDescription)
self.loginErrorAlert("Error!", message: "Username or password incorrect, please try again.")
return
}
// successfully logged in our user and keep user logged in until they logout
if Auth.auth().currentUser != nil {
UserDefaults.standard.set(Auth.auth().currentUser!.uid, forKey: "loggedIn")
UserDefaults.standard.synchronize()
self.performSegue(withIdentifier: "chatRoom", sender: self)// this is the error I am facing
}
})
}
func handleRegister() {
guard let email = emailTextField.text, let password = passwordTextField.text, let name = nameTextField.text else {
print("Form is not valid")
signupErrorAlert("Error!", message: "Could not be Registered at this time, please try again.")
return
}
Auth.auth().createUser(withEmail: email, password: password, completion: { (user: User?, error) in
if error != nil {
print(error!.localizedDescription)
self.loginErrorAlert("Error!", message: "Could not be Registered at this time, please try again later.")
return
}
guard let uid = user?.uid else {
return
}
// successfully authenticated user and keep logged in until they logout
let ref = Database.database().reference(fromURL: "https://boccighub.firebaseio.com/")
let usersReference = ref.child("users").child(uid)
let values = ["name": name, "email": email]
usersReference.updateChildValues(values, withCompletionBlock: { (err, ref) in
if err != nil {
print(err!.localizedDescription)
return
}
if Auth.auth().currentUser != nil {
UserDefaults.standard.set(Auth.auth().currentUser!.uid, forKey: "loggedIn")
UserDefaults.standard.synchronize()
self.performSegue(withIdentifier: "chatRoom", sender: self)
}
})
})
}
// User logged out
#IBAction func handleLogout(_ sender: Any) {
do {
try Auth.auth().signOut()
print("user signedout")
if Auth.auth().currentUser == nil {
print("No user, key removed")
UserDefaults.standard.removeObject(forKey: "loggedIn")
UserDefaults.standard.synchronize()
print("User logged out")
let loginController = LoginViewController()
present(loginController, animated: true, completion: nil)
}
} catch let logoutError {
print(logoutError)
}
}
Fixed. I Removed the let loginController = LoginViewController(),
I created a new segue and now do self.performSegue(withIdentifier: "logOut", sender: self), works perfectly
Why is it that when I type in an email and password nothing shows up in Firebase? I've done it before with no issues.
No user entry is made when i check my Authentication tab in firebase.
//Sign up user
let email = emailTextField.text
let password = passwordTextField.text
FIRAuth.auth()?.createUser(withEmail:email! , password: password!, completion: { (user, error) in
if error != nil {
self.login()
} else {
print("User created!")
self.login()
}
})
}
#IBAction func signInButton(_ sender: Any) {
}
//Functions
func login(){
FIRAuth.auth()?.signIn(withEmail: emailTextField.text!, password: passwordTextField.text!, completion: { (user, error) in
if error != nil {
print("Password or email has not been entered correctly")
} else {
print("Successful Login")
}
})
}
}
I don't know if it will fix your problem, but try declaring with a guard statement, and remove your bangs (!) in the signup/login. It should at least be a lot less error prone.
Guard let email = emailTextField.text else { return }
Guard let password = passwordTextField.text else { return }
Now remove bangs so it looks like this
FIRAuth.auth()?.createUser(withEmail: email , password: password, completion: { (user, error) in
And do the same for your login function, unwrap your textField texts with a guard statement.
Cheers.
I was using the wrong GoogleService-info.plist file. I kept using the same one because i didn't think i had to keep re-downloading them for each app.