Unknown Selector Issue - ios

I am attempting to create a sign up page where people put such info as their email and desired password, after this is completed I want to switch to the next view controller however, I am getting the following error
[StudyBuddy.SignUpViewController signUpButton:]: unrecognized selector sent to instance 0x7fac5e061b80'
SignUpViewController
import UIKit
import Firebase
import CoreData
import CoreLocation
class SignUpViewController: UIViewController {
#IBOutlet weak var emailField: UITextField!
#IBOutlet weak var passwordField: UITextField!
#IBOutlet weak var confirmPasswordField: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
if let user = FIRAuth.auth()?.currentUser {
}
// Do any additional setup after loading the view.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
#IBAction func signupButton(sender: AnyObject)
{
if self.emailField.text == "" || self.passwordField.text == "" {
let alertController = UIAlertController(title: "Oops!", message: "Please enter a valid username and password", preferredStyle: .Alert)
let defaultAction = UIAlertAction(title: "Ok", style: .Cancel, handler: nil)
self.presentViewController(alertController, animated: true, completion: nil)
}
else if self.confirmPasswordField.text != self.passwordField.text {
let passwordAlert = UIAlertController(title: "Oops!", message: "Passwords do not match", preferredStyle: .Alert)
let passwordAction = UIAlertAction(title: "Ok", style: .Cancel, handler: nil)
self.presentViewController(passwordAlert, animated: true, completion: nil)
}
else {
FIRAuth.auth()?.createUserWithEmail(emailField.text!, password: passwordField.text!, completion: {(user, error) in
if error == nil {
self.performSegueWithIdentifier("goToSignUp", sender: sender)
}
else {
let createAlert = UIAlertController(title: "There was a problem", message: "There was a problem creating your account, please check the information you provided and try again", preferredStyle: .Alert)
let createAction = UIAlertAction(title: "Ok", style: .Cancel, handler: nil)
self.presentViewController(createAlert, animated: true, completion: nil)
}
})
}
}

You need to change your signupButton method declaration with signUpButton because inside your declaration u is lower latter for up you need to change it with Up because method name and property name are case sensitive or else change to selector with this signupButton one where you are adding target for button.

Related

'hostViewController must not be nil on STPPaymentContext when calling pushPaymentOptionsViewController on it

This is my SignUpViewController:-
import UIKit
import Firebase
import Stripe
class SignUpViewController: UIViewController {
var paymentContext = STPPaymentContext()
#IBOutlet weak var email: UITextField!
#IBOutlet weak var password: UITextField!
#IBOutlet weak var passwordConfirm: UITextField!
#IBAction func signUpAction(_ sender: Any) {
if password.text != passwordConfirm.text {let alertController = UIAlertController(title: "Password Incorrect", message: "Please re-type password", preferredStyle: .alert)
let defaultAction = UIAlertAction(title: "OK", style: .cancel, handler: nil)
alertController.addAction(defaultAction)
self.present(alertController, animated: true, completion: nil)
}else{
Auth.auth().createUser(withEmail: email.text!, password: password.text!){ (user, error) in if error == nil {
self.performSegue(withIdentifier: "signupToHome", sender: self)
}
else{
let alertController = UIAlertController(title: "Error", message: error?.localizedDescription, preferredStyle: .alert)
let defaultAction = UIAlertAction(title: "OK", style: .cancel, handler: nil)
alertController.addAction(defaultAction)
self.present(alertController, animated: true, completion: nil)
}
}
}
}
#IBAction func userIconClicked(_ sender: Any){
let userSheet = UIAlertController(title: nil, message: nil, preferredStyle: .actionSheet)
let logout = UIAlertAction(title: "Logout", style: .default) { (action) in
// Logout
do {
try Auth.auth().signOut()
} catch {
debugPrint(error.localizedDescription)
}
}
let manageCards = UIAlertAction(title: "Manage Credit Cards", style: .default) { (action) in
// Display Stripe Widget
self.paymentContext.pushPaymentOptionsViewController()
}
let manageBanks = UIAlertAction(title: "Manage Bank Accounts", style: .default) { (action) in
// Manage Bank Accounts
}
let close = UIAlertAction(title: "Close", style: .cancel)
userSheet.addAction(manageCards)
userSheet.addAction(manageBanks)
userSheet.addAction(logout)
userSheet.addAction(close)
present(userSheet, animated: true)
}
func setupStripe() {
Wallet.instance.customerContext = STPCustomerContext(keyProvider: StripeApiClient())
let config = STPPaymentConfiguration.shared
paymentContext = STPPaymentContext(customerContext: Wallet.instance.customerContext,
configuration: config(),
theme: .default())
paymentContext.hostViewController = self
}
}
This what it is in the simulator:--
The "Button" on the right-hand-upper-corner is linked to "#IBAction func userIconClicked(_ sender: Any)". When I click on this button, the following shows up on the simulator: -
Now, when I click on "Manage Credit Cards", it shows error in the AppDelegate - - Thread 1: "hostViewController must not be nil on STPPaymentContext when calling pushPaymentOptionsViewController on it. Next time, set the hostViewController property first! as shown in the screenshot below:-
What am I doing wrong ?
Edit 1: after humnoz comment, I called "setupStripe()", still same error -
import UIKit
import Firebase
import Stripe
class SignUpViewController: UIViewController {
var paymentContext = STPPaymentContext()
#IBOutlet weak var email: UITextField!
#IBOutlet weak var password: UITextField!
#IBOutlet weak var passwordConfirm: UITextField!
#IBAction func signUpAction(_ sender: Any) {
if password.text != passwordConfirm.text {let alertController = UIAlertController(title: "Password Incorrect", message: "Please re-type password", preferredStyle: .alert)
let defaultAction = UIAlertAction(title: "OK", style: .cancel, handler: nil)
alertController.addAction(defaultAction)
self.present(alertController, animated: true, completion: nil)
}else{
Auth.auth().createUser(withEmail: email.text!, password: password.text!){ (user, error) in if error == nil {
self.performSegue(withIdentifier: "signupToHome", sender: self)
}
else{
let alertController = UIAlertController(title: "Error", message: error?.localizedDescription, preferredStyle: .alert)
let defaultAction = UIAlertAction(title: "OK", style: .cancel, handler: nil)
alertController.addAction(defaultAction)
self.present(alertController, animated: true, completion: nil)
}
}
}
}
func viewDidLoad(_ animated: Bool) {
Auth.auth().addStateDidChangeListener { (auth, user) in
// If there is a user logged in, stay here on homeVC
// else, take them to the login page.
if user == nil {
let loginVC = LoginRegisterVC()
loginVC.modalPresentationStyle = .fullScreen
self.present(loginVC, animated: true)
} else {
UserManager.instance.getCurrentUser {
self.setupStripe()
}
}
}
}
#IBAction func userIconClicked(_ sender: Any){
let userSheet = UIAlertController(title: nil, message: nil, preferredStyle: .actionSheet)
let logout = UIAlertAction(title: "Logout", style: .default) { (action) in
// Logout
do {
try Auth.auth().signOut()
} catch {
debugPrint(error.localizedDescription)
}
}
let manageCards = UIAlertAction(title: "Manage Credit Cards", style: .default) { (action) in
// Display Stripe Widget
self.paymentContext.pushPaymentOptionsViewController()
}
let manageBanks = UIAlertAction(title: "Manage Bank Accounts", style: .default) { (action) in
// Manage Bank Accounts
}
let close = UIAlertAction(title: "Close", style: .cancel)
userSheet.addAction(manageCards)
userSheet.addAction(manageBanks)
userSheet.addAction(logout)
userSheet.addAction(close)
present(userSheet, animated: true)
}
func setupStripe() {
Wallet.instance.customerContext = STPCustomerContext(keyProvider: StripeApiClient())
let config = STPPaymentConfiguration.shared
paymentContext = STPPaymentContext(customerContext: Wallet.instance.customerContext,
configuration: config(),
theme: .default())
paymentContext.hostViewController = self
}
}
This post helped me im my case - https://it.wenda123.org/question/stack/40711655/stripe-checkout-ios-integration-with-storyboards-in-swift-3

