codes are not dismissing keyboard - ios

I added resignFirstResponder and touchesBegan to my code but the keyboard is not dismissing. I already checked the delegate and assigned delegate to textfields but still no sign of dismissing.
Please help spot the error? (i'm guessing it might be about multiple textFields?)
import UIKit
import Parse
class SignUpViewController: UIViewController, UITextFieldDelegate {
#IBAction func BackToFirstPageButton(sender: AnyObject) {
performSegueWithIdentifier("BackToLogInPage", sender: self)
}
func SignOutForEmailVerification() {
PFUser.logOut()
performSegueWithIdentifier("BackToLogInPage", sender: self)
}
#IBOutlet var UsernameTextField: UITextField!
#IBOutlet var PasswordTextField: UITextField!
#IBOutlet var EmailTextField: UITextField!
var ActivityIndicator:UIActivityIndicatorView = UIActivityIndicatorView()
#IBAction func SignUpButton(sender: AnyObject) {
if UsernameTextField.text == "" || PasswordTextField.text == "" || EmailTextField.text == "" {
let SignUpAlert = UIAlertController (title: "Error in form", message: "Please fill in the blanks", preferredStyle: UIAlertControllerStyle.Alert)
SignUpAlert.addAction((UIAlertAction(title: "Dismiss", style: .Default, handler: { (action) -> Void in
})))
self.presentViewController(SignUpAlert, animated: true, completion: nil)
} else {
ActivityIndicator = UIActivityIndicatorView(frame: CGRectMake(0, 0, 50, 50))
ActivityIndicator.center = self.view.center
ActivityIndicator.hidesWhenStopped = true
ActivityIndicator.activityIndicatorViewStyle = UIActivityIndicatorViewStyle.Gray
view.addSubview(ActivityIndicator)
ActivityIndicator.startAnimating()
UIApplication.sharedApplication().beginIgnoringInteractionEvents()
var user = PFUser()
user.username = UsernameTextField.text
user.password = PasswordTextField.text
user.email = EmailTextField.text
user.signUpInBackgroundWithBlock({ (success, error) -> Void in
self.ActivityIndicator.stopAnimating()
UIApplication.sharedApplication().endIgnoringInteractionEvents()
if error == nil {
let EmailVerificationAlert = UIAlertController(title: "Email Verification", message: "Please click the link in an email we have just sent you", preferredStyle: UIAlertControllerStyle.Alert)
EmailVerificationAlert.addAction(UIAlertAction(title: "Okay", style: .Default, handler: { EmailVerificationAlert in self.SignOutForEmailVerification()})
)
self.presentViewController(EmailVerificationAlert, animated: true, completion: nil)
}
})
}
func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
self.UsernameTextField.delegate = self
self.PasswordTextField.delegate = self
self.EmailTextField.delegate = self
}
func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
self.view.endEditing(true)
}
func textFieldShouldReturn(textField: UITextField!) -> Bool {
textField.resignFirstResponder()
return true
}
}
}

When user taps "Return" key on the keyboard
As you already added the UITextFieldDelegate, try modifying this code :
func textFieldShouldReturn(textField: UITextField!) -> Bool {
self.UsernameTextField.resignFirstResponder();
self.EmailTextField.resignFirstResponder();
self.PasswordTextField.resignFirstResponder();
return true;
}
This will work when the user taps the button return on the keyboard.
Your mistake is as you said handling multiple text fields. Also you have to say which one you want to resignFirstResponder().
When user taps screen
User could also dismiss the keyboard by tapping anywhere on the screen.
Here we have a UITapGestureRecognizer that detects the tap to dismiss keyboard. It is an alternative solution.
override func viewDidLoad() {
super.viewDidLoad()
let tap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: "dismissKeyboard")
view.addGestureRecognizer(tap)
}
func dismissKeyboard() {
view.endEditing(true)
}

