password reset swift 4 firebase - swift

Hey guys so I'm doing registration form with Swift 4 and using Firebase.
I'm stuck on reseting the password. Whenever I click on the button "forget password" I should get a pop up window with textField to fill in my email address. But when I do click nothing happens. Posting code below if anyone have some ideas what can be wrong with it
#IBAction func forgotPasswordTapped(_ sender: Any) {
let forgotPasswordAlert = UIAlertController(title: "Forgot password?", message: "Enter email address", preferredStyle: .alert)
forgotPasswordAlert.addTextField { (textField) in
textField.placeholder = "Enter email address"
}
forgotPasswordAlert.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: nil))
forgotPasswordAlert.addAction(UIAlertAction(title: "Reset Password", style: .default, handler: { (action) in
let resetEmail = forgotPasswordAlert.textFields?.first?.text
Auth.auth().sendPasswordReset(withEmail: resetEmail!, completion: { (error) in
if error != nil{
let resetFailedAlert = UIAlertController(title: "Reset Failed", message: "Error: \(String(describing: error?.localizedDescription))", preferredStyle: .alert)
resetFailedAlert.addAction(UIAlertAction(title: "OK", style: .default, handler: nil))
self.present(resetFailedAlert, animated: true, completion: nil)
}else {
let resetEmailSentAlert = UIAlertController(title: "Reset email sent successfully", message: "Check your email", preferredStyle: .alert)
resetEmailSentAlert.addAction(UIAlertAction(title: "OK", style: .default, handler: nil))
self.present(resetEmailSentAlert, animated: true, completion: nil)
}
})
}))
}

As a quick answer, you are simply forgetting to present your forgotPasswordAlert. The fix is simple:
#IBAction func forgotPasswordTapped(_ sender: Any) {
let forgotPasswordAlert = UIAlertController(title: "Forgot password?", message: "Enter email address", preferredStyle: .alert)
forgotPasswordAlert.addTextField { (textField) in
textField.placeholder = "Enter email address"
}
forgotPasswordAlert.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: nil))
forgotPasswordAlert.addAction(UIAlertAction(title: "Reset Password", style: .default, handler: { (action) in
let resetEmail = forgotPasswordAlert.textFields?.first?.text
Auth.auth().sendPasswordReset(withEmail: resetEmail!, completion: { (error) in
if error != nil{
let resetFailedAlert = UIAlertController(title: "Reset Failed", message: "Error: \(String(describing: error?.localizedDescription))", preferredStyle: .alert)
resetFailedAlert.addAction(UIAlertAction(title: "OK", style: .default, handler: nil))
self.present(resetFailedAlert, animated: true, completion: nil)
}else {
let resetEmailSentAlert = UIAlertController(title: "Reset email sent successfully", message: "Check your email", preferredStyle: .alert)
resetEmailSentAlert.addAction(UIAlertAction(title: "OK", style: .default, handler: nil))
self.present(resetEmailSentAlert, animated: true, completion: nil)
}
})
}))
//PRESENT ALERT
self.present(forgotPasswordAlert, animated: true, completion: nil)
}
As a small aside, you'll want to make sure you are presenting your confirmation alerts on the main queue to avoid unexpected behavior. Perhaps the sendPasswordReset does this automatically, but I do not believe this to be the case. Additionally, the better way of getting the error description to present to the user is with optional binding (using if let).
Auth.auth().sendPasswordReset(withEmail: resetEmail!, completion: { (error) in
//Make sure you execute the following code on the main queue
DispatchQueue.main.async {
//Use "if let" to access the error, if it is non-nil
if let error = error {
let resetFailedAlert = UIAlertController(title: "Reset Failed", message: error.localizedDescription, preferredStyle: .alert)
resetFailedAlert.addAction(UIAlertAction(title: "OK", style: .default, handler: nil))
self.present(resetFailedAlert, animated: true, completion: nil)
} else {
let resetEmailSentAlert = UIAlertController(title: "Reset email sent successfully", message: "Check your email", preferredStyle: .alert)
resetEmailSentAlert.addAction(UIAlertAction(title: "OK", style: .default, handler: nil))
self.present(resetEmailSentAlert, animated: true, completion: nil)
}
}
})

For swift 4.2
import Firebase
// MARK: Firebase Forgotpassword
func callFIRPasswordReset(){
//show loader
Auth.auth().sendPasswordReset(withEmail: self.txtEmail.text!) { (error) in
DispatchQueue.main.async {
//hide loader
self.txtEmail.text = ""
if let error = error {
//show alert here
print(error.localizedDescription)
}
else {
//show alert here
print("We send you an email with instructions on how to reset your password.")
}
}
}
}

Related

How to set keyboard type inside an alert