Why won't my swift code process firebase email authentication?

I have view controllers connected with my code that include a login page and a sign up page, I've started a firebase project and connected with my Xcode and it successfully connected to the firebase servers. The code below is for my sign up view controller and is supposed to act like a sign up form with email, password and password confirmation but for some reason the build succeeds with no errors but the user that registered does not show up on my firebase user database and the textfields don't act like the way they should (i.e throw an error if password and password confirmation don't match) .
import UIKit
import FirebaseAuth
import FirebaseCore
import Firebase
class SignUpViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
#IBAction func SignUpToHome(sender: AnyObject) {
performSegue(withIdentifier: "SignUpToHome", sender: sender)
}
#IBOutlet weak var email: UITextField!
#IBOutlet weak var password: UITextField!
#IBOutlet weak var passwordConfirm: UITextField!
#IBAction func signUpAction(_ sender: Any) {
//func signUpAction(_ sender: Any) {
//}
if password.text != passwordConfirm.text {
let alertController = UIAlertController(title: "Password Incorrect", message: "Please re-type password", preferredStyle: .alert)
let defaultAction = UIAlertAction(title: "OK", style: .cancel, handler: nil)
alertController.addAction(defaultAction)
self.present(alertController, animated: true, completion: nil)
}
else{
Auth.auth().createUser(withEmail: email.text!, password: password.text!){ (user, error) in
if error == nil {
self.performSegue(withIdentifier: "SignUpToHome", sender: self)
}
else{
let alertController = UIAlertController(title: "Error", message: error?.localizedDescription, preferredStyle: .alert)
let defaultAction = UIAlertAction(title: "OK", style: .cancel, handler: nil)
alertController.addAction(defaultAction)
self.present(alertController, animated: true, completion: nil)
}
}
}
}
}