import UIKit
import Parse
class SignUpViewController: UIViewController, UITextFieldDelegate {
#IBOutlet var UsernameTextField: UITextField!
#IBOutlet var PasswordTextField: UITextField!
#IBOutlet var EmailTextField: UITextField!
var ActivityIndicator:UIActivityIndicatorView = UIActivityIndicatorView()
var currentTextField : UITextField?
func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
self.UsernameTextField.delegate = self
self.PasswordTextField.delegate = self
self.EmailTextField.delegate = self
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
#IBAction func BackToFirstPageButton(sender: AnyObject) {
performSegueWithIdentifier("BackToLogInPage", sender: self)
}
func SignOutForEmailVerification() {
PFUser.logOut()
performSegueWithIdentifier("BackToLogInPage", sender: self)
}
#IBAction func SignUpButton(sender: AnyObject) {
if UsernameTextField.text == "" || PasswordTextField.text == "" || EmailTextField.text == "" {
let SignUpAlert = UIAlertController (title: "Error in form", message: "Please fill in the blanks", preferredStyle: UIAlertControllerStyle.Alert)
SignUpAlert.addAction((UIAlertAction(title: "Dismiss", style: .Default, handler: { (action) -> Void in
})))
self.presentViewController(SignUpAlert, animated: true, completion: nil)
} else {
currentTextField?.resignFirstResponder();
ActivityIndicator = UIActivityIndicatorView(frame: CGRectMake(0, 0, 50, 50))
ActivityIndicator.center = self.view.center
ActivityIndicator.hidesWhenStopped = true
ActivityIndicator.activityIndicatorViewStyle = UIActivityIndicatorViewStyle.Gray
view.addSubview(ActivityIndicator)
ActivityIndicator.startAnimating()
UIApplication.sharedApplication().beginIgnoringInteractionEvents()
var user = PFUser()
user.username = UsernameTextField.text
user.password = PasswordTextField.text
user.email = EmailTextField.text
user.signUpInBackgroundWithBlock({ (success, error) -> Void in
self.ActivityIndicator.stopAnimating()
UIApplication.sharedApplication().endIgnoringInteractionEvents()
if error == nil {
let EmailVerificationAlert = UIAlertController(title: "Email Verification", message: "Please click the link in an email we have just sent you", preferredStyle: UIAlertControllerStyle.Alert)
EmailVerificationAlert.addAction(UIAlertAction(title: "Okay", style: .Default, handler: { EmailVerificationAlert in self.SignOutForEmailVerification()})
)
self.presentViewController(EmailVerificationAlert, animated: true, completion: nil)
}
})
}
func textFieldDidBeginEditing(textField: UITextField!){
currentTextField = textField;
}
func textFieldShouldReturn(textField: UITextField!) -> Bool {
currentTextField.resignFirstResponder()
return true
}
}
}

Related

Use of local variable 'displayAlert' before its declaration

I have called function and It isn't still displaying the code right I have been receiving and error Use of local variable 'displayAlert' before its declaration
import UIKit
import FirebaseAuth
class ViewController: UIViewController {
// I have also switched the #IBAction to function so the code actually run better any suggestions to fix this
#IBOutlet weak var dropoffLabel: UILabel!
#IBOutlet weak var pickupLabel: UILabel!
#IBOutlet weak var passwordTextField: UITextField!
#IBOutlet weak var emailTextfield: UITextField!
#IBOutlet weak var pickupDropoffSwitch: UISwitch!
#IBOutlet weak var buttomButton: UIButton!
#IBOutlet weak var topButton: UIButton!
var signUpMode = true
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
#IBAction func topTapped(_ sender: Any) {
if emailTextfield.text == "" || passwordTextField.text == "" {
code error coming from here ----> displayAlert(title: "Missing Information", message: "You must provide both a email and password")
} else {
if let email = emailTextfield.text {
if let password = passwordTextField.text {
if signUpMode {
// SIGN UP
Auth.auth().createUser(withEmail: email, password: password, completion: { (user, error) in
if error != nil {
displayAlert(title: "Error", message: error!.localizedDescription)
} else {
print("Sign Up Success")
}
})
} else {
// LOG IN
Auth.auth().signIn(withEmail: email, password: password, completion: { (user, error) in
if error != nil {
displayAlert(title: "Error", message: error!.localizedDescription)
} else {
print("Sign Up Success")
}
})
}
}
}
}
------> I believe if I am doing it correctly called the function here func displayAlert(title:String, message:String) {
let alertController = UIAlertController(title: title, message: message, preferredStyle: .alert)
alertController.addAction(UIAlertAction(title: "OK", style: .default, handler: nil))
self.present(alertController, animated: true, completion: nil)
}
//there is no other issue besides the code not understanding the the function I have labeled
//Also I have ran the code before and the alert would pop up but now it is not allowing the
func buttomTapped(_ sender: Any) {
if signUpMode {
topButton.setTitle("Log In", for: .normal)
buttomButton.setTitle("Switch to Sign Up", for: .normal)
pickupLabel.isHidden = true
dropoffLabel.isHidden = true
pickupDropoffSwitch.isHidden = true
signUpMode = false
} else {
topButton.setTitle("Sign Up", for: .normal)
buttomButton.setTitle("Switch to Log In", for: .normal)
pickupLabel.isHidden = false
dropoffLabel.isHidden = false
pickupDropoffSwitch.isHidden = false
signUpMode = true
}
}
}
}
You have incorrectly defined your displayAlert func inside the topTapped func.
You need to move it outside so it is on the class level
#IBAction func topTapped(_ sender: Any) {
//code here
}
func displayAlert(title:String, message:String) {
//
}

