2

I have a UITextfield for entering password with SECURE attribute enabled.

When the user taps outside the UITextfield after inputting a certain number of characters, and then taps back again to the UITextfield for editing, the following behavior is taking place:

iOS 5.1 -When I try to delete a character(using backspace from keyboard) from the UITextfield, the last character is deleted.

iOS 6.0 -When I try to delete a character from the UITextfield, all typed characters are getting deleted.

Am I doing something wrong, or is this a bug in iOS 6?

footyapps27
  • 3,982
  • 2
  • 25
  • 42

4 Answers4

6

This is the intended behaviour under iOS6 and you shouldn't probably change it.

If for whatever reason you really need this, you can override UITextField's delegate method textField:shouldChangeCharactersInRange:replacementString: and restore the old behaviour:

#import "ViewController.h"

@interface ViewController () <UITextFieldDelegate>
@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.passwordField.delegate = self;
}

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    if (range.location > 0 && range.length == 1 && string.length == 0)
    {
        // iOS is trying to delete the entire string
        textField.text = [textField.text substringToIndex:textField.text.length - 1];
        return NO;
    }
    return YES;
}

@end
Tomas Camin
  • 9,996
  • 2
  • 43
  • 62
  • 1
    this is not correct. simply hitting backspace in the textField will cause your if statement to be true, therefore "iOS is trying to delete the entire string" isn't valid. What you've actually proposed will disable the ability to delete characters. I can't down vote your answer, but it should be removed/edited. – crizzwald May 27 '13 at 01:51
5

There is also an issue with iOS 6 w/ regard to this new behavior. Try this:

  1. Navigate to a view with two UITextFields, one which is secure
  2. Type anything into the non-secure text field.
  3. Tap into the secure text field, do not type or delete anything. Tap back into the non-secure field
  4. Hit backspace once

Viola! You have nothing in your text field now. I am able to reproduce this consistently with iOS 6.0.x and 6.1

  • I'm seeing this too. Has anyone filed a bug with Apple? – dkmp Apr 22 '13 at 21:17
  • Same exact problem. How did you all decide to solve this issue? – danbretl May 01 '13 at 21:54
  • We can fix this if set Clear Button = Appears When Editing. This is not correct fix, but this helps to avoid bug. – George Aug 01 '13 at 10:46
  • 1
    I ran into this when leaving a view with a secure text field and displaying another view. A non-secure text field on the other view would pick up the previous secure text field's backspace behavior the very first time backspace was tapped. All subsequent backspace taps worked as they should. This behavior was solved by calling deleteBackward on the secure text field before navigating away from the view as a way to "use up" the latent delete-everything behavior so it isn't accidentally applied to future text fields. – Oran Dennison Aug 24 '13 at 01:06
2

Looks like this can be fixed by explicitly setting secureTextEntry to NO in viewDidLoad or thereabouts.

- (void)viewDidLoad {
    [super viewDidLoad];

    self.textField.secureTextEntry = NO;
}

This seems to work with the case described above (a view with an email field and a secure password field on it), under iOS 6.

HTH!

Raconteur
  • 1,381
  • 1
  • 15
  • 29
  • 1
    I can confirm this solves the already non-secure textfield being cleared after selecting a secure textfield. Looks like a bug, since the field was already defined as non-secure in Interface Builder. – leolobato Jun 28 '13 at 09:18
0

Here is a way to detect if a secure field is going to be cleared by a backspace:

iOS 6 UITextField Secure - How to detect backspace clearing all characters?

Community
  • 1
  • 1
crizzwald
  • 1,037
  • 2
  • 14
  • 30