Validating user swift 4, login page

I need to my button proceedToAppFeatures to run the code and display an alert of success or failure login(just a single user, for now)
I have tried self.showAlert and self.present
Code:
import UIKit
import FLAnimatedImage
import ImageIO
import UserNotifications//import this to use user notifications
import Firebase
class ViewController: UIViewController, UITextFieldDelegate{
#IBOutlet weak var background: FLAnimatedImageView!
#IBOutlet weak var emailTextField: UITextField!
#IBOutlet weak var passwordTextField: UITextField!
#IBOutlet weak var lblValidationTxt: UILabel!
#IBOutlet weak var lblValidationMessage: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
background.loadGif(name: "neon lights")
passwordTextField.isSecureTextEntry = true// the password will no be visible
emailTextField.delegate = self
passwordTextField.delegate = self
let Tap:UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(DismissKeyboard))//here i have used the function to didmiss the keyboard
view.addGestureRecognizer(Tap)
let center = UNUserNotificationCenter.current()
//this initialize the permission that will appear on user screen
center.requestAuthorization(options: [.alert, .sound, .badge]) {
(granted,error) in
//enable or disable features based on the authorization
}
// emailTextField.text = ""
// passwordTextField.text = ""//on view did load to store text field property
}
#objc func DismissKeyboard() {//function that implements the type outside the screen
view.endEditing(true)
}
#IBAction func sigInButton(_ sender: UIButton) {
performSegue(withIdentifier: "userData", sender: true)
}
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
if textField == emailTextField {
textField.resignFirstResponder()
}else{
passwordTextField.becomeFirstResponder()//just to decide which of the buttons will respond after a tab is pressed
passwordTextField.resignFirstResponder()
}
return true
}
func showAlert(title: String, message: String){
let alert = UIAlertController(title: title, message: message, preferredStyle: UIAlertController.Style.alert)
alert.addAction(UIAlertAction(title: "OK", style: UIAlertAction.Style.default, handler: {(action) in alert.dismiss(animated: true, completion: nil)}))
self.present(alert, animated: true, completion: nil)
}
func isValidEmail(testStr:String) -> Bool {
let emailRegEx = "[A-Z0-9a-z._%+-]+#[A-Za-z0-9.-]+\\.[A-Za-z]{2,64}"
let emailTest = NSPredicate(format:"SELF MATCHES %#", emailRegEx)
return emailTest.evaluate(with: testStr)
}
#IBAction func proceedToAppFeatures(_ sender: Any) {
let email = isValidEmail(testStr: emailTextField.text!)
if email == false{
self.showAlert(title: "ERROR", message: "This is not a valid e-mail. Please try again")
emailTextField.text = ""
}else{
self.showAlert(title: "Success", message: "Yousucessifully loged in")
//performSegue(withIdentifier: "formPage", sender: true)
}
let myEmail: String!
myEmail = emailTextField.text!
let myPassword: String!
myPassword = passwordTextField.text!
if(myEmail == "leandroaaramos" && myPassword == "123456"){
//performSegue(withIdentifier: "homeSegue", sender: nil)
//code for the truth
print("You are success")
}else if (myEmail.isEmpty && myPassword.isEmpty){
//here i put the alert message
let alert = UIAlertController(title: "Wow", message: "Hold on fill the gaps", preferredStyle: UIAlertController.Style.alert)
//add an action to the alert
alert.addAction(UIAlertAction(title: "Close", style: UIAlertAction.Style.default, handler: nil))
self.present(alert, animated: true, completion: nil)
self.showAlert(title: "Empty field", message: "Try again")
}else {
//here i put the alert message
let alert = UIAlertController(title: "Ops", message: "Your account is not here", preferredStyle: UIAlertController.Style.alert)
//add an action to the alert
alert.addAction(UIAlertAction(title: "Try Again", style: UIAlertAction.Style.default, handler: nil))
self.present(alert, animated: true, completion: nil)
self.showAlert(title: "Sorry", message: "Can't find you.")
}
}
}
I want go to my loginViewController, if the user leandroaaramos and password 123456 was typed.