Use of unresolved identifier 'displayAlertMessage'

I tried the moved the code around a bit. The errors I got was use of local variable displayMessage before its declaration. So I moved the func displayAlertMessage above the display an alert Message comment, the new error is use of unresolved identifier 'displayAlertMessage'
//
// RegisterPageViewController.swift
// UserLoginandRegistration
//
// Created by Iyah Chulo on 17/11/2017.
// Copyright © 2017 Iyah Chulo. All rights reserved.
//
import UIKit
class RegisterPageViewController: UIViewController {
#IBOutlet weak var userEmailTextField: UITextField!
#IBOutlet weak var userPasswordTextField: UITextField!
#IBOutlet weak var ReenterPasswordTextField: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
#IBAction func RegisterButtonTapped(_ sender: Any) {
let userEmail = userEmailTextField.text;
let userPassword = userPasswordTextField.text;
let userReenterPassword = ReenterPasswordTextField.text;
// Check for empty fields
if((userEmail?.isEmpty)! || (userPassword?.isEmpty)! ||
(userReenterPassword?.isEmpty)!)
{
func displayAlertMessage(userMessage: String) { let myAlert = UIAlertController(title:"Alert", message: userMessage, preferredStyle:
UIAlertControllerStyle.alert);
let okAction = UIAlertAction(title:"Ok", style:
UIAlertActionStyle.default, handler:nil)
myAlert.addAction(okAction);
self.present(myAlert, animated: true,
completion:nil)
}
//Display alert message
displayAlertMessage(userMessage: "All fields are required")
return;
}
//Check if passwords match
if(userPassword != userReenterPassword)
{
// Display an alert message
displayAlertMessage(userMessage: "Passwords do not match")
return;
}
// Store data
UserDefaults.standard.set(userEmail, forKey:"userEmail")
UserDefaults.standard.set(userEmail, forKey:"userPassword")
UserDefaults.standard.synchronize()
// Display alert message with confirmation
var myAlert = UIAlertController(title:"Alert", message: "Registration is successful.Thank you!", preferredStyle:
UIAlertControllerStyle.alert);
let okAction = UIAlertAction(title:"Ok", style:
UIAlertActionStyle.default) { action in
self.dismiss(animated: true, completion:nil)
}
}
}
Your displayAlertMessage function definition needs to be outside of the other functions in your class.
Also, please note that Swift does not require semicolons!
Try this:
import UIKit
class RegisterPageViewController: UIViewController {
#IBOutlet weak var userEmailTextField: UITextField!
#IBOutlet weak var userPasswordTextField: UITextField!
#IBOutlet weak var ReenterPasswordTextField: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
#IBAction func RegisterButtonTapped(_ sender: Any) {
let userEmail = userEmailTextField.text
let userPassword = userPasswordTextField.text
let userReenterPassword = ReenterPasswordTextField.text
// Check for empty fields
if((userEmail?.isEmpty)! || (userPassword?.isEmpty)! ||
(userReenterPassword?.isEmpty)!)
{
//Display alert message
displayAlertMessage(userMessage: "All fields are required")
return;
}
//Check if passwords match
if(userPassword != userReenterPassword)
{
// Display an alert message
displayAlertMessage(userMessage: "Passwords do not match")
return;
}
// Store data
UserDefaults.standard.set(userEmail, forKey:"userEmail")
UserDefaults.standard.set(userEmail, forKey:"userPassword")
UserDefaults.standard.synchronize()
// Display an alert message
displayAlertMessage(userMessage: "Registration is successful.Thank you!")
}
func displayAlertMessage(userMessage: String) {
let myAlert = UIAlertController(title:"Alert", message: userMessage, preferredStyle: UIAlertControllerStyle.alert)
let okAction = UIAlertAction(title:"Ok", style: UIAlertActionStyle.default) {
action in
self.dismiss(animated: true, completion:nil)
}
myAlert.addAction(okAction);
self.present(myAlert, animated: true, completion: nil)
}
}