I have the following code which uses an alert to gather an email address from the user. I'd like to specify the keyboard type but haven't been able to figure out how to do that inside an alert. Is anyone able to help show how to set .keyboardType = UIKeyboardType.emailAddress?
var userInput: String = ""
let prompt = UIAlertController.init(title: nil, message: "Enter your email address", preferredStyle: UIAlertControllerStyle.alert)
let okAction = UIAlertAction.init(title: "OK", style: UIAlertActionStyle.default) { (action) in
userInput = prompt.textFields![0].text
if (userInput!.isEmpty) {
return
}
}
let cancelAction = UIAlertAction(title: "Cancel", style: .cancel) { (action) in
print(action)
}
prompt.addTextField(configurationHandler: nil)
prompt.addAction(okAction)
prompt.addAction(cancelAction)
self.view?.window?.rootViewController?.present(prompt, animated: true, completion: nil)
Switch this with the addTextfield line. Basically this is where you do the configuration for your textfield as it implies.
prompt.addTextField { (textfield) in
textfield.keyboardType = .emailAddress
}

How to add message Phone call alert popup?l

I wanna add message to phone call alert popup.
how to make it?
enter image description here
Yes, you can do it like that:
func phoneCall(to phoneNumber:String) {
if let callURL:URL = URL(string: "tel:\(phoneNumber)") {
let application:UIApplication = UIApplication.shared
if (application.canOpenURL(callURL)) {
let alert = UIAlertController(title: "Your Title", message: "Do you want call that number?", preferredStyle: .alert)
let callAction = UIAlertAction(title: "Call", style: .default, handler: { (action) in
application.openURL(callURL)
})
let noAction = UIAlertAction(title: "No", style: .cancel, handler: { (action) in
print("Canceled Call")
})
alert.addAction(callAction)
alert.addAction(noAction)
self.present(alert, animated: true, completion: nil)
}
}
}

How to get the index of item selected in Alert of UIAlertController Swift

I am going to use UIAlertController for a user to select one item.
The items to select are array as following:
let arraySelect = ["NewYork", "Washington", "Seoul", "Tokyo", "Peking", "Sidney", ... ]
let alert = UIAlertController(title: nil, message: nil, preferredStyle: .Alert)
// Add items in array to Alert
for var i = 0; i < arraySelect.count; i++ {
alert.addAction(UIAlertAction(title: arrayBibleVersions[i], style: .Default, handler: {(_) in }))
}
// Add cancel button.
alert.addAction(UIAlertAction(title: "취소", style: .Cancel, handler: {(_) in }))
self.presentViewController(alert, animated: false, completion: nil)
When a user touched one item, I have to get the index of the item that a user toched on.
But I don't know how to get the index..
Please help me.
I solved my question as following:
let arraySelect = ["NewYork", "Washington", "Seoul", "Tokyo", "Peking", "Sidney", ... ]
let alert = UIAlertController(title: nil, message: nil, preferredStyle: .Alert)
let closure = { (action: UIAlertAction!) -> Void in
let index = alert.actions.indexOf(action)
if index != nil {
NSLog("Index: \(index!)")
}
}
for var i = 0; i < arrayBibleVersions.count; i++ {
alert.addAction(UIAlertAction(title: arrayBibleVersions[i][1], style: .Default, handler: closure))
}
alert.addAction(UIAlertAction(title: "cancel", style: .Cancel, handler: {(_) in }))
self.presentViewController(alert, animated: false, completion: nil)
This is how I solved it in Swift 4 / iOS11
projects.forEach { project in
alertController.addAction(
UIAlertAction(title: project.name, style: .default, handler: { action in
if let index = alertController.actions.index(where: { $0 === action }) {
self.showProject(project: projects[index])
}
})
)
}
I have the same problem too and found the method below which saved me.
But be careful if the item in array are the same, it will return the first item's index.
Fortunately, It's work in this case, because we create the action and using action to search.
extension Array where Self.Element : Equatable {
...
#inlinable public func index(of element: Element) -> Int?
...
}
It work in Swift 4
let arraySelect = ["NewYork", "Washington", "Seoul", "Tokyo", "Peking", "Sidney"]
let alert = UIAlertController(title: nil, message: nil, preferredStyle: .alert)
// Add items in array to Alert
for name in arraySelect {
alert.addAction(UIAlertAction(title: name, style: .default, handler: {(action) in
// print idx that user selected
if let idx = alert.actions.index(of: action) {
print("idx = \(idx)")
}
}))
}
// Add cancel button.
alert.addAction(UIAlertAction(title: "취소", style: .cancel, handler: {(_) in }))
self.present(alert, animated: true, completion: nil)
This is working good:
var arraySelect = ["NewYork", "Washington", "Seoul", "Tokyo", "Peking", "Sidney"]
let alert = UIAlertController(title: nil, message: nil, preferredStyle: .Alert)
let closure = { (action: UIAlertAction!) -> Void in
let index = alert.actions.indexOf(action)
if index != nil {
NSLog("Index: \(index!)")
}
}
for field in arraySelect {
alert.addAction(UIAlertAction(title: field, style: .Default, handler: closure))
}
alert.addAction(UIAlertAction(title: "cancel", style: .Cancel, handler: {(_) in }))
self.presentViewController(alert, animated: false, completion: nil)
Block is executed when the user selects a action.
// Add cancel button.
alert.addAction(UIAlertAction(title: "취소", style: .Cancel, handler: {(_) in
//your code
}))
func FirefoxAhri(){
let optionMenu = UIAlertController(title: "title", message: "message", preferredStyle: UIAlertControllerStyle.ActionSheet)
let action1 = UIAlertAction(title: "Yes", style: UIAlertActionStyle.Destructive){
action -> Void in
//do stuff on click Yes
}
let action2 = UIAlertAction(title: "No", style: UIAlertActionStyle.Destructive){
action -> Void in
//do stuff on click No
}
let cancelAction: UIAlertAction = UIAlertAction(title: "Cancel", style: .Cancel) { action -> Void in
print("Cancel")
// do nothing if you don't want. alert closes automatically
}
optionMenu.addAction(action1)
optionMenu.addAction(action2)
optionMenu.addAction(cancelAction)
self.presentViewController(optionMenu, animated: true, completion: nil)
}