Error - The email address is badly formed. Xcode Swift Firebase

I'm building a UI interface with authentication as an iOS app. I've come a long way from where I was originally but I cannot get past this error. I don't understand what I'm compiling incorrectly. Please take a look and help me out if you can.
import UIKit
import Firebase
class SignUpViewController: UIViewController, UITextFieldDelegate
{
#IBOutlet weak var email: UITextField!
#IBOutlet weak var password: UITextField!
#IBOutlet weak var passwordConfirm: UITextField!
#IBOutlet weak var signUpButton: UIButton!
override func viewDidLoad() {
super.viewDidLoad()
email.delegate = self
password.delegate = self
passwordConfirm.delegate = self
}
#IBAction func signUpAction(_ sender: UIButton) {
if password.text !=
passwordConfirm.text{
let alertController = UIAlertController(title: "Password Incorrect", message: "Please re-type password", preferredStyle: .alert)
let defaultAction = UIAlertAction(title: "OK", style: .cancel, handler: nil)
alertController.addAction(defaultAction)
self.present(alertController, animated: true, completion: nil)
}
else{
Auth.auth().createUser(withEmail: email.text!, password: password.text!){ (user, error) in
if error == nil {
self.performSegue(withIdentifier: "signupToHome", sender: self)
}
else{
let alertController = UIAlertController(title: "Error", message: error?.localizedDescription, preferredStyle: .alert)
let defaultAction = UIAlertAction(title: "OK", style: .cancel, handler: nil)
alertController.addAction(defaultAction)
self.present(alertController, animated: true, completion: nil)
}
}
}
}
func textFieldShouldReturn(_ textField: UITextField) -> Bool { //delegate method
textField.resignFirstResponder()
return true
}
}
The goal is to then accept the user input from email.text and password.text and segue into the home screen after creating a new user in my Firebase console.
Firebase has its own email validation which is like this 'abc#somedomain.com' so you need to pass a email address like this other wise it will keep throwing this error.

Attempt to present UIAlertController whose view is not in the window hierarchy - Swift Error