Segue to next view controller after successful sign up

I've searched and searched and searched but not found a solution. The sign up works but will not segue to the next view controller. Heres my code:
import UIKit
import Parse
import Bolts
class SignUpVC: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate {
#IBOutlet weak var profilePictureIV: UIImageView!
#IBOutlet weak var firstNameTF: UITextField!
#IBOutlet weak var lastNameTF: UITextField!
#IBOutlet weak var newUsernameTF: UITextField!
#IBOutlet weak var newPasswordTF: UITextField!
#IBOutlet weak var emailTF: UITextField!
#IBOutlet weak var phoneNumberTF: UITextField!
#IBAction func setProfilePicture(sender: AnyObject) {
let myPickerController = UIImagePickerController()
myPickerController.delegate = self
myPickerController.sourceType = UIImagePickerControllerSourceType.PhotoLibrary
self.presentViewController(myPickerController, animated: true, completion: nil)
}
#IBAction func signUpButton(sender: AnyObject) {
let spinner: UIActivityIndicatorView = UIActivityIndicatorView(frame: CGRectMake(0, 0, 150, 150)) as UIActivityIndicatorView
if firstNameTF.text != "" && lastNameTF.text != "" && newUsernameTF.text != "" && newPasswordTF.text != "" && emailTF.text != "" && phoneNumberTF.text != "" {
let newUser = PFUser()
if let profilePictureImage = profilePictureIV?.image {
let profilePicture = UIImageJPEGRepresentation(profilePictureImage, 1)!
let profilePictureImageFile = PFFile(data: profilePicture)
newUser["profilePicture"] = profilePictureImageFile
}
newUser["firstName"] = firstNameTF.text
newUser["lastName"] = lastNameTF.text
newUser.username = newUsernameTF.text
newUser.password = newPasswordTF.text
newUser.email = emailTF.text
newUser["phoneNumber"] = phoneNumberTF.text
newUser.signUpInBackgroundWithBlock {
(succeeded: Bool, error: NSError?) -> Void in
if let error = error {
spinner.startAnimating()
self.alert("Opps", textMessage: (error.localizedDescription))
} else {
spinner.startAnimating()
self.alert("Congratualtion!", textMessage: "Success, your account has been created.")
self.performSegueWithIdentifier("showMessages", sender: self)
}
}
} else {
alert("Hmm...", textMessage: "If you want an account, please fill in the blanks.")
}
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
override func viewWillAppear(animated: Bool) {
self.navigationController!.navigationBar.hidden = false
}
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {
profilePictureIV.image = info[UIImagePickerControllerOriginalImage] as? UIImage
self.dismissViewControllerAnimated(true, completion: nil)
}
func alert(textTitle: String, textMessage: String) {
let alertController = UIAlertController(title: textTitle, message: textMessage, preferredStyle: .Alert)
alertController.addAction(UIAlertAction(title: "OK", style: .Default, handler: nil))
self.presentViewController(alertController, animated: true, completion: nil)
}
/*
// MARK: - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
// Get the new view controller using segue.destinationViewController.
// Pass the selected object to the new view controller.
}
*/
}
If i play the self.performSegueWithIdentifier("showMessages", sender: self) line anywhere else then when i run the program and press the signup button it will directly go to in the messages view controller without any signup required.
as Paulw11 said , Try to remove alert message :-
if let error = error {
spinner.startAnimating()
self.alert("Opps", textMessage: (error.localizedDescription))
} else {
spinner.startAnimating()
self.performSegueWithIdentifier("showMessages", sender: self)
}
or You can call the handler in your code.
if let error = error {
spinner.startAnimating()
self.alert("Opps", textMessage: (error.localizedDescription))
} else {
spinner.startAnimating()
var alert = UIAlertController(title: "Welcome", message: "Login", preferredStyle: UIAlertControllerStyle.Alert)
alert.addAction(UIAlertAction(title:"OK", style: .Default, handler: { action in self.performSegueWithIdentifier("showMessages", sender: self) }
}
You can not pass self as a argument in side the block.
so create a method to call a segue
#IBAction func signUpButton(sender: AnyObject) {
let spinner: UIActivityIndicatorView = UIActivityIndicatorView(frame: CGRectMake(0, 0, 150, 150)) as UIActivityIndicatorView
if firstNameTF.text != "" && lastNameTF.text != "" && newUsernameTF.text != "" && newPasswordTF.text != "" && emailTF.text != "" && phoneNumberTF.text != "" {
let newUser = PFUser()
if let profilePictureImage = profilePictureIV?.image {
let profilePicture = UIImageJPEGRepresentation(profilePictureImage, 1)!
let profilePictureImageFile = PFFile(data: profilePicture)
newUser["profilePicture"] = profilePictureImageFile
}
newUser["firstName"] = firstNameTF.text
newUser["lastName"] = lastNameTF.text
newUser.username = newUsernameTF.text
newUser.password = newPasswordTF.text
newUser.email = emailTF.text
newUser["phoneNumber"] = phoneNumberTF.text
newUser.signUpInBackgroundWithBlock {
(succeeded: Bool, error: NSError?) -> Void in
if let error = error {
spinner.startAnimating()
self.alert("Opps", textMessage: (error.localizedDescription))
} else {
spinner.startAnimating()
self.alert("Congratualtion!", textMessage: "Success, your account has been created.")
callSegue()
}
}
} else {
alert("Hmm...", textMessage: "If you want an account, please fill in the blanks.")
}
}
func callSegue()
{
self.performSegueWithIdentifier("showMessages", sender: self)
}

Don't understand why I am getting Parse 206 error (UserCannotBeAlteredWithoutSessionError)

PFCloud.callFunctionInBackground("updatePositions", withParameters: ["username" : username, "location" : locationTitle, "instructor" : instructorSwitch.on, "guard" : guardSwitch.on, "sup" : supSwitch.on]) {
(positions: AnyObject!, error: NSError!) -> Void in
if error == nil {
self.currentUser.setValue(self.instructorSwitch.on, forKey: (self.shortTitle + "Instructor"))
self.currentUser.setValue(self.guardSwitch.on, forKey: (self.shortTitle + "Guard"))
self.currentUser.setValue(self.supSwitch.on, forKey: (self.shortTitle + "Sup"))
self.currentUser.save(nil)
self.navigationController?.popToRootViewControllerAnimated(true)
}
else {
let errorAlert = UIAlertController (title: "Error", message: "There was an error while processing your request. This may be because the app could not connect to the internet. Please try again.", preferredStyle: UIAlertControllerStyle.Alert)
let actionCancel = UIAlertAction (title: "Dismiss", style: .Cancel, handler: nil)
errorAlert.addAction(actionCancel)
self.presentViewController(errorAlert, animated: true, completion: nil)
}
When I run the above code, my goal to is to update the Parse Objects in my Data Browser and at the same time edit the same objects in currentUser.
As it stands now, the Login VC is shown if no one is logged in (based on a boolean value in Core Data). When a user logs in they are taken to the apps Home Page (and the boolean value is changed). If they were to shutdown the app and restart, they would be taken to the Home Page rather than the Login VC. I have a "dummy" VC where the Core Data entity is updated (I know it sounds redundant but it's there for a purpose that is unrelated to my issue).
Login VC
#IBOutlet var fullName: UITextField!
#IBOutlet var password: UITextField!
#IBOutlet var helpButton: UIButton!
#IBOutlet var loginButton: UIButton!
var nameID: String!
var passwordStatus: Bool?
var username: String?
var userID: String?
var objectID: String!
var currentUser = PFUser.currentUser()
override func viewDidLoad() {
super.viewDidLoad()
navigationItem.hidesBackButton = true
checkInternetConnection()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func textFieldShouldReturn(textField: UITextField) -> Bool {
textField.resignFirstResponder()
return true
}
override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
self.view.endEditing(true)
}
#IBAction func LoginTapped(sender: AnyObject) {
if (fullName.text.isEmpty || password.text.isEmpty) {
let alert = UIAlertView()
alert.title = "Error"
alert.message = "You have not completed the required fields!"
alert.addButtonWithTitle("Dismiss")
alert.show()
}
else {
PFUser.logInWithUsernameInBackground(fullName.text, password: password.text) {
(user: PFUser!, error: NSError!) -> Void in
if (user != nil) {
self.currentUser = user
self.currentUser.save()
//println(self.currentUser.objectForKey("username") as String!)
if (self.currentUser.objectForKey("passwordChanged") as Bool! == nil || self.currentUser.objectForKey("passwordChanged") as Bool! == false) {
self.performSegueWithIdentifier("password", sender: self)
}
else {
self.performSegueWithIdentifier("skipPassword", sender: self)
}
}
else {
let errorAlert = UIAlertController (title: "Error", message: "Profile not found. Please check your username and password.", preferredStyle: UIAlertControllerStyle.Alert)
let actionCancel = UIAlertAction (title: "Dismiss", style: .Cancel, handler: nil)
errorAlert.addAction(actionCancel)
self.presentViewController(errorAlert, animated: true, completion: nil)
}
}
}
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject!) {
if (segue.identifier == "password") {
var svc = segue.destinationViewController as AccountSetup
svc.username = fullName.text
}
else if (segue.identifier == "skipPassword") {
var svc = segue.destinationViewController as Agree
svc.password = password.text
}
}
The only place that there is a chance for the user to log out is in the app's Settings controller:
Settings VC
var choice: String!
var currentEmail: String!
var sendEmail: String!
var email: UITextField!
var listArray = NSArray (objects: "-", "-", "-", "1", "2", "3", "4", "5", "6")
var currentUser = PFUser.currentUser()
var res : NSManagedObject!
var context : NSManagedObjectContext!
override func viewDidLoad() {
super.viewDidLoad()
currentEmail = currentUser.objectForKey("email") as String!
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
// MARK: - Table view data source
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
// #warning Potentially incomplete method implementation.
// Return the number of sections.
return 4
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// Return the number of rows in the section.
switch(section) {
case 0: return 2
case 1: return 9
case 2: return 4
case 3: return 1
default: fatalError("Unknown number of sections")
}
}
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
choice = listArray.objectAtIndex(indexPath.row) as NSString
if (indexPath.row >= 3) {
performSegueWithIdentifier("positions", sender: self)
}
else if (indexPath.row == 1) {
var passwordAlert = UIAlertController (title: "Change Password", message: "For security reasons, you can not change your password through the app. We will send an email to " + currentEmail + ". If you rather us send it to another email, enter it below. Otherwise, click 'Send'.", preferredStyle: UIAlertControllerStyle.Alert)
let standardCancel = UIAlertAction (title: "Dismiss", style: .Cancel, handler: nil)
let actionSubmit = UIAlertAction (title: "Send", style: .Default) { (action) in
if (self.email.text.isEmpty) {
self.sendEmail = self.currentEmail
}
else {
self.sendEmail = self.email.text
}
PFUser.requestPasswordResetForEmailInBackground(self.sendEmail){
(success: Bool!, error: NSError!) -> Void in
if (success == true) {
let emailAlert = UIAlertController (title: "Password", message: "An email containing information on how to change your password has been sent to " + self.sendEmail + ".", preferredStyle: UIAlertControllerStyle.Alert)
emailAlert.addAction(standardCancel)
self.presentViewController(emailAlert, animated: true, completion: nil)
}
else {
let errorAlert = UIAlertController (title: "Error", message: "There was an error while processing your request. This may be because the email is invalid or the app could not connect to the internet. Please try again.", preferredStyle: UIAlertControllerStyle.Alert)
errorAlert.addAction(standardCancel)
self.presentViewController(errorAlert, animated: true, completion: nil)
}
}
}
let actionCancel = UIAlertAction (title: "Cancel", style: .Cancel, handler: nil)
passwordAlert.addTextFieldWithConfigurationHandler {
(textField) in textField.placeholder = "Email"
self.email = textField
}
passwordAlert.addAction(actionSubmit)
passwordAlert.addAction(actionCancel)
self.presentViewController(passwordAlert, animated: true, completion: nil)
}
}
#IBAction func logoutPressed (sender: AnyObject) {
PFUser.logOut()
//code to change Core Data boolean value to start on Login is here but there is no need to put it (unrelated)
tabBarController?.tabBar.hidden = true
performSegueWithIdentifier("goToLogin", sender: self)
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject!) {
if (segue.identifier == "positions") {
var svc = segue.destinationViewController as UpdatePositions;
svc.locationTitle = choice
println (choice)
}
}
Like I've mentioned before, everything works up until the app is shutdown where the authentication is lost.
Ends up that there was an error in the 1.6.0 Parse SDK. Works fine with 1.6.5.

