I am using the Parse login controller (default Parse one they offer) to implement a user login/pass for my app. It works fine, I am able to log in, register, and the whole thing. Only thing is that I want to add an alert if the password is wrong, right now it only println("Failed to login...") when the password is wrong, and the user has no idea that their password is wrong. I added an alert, but some reason it isn't appearing. The error message I get is "Warning: Attempt to present on whose view is not in the window hierarchy!"
Here is the alert I added to my code:
func logInViewController(logInController: PFLogInViewController!, didFailToLogInWithError error: NSError!) {
println("Failed to login...")
let alertView = UIAlertController(title: "Failed to login...", message: "Incorrect password or username.", preferredStyle: .Alert)
alertView.addAction(UIAlertAction(title: "Ok", style: .Default, handler: nil))
presentViewController(alertView, animated: true, completion: nil)
}
I think it might be because the parse default login screen I am using that they provided is not on my storyboard, but called from the Parse framework(?). How do I go about adding an alert view?
Thank you!
Josh
Try this
logInController.presentViewController(alertView, animated: true, completion: nil)
I think parse displays an alert when you log in with invalid credentials, so this might show 2nd - after there alert is dismissed.
Related
I'm creating a function in my Login View controller. Where if the users password or email Address is incorrect he gets an alert saying 'Login not Successful'. But if the users password and email matches he gets to segue to the Dashboard view controller.
func alert(response:String) {
let alert = UIAlertController(title: "Login Failure", message: "Incorrect Email Address, Phone Number or Password", preferredStyle: .alert)
if response == "Login in not sucessful" {
alert.addAction(UIAlertAction(title:"Okay", style: .cancel,handler:nil))
alert.dismiss(animated: true, completion: nil)
self.present(alert, animated: true, completion: nil)
} else if response == "Login Successful" {
self.performSegue(withIdentifier: "dashboardSegue", sender: self)
}
But whenever I tap the Log in button I either get one of these errors:
Warning: Attempt to present <myApp.customTabBar: 0x7fe68a889800> on <myApp.ViewController: 0x7fe68a606f90> while a presentation is in progress!
Warning: Attempt to present <UIAlertController: 0x7fe68b853e00> on myApp.ViewController: 0x7fe68a606f90> while a presentation is in progress!
Warning: Attempt to present <UIAlertController: 0x7fe68b815400> on <myApp.ViewController: 0x7fe68a475240> whose view is not in the window hierarchy!
the alert shows only once and the app segues anyways to the dashboard viewcontroller even though the users email and password are incorrect. I'm thinking maybe its the if-condition statement. I don't whats wrong. I've checked other questions but I can't quite use the viewDidAppear method because I don't want the alert to appear on startup. Newbie Here , Thanks for any help !!!
Change following line of code will fix your issue.
alert.addAction(UIAlertAction(title:"Okay", style: .cancel,handler:nil))
alert.dismiss(animated: true, completion: nil)
self.present(alert, animated: true, completion: nil)
To
alert.addAction(UIAlertAction(title:"Okay", style: .cancel, handler:nil))
self.present(alert, animated: true, completion: nil)
I have a login screen that raises an alert when login fails. The code that calls the alert is run from a callback closure which itself calls a function in the main VC.
_ = UserProfile.logIn(emailAddressLabel.text!, passwordLabel.text!) { success in
if success {
self.performSegue(withIdentifier: "mainTabBarSegue", sender: nil)
}
else {
self.displayFailedLoginAlert()
}
self.loggingIn = false
}
and the displayFailedLoginAlert looks like this:
func displayFailedLoginAlert () {
let alert = UIAlertController(title: "Login", message: "Login Failed", preferredStyle: UIAlertControllerStyle.alert)
alert.addAction(UIAlertAction(title: "Dismiss", style: UIAlertActionStyle.default, handler: { _ in
alert.dismiss(animated: false, completion: nil)
}))
self.present(alert, animated: false, completion: nil)
}
However, when I do this I get:
Warning: Attempt to present <UIAlertController: 0x7ff8fd0b5800> on <LoginViewController: 0x7ff8fcc0deb0> which is already presenting <UIAlertController: 0x7ff8fe0cca00>
I have tried a number of different approaches and either get this or a crash if I use a UIAlertController as a class member. What am I doing wrong, I just can't see it?
You don't need to tell the alert to dismiss at all. The default behavior when tapping an action in a UIAlertController is that the alert will dismiss. Just pass nil to the handler.
The issue was that, each time the user logged in I added an observer to the API call. So, on the second login attempt the closure was called twice and so raised the error. Thanks to Frizzo for pointing me in the right direction
I am writing a swift/Xcode app and have a particular page where a user can upload a picture, which is saved to a database. Upon success of the upload, I give the user a pop up telling them that the save was successful. Here is the relevant code for that bit:
func displayAlert(title: String, message: String){ //all possible alerts go into here
if #available(iOS 8.0, *) {
let alert = UIAlertController(title: title, message: message, preferredStyle: UIAlertControllerStyle.Alert)
alert.addAction(UIAlertAction(title: "Ok", style: .Default, handler: { (action) in
self.dismissViewControllerAnimated(true, completion: nil)
}))
self.presentViewController(alert, animated: true, completion: nil)
} else {
// Fallback on earlier versions
}
}
Thats the function that is called. Here is where and how it is called:
if error == nil{
self.imageToPost.image = UIImage(named: "blah.jpg" )
self.message.text = "" //input fields are set back to original
self.displayAlert("Success", message: "Your image has been uploaded")
} else{}
If the image is uploaded successfully, which it is, the error returns nil and the code runs. The user gets the alert, clicks ok, and then... the app resets. It doesn't close out, but immediately goes to the main page viewcontroller (which is not this page's viewcontroller)
I have deduced that the displayAlert function must be at fault, because commenting out the self.displayAlert("Success", .....) fixes the issue, and the app does not reset. But I see nothing in the syntax that would cause this issue. (There is the exact same function in the main page's swift file - I copied and pasted it over - could that be the issue?). I am stumped
Your call to self.dismissViewControllerAnimated(true, completion: nil) dismisses your view and shows your previous ViewController (Main) in the stack.
I'm working on a google sign in tutorial to my ios app and there's a part when we cannot log in user to my app.
So far the code section in appDelegate.swift looks like:
guard error == nil && data != nil else {
// check for fundamental networking error
print("error=\(error)")
//lets put popup here that says cant connect to server
GIDSignIn.sharedInstance().signOut()
return
}
and now instead of printing the error I want to put the alert popup window. I tried to write there:
let alert = UIAlertController(title: "Alert", message: "Message", preferredStyle: UIAlertControllerStyle.Alert)
alert.addAction(UIAlertAction(title: "Click", style: UIAlertActionStyle.Default, handler: nil))
self.presentViewController(alert, animated: true, completion: nil)
but then I'm getting the xcode error near self.presentViewController saying that value of type AppDelegate has no member presentViewController.
How can I display an alert popup in that case?
In Swift 3
self.window?.rootViewController?.present(alert, animated: true, completion: nil)
Try Using this line :-
self.window?.rootViewController?.presentViewController(alert, animated: true, completion: nil)
Here you need is a viewController object to present the AlertController, Hope this will help you :)
I'm developing an iPhone app with Swift using Parse.
I've created a simple log in screen using Parse's PFLoginViewController. There are pop ups (UIAlertControllers) when the user does things like signing up without a username etc, but there is no pop up for when the log in credentials are invalid. Instead I just get an error in the console:
2015-08-23 22:50:10.246 SwifferApp[24429:1614072] [Error]: invalid login parameters (Code: 101, Version: 1.8.1)
I have a function for when the log in fails, but I can't present the UIAlertController over the PFLoginViewController
func logInViewController(logInController: PFLogInViewController, didFailToLogInWithError: NSError?){
var invalidCredentials = UIAlertController(title: "Invalid Credentials", message: "Incorrect username or password.", preferredStyle: UIAlertControllerStyle.Alert)
invalidCredentials.addAction(UIAlertAction(title: "Okay", style: .Default, handler: { (action: UIAlertAction!) in
//do nothing
}))
presentViewController(invalidCredentials, animated: true, completion: nil)
}
On the line with "presentViewController" i get the error
2015-08-23 22:50:10.249 SwifferApp[24429:1613783] Warning: Attempt to present <UIAlertController: 0x7c1f1a50> on <SwifferApp.TimelineTableViewController: 0x7c1b7120> whose view is not in the window hierarchy!
What can I do to include a log in error pop up?
Figured it out!
All I need to do was replace
presentViewController(invalidCredentials, animated: true, completion: nil)
with
logInViewController.presentViewController(invalidCredentials, animated: true, completion: nil)
The error describes the reason itself.
Warning: Attempt to present <UIAlertController: 0x7c1f1a50> on
<SwifferApp.TimelineTableViewController: 0x7c1b7120> whose view is not
in the window hierarchy!
Your TimelineTableViewController is not in view hierarchy. That's why alert is not showing up. If you want to still show the alert not being in the TimelineTableViewController then,
Replace this:
presentViewController(invalidCredentials, animated: true, completion: nil)
With this:
UIApplication.sharedApplication().keyWindow?.rootViewController?.presentViewController(invalidCredentials, animated: true, completion: nil)