Can't add an Alert Action to UIAlertController (Swift)

I'm trying to add an Ok button to a UI Alert but I cannot add it using alertController.addAction
What do I do in this case?
Thanks in advance!!
if error == nil {
let alert: UIAlertController = UIAlertController(title: "Account Created", message: "Please confirm your email", preferredStyle: .Alert)
let okButton = UIAlertAction(title: "Ok", style: .Default) { action -> Void in
self.performSegueWithIdentifier("toMain", sender: self)
alertController.addAction(okButton)
self.presentViewController(alert, animated: true, completion: nil)
}
} else {
println("\(error)")
}
alert instead of alertController
alert.addAction should be outside of your okbutton action.
Change your code with :
if error == nil {
let alert: UIAlertController = UIAlertController(title: "Account Created", message: "Please confirm your email", preferredStyle: .Alert)
let okButton = UIAlertAction(title: "Ok", style: .Default) { action -> Void in
self.performSegueWithIdentifier("toMain", sender: self)
}
alert.addAction(okButton)
self.presentViewController(alert, animated: true, completion: nil)
} else {
println("\(error)")
}

Swift: Insert Alert Box with Text Input (and Store Text Input )

In one of my viewController, I want to make an alert box appear that prompts the user to type this information.Then, I want the user to store this input using NSUserDefaults. How can I achieve this?
Thank you in advance!
Check this out:
let alertController = UIAlertController(title: "Email?", message: "Please input your email:", preferredStyle: .alert)
let confirmAction = UIAlertAction(title: "Confirm", style: .default) { (_) in
guard let textFields = alertController.textFields,
textFields.count > 0 else {
// Could not find textfield
return
}
let field = textFields[0]
// store your data
UserDefaults.standard.set(field.text, forKey: "userEmail")
UserDefaults.standard.synchronize()
}
let cancelAction = UIAlertAction(title: "Cancel", style: .cancel) { (_) in }
alertController.addTextField { (textField) in
textField.placeholder = "Email"
}
alertController.addAction(confirmAction)
alertController.addAction(cancelAction)
self.present(alertController, animated: true, completion: nil)
SWIFT 3
func presentAlert() {
let alertController = UIAlertController(title: "Email?", message: "Please input your email:", preferredStyle: .alert)
let confirmAction = UIAlertAction(title: "Confirm", style: .default) { (_) in
if let emailTextField = alertController.textFields?[0] {
// do your stuff with emailTextField
}
}
let cancelAction = UIAlertAction(title: "Cancel", style: .cancel) { (_) in }
alertController.addTextField { (textField) in
textField.placeholder = "Email"
}
alertController.addAction(confirmAction)
alertController.addAction(cancelAction)
present(alertController, animated: true, completion: nil)
}
In swift 3
let alertController = UIAlertController(title: "SecureStyle", message: "SecureStyle AlertView.", preferredStyle: UIAlertControllerStyle.Alert)
alertController.addTextFieldWithConfigurationHandler { (textField : UITextField) -> Void in
textField.secureTextEntry = true
textField.placeholder = "Password"
}
let cancelAction = UIAlertAction(title: "Cancel", style: UIAlertActionStyle.Cancel) { (result : UIAlertAction) -> Void in
print("Cancel")
}
let okAction = UIAlertAction(title: "OK", style: UIAlertActionStyle.Default) { (result : UIAlertAction) -> Void in
print(alertController.textFields?.first?.text)
}
alertController.addAction(cancelAction)
alertController.addAction(okAction)
self.presentViewController(alertController, animated: true, completion: nil)