Core Data How would I use core data to keep the user logged in?

I am making this app called imagur and it has a signup and login and whenever I leave the app when the user already has an account I want it to automatically recognize the user how would I do that this is what I have so far :
import UIKit
class ViewController: UIViewController, UINavigationControllerDelegate, UIImagePickerControllerDelegate {
var signupActive = true
var activityIndicator: UIActivityIndicatorView = UIActivityIndicatorView()
func displayAlert(title:String, error:String) {
var alert = UIAlertController(title: title, message: error, 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)
}
#IBOutlet var username: UITextField!
#IBOutlet var password: UITextField!
#IBOutlet var alreadyRegistered: UILabel!
#IBOutlet var signUpButton: UIButton!
#IBOutlet var signUpLabel: UILabel!
#IBOutlet var signUpToggleButton: UIButton!
#IBAction func toggleSignUp(sender: AnyObject) {
if signupActive == true {
signupActive = false
signUpLabel.text = "Use the form below to log in"
signUpButton.setTitle("Log In", forState: UIControlState.Normal)
alreadyRegistered.text = "Not Registered?"
signUpToggleButton.setTitle("Sign Up", forState: UIControlState.Normal)
} else {
signupActive = true
signUpLabel.text = "Use the form below to sign up"
signUpButton.setTitle("Sign Up", forState: UIControlState.Normal)
alreadyRegistered.text = "Already Registered?"
signUpToggleButton.setTitle("Log In", forState: UIControlState.Normal)
}
}
#IBAction func signUp(sender: AnyObject) {
var error = ""
if username.text == "" || password.text == "" {
error = "Please enter a username and password"
}
if error != "" {
displayAlert("Error In Form", error: error)
} else {
activityIndicator = UIActivityIndicatorView(frame: CGRectMake(0, 0, 50, 50))
activityIndicator.center = self.view.center
activityIndicator.hidesWhenStopped = true
activityIndicator.activityIndicatorViewStyle = UIActivityIndicatorViewStyle.Gray
view.addSubview(activityIndicator)
activityIndicator.startAnimating()
UIApplication.sharedApplication().beginIgnoringInteractionEvents()
if signupActive == true {
var user = PFUser()
user.username = username.text
user.password = password.text
user.signUpInBackgroundWithBlock {
(succeeded: Bool!, signupError: NSError!) -> Void in
self.activityIndicator.stopAnimating()
UIApplication.sharedApplication().endIgnoringInteractionEvents()
if signupError == nil {
// Hooray! Let them use the app now.
println("signed up")
} else {
if let errorString = signupError.userInfo?["error"] as? NSString {
error = errorString
} else {
error = "Please try again later."
}
self.displayAlert("Could Not Sign Up", error: error)
}
}
} else {
PFUser.logInWithUsernameInBackground(username.text, password:password.text) {
(user: PFUser!, signupError: NSError!) -> Void in
self.activityIndicator.stopAnimating()
UIApplication.sharedApplication().endIgnoringInteractionEvents()
if signupError == nil {
println("logged in")
} else {
if let errorString = signupError.userInfo?["error"] as? NSString {
error = errorString
} else {
error = "Please try again later."
}
self.displayAlert("Could Not Log In", error: error)
}
}
}
}
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
Oh and I am also using Parse.
If all you are wanting to do is remember that a user is logged in across app closures/reboots, then you can simply save that fact with NSUserDefaults and check NSUserDefaults at launch. This is a pretty straightforward approach.
Using core data just to persist if a user is logged in is probably overkill.
If you are actually needing to remember their user name and password, then that is another question entirely. You should not user Core Data or NSUserDefaults in this scenario. To securely store user credentials you'll want to use the Keychain Service in the security framework.

Resources