I'm trying to create an alert so when a user signs up and then wants to log back in they can be warned if the password is wrong because at the moment it just performs the segue and the attempt to alert fails. I'm using Firebase so the password that is entered into firebase on sign up should be the one users log in with.
import Foundation
import UIKit
import Firebase
class SignInViewController: UIViewController, UITextFieldDelegate {
#IBOutlet weak var EmailAddressTextField: UITextField!
#IBOutlet weak var PasswordTextField: UITextField!
// Do any additional setup after loading the view.
override func viewDidLoad() {
super.viewDidLoad()
EmailAddressTextField.delegate = self
PasswordTextField.delegate = self
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
}
#IBAction func LogInButton(_ sender: Any) {
if (EmailAddressTextField.text != "" && PasswordTextField.text != ""){
Auth.auth().signIn(withEmail: EmailAddressTextField.text!, password: PasswordTextField.text!) { user, error in
if error == nil {
self.performSegue(withIdentifier: "LogInSegue", sender: nil)
} else {
let alert = UIAlertController(title: "Error", message: "Enter a correct email and password", preferredStyle: .alert)
let action = UIAlertAction(title: "OK", style: .default, handler: nil)
alert.addAction(action)
self.present(alert, animated: true, completion: nil)
}
}
}
}
}
Just want to check something with you, as your description above is a little unclear. When you enter the wrong email/password combination, does your app still perform the "LogInSegue" (as well as failing to show the alert)?
If so, it sounds like you might have connected your segue to the UIButton instead of the UIViewController.
To check this, click on your segue in the Storyboard and see if the UIButton is highlighted.
If it is, then delete this segue and follow the instruction below to connect your new segue from the view controller:
You are presenting UIAlertController on SignInViewController so at that time SignInViewController is not in navigation stack.
So when you are presenting UIAlertController at that time check the self.navigationController?.viewControllers and verify that it is in stack or not.
for eg.
//check before self.presnt...
print(self.navigationController?.viewControllers)
So you need to make sure that when you are presenting a view controller over another view controller that must be in navigation stack otherwise you will get this message in log.
Attempt to present UIAlertController whose view is not in the window hierarchy
Use the following function to show alert over root view controller
func showAlert(title: String, msg: String, actions:[UIAlertAction]?) {
var actions = actions
let alertVC = UIAlertController(title: title, message: msg, preferredStyle: .alert)
if actions == nil {
actions = [UIAlertAction(title: "OK", style: .default, handler: nil)]
}
for action in actions! {
alertVC.addAction(action)
}
if let rootVC = UIApplication.shared.delegate?.window??.rootViewController {
rootVC.present(alertVC, animated: true, completion: nil)
} else {
print("Root view controller is not set.")
}
}
Usage
self.showAlert(title: "My App", msg: "your message", actions: nil)
You need to use this with your code like this...
class SignInViewController: UIViewController, UITextFieldDelegate {
#IBOutlet weak var EmailAddressTextField: UITextField!
#IBOutlet weak var PasswordTextField: UITextField!
// Do any additional setup after loading the view.
override func viewDidLoad() {
super.viewDidLoad()
EmailAddressTextField.delegate = self
PasswordTextField.delegate = self
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
}
#IBAction func LogInButton(_ sender: Any) {
if (EmailAddressTextField.text != "" && PasswordTextField.text != ""){
Auth.auth().signIn(withEmail: EmailAddressTextField.text!, password: PasswordTextField.text!) { user, error in
if error == nil {
self.performSegue(withIdentifier: "LogInSegue", sender: nil)
} else {
// let alert = UIAlertController(title: "Error", message: "Enter a correct email and password", preferredStyle: .alert)
// let action = UIAlertAction(title: "OK", style: .default, handler: nil)
// alert.addAction(action)
// self.present(alert, animated: true, completion: nil)
//use like this...
self.showAlert(title: "Error", msg: "Enter a correct email and password", actions: nil)
}
}
}
}
//###############################################
func showAlert(title: String, msg: String, actions:[UIAlertAction]?) {
var actions = actions
let alertVC = UIAlertController(title: title, message: msg, preferredStyle: .alert)
if actions == nil {
actions = [UIAlertAction(title: "OK", style: .default, handler: nil)]
}
for action in actions! {
alertVC.addAction(action)
}
if let rootVC = UIApplication.shared.delegate?.window??.rootViewController {
rootVC.present(alertVC, animated: true, completion: nil)
} else {
print("Root view controller is not set.")
}
}
//###############################################
}
Present it in main activity :
dispatch_async(dispatch_get_main_queue(), ^{
let alert = UIAlertController(title: "Error", message: "Enter a correct email and password", preferredStyle: .alert)
let action = UIAlertAction(title: "OK", style: .default, handler: nil)
alert.addAction(action)
self.present(alert, animated: true, completion: nil)
});

Resources