Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UITextField secureTextEntry - works going from YES to NO, but changing back to YES has no effect

Tags:

uitextfield

The above says it all- I have a UITextField set to secure, but want to give users the option to make it not secure (so they can see for sure what they typed if they are in a private area). However, suppose they hit the toggle by mistake, and want to change it back to secure mode? That does not work. I've tried everything - using -1 instead of YES, deleting the text and then putting it back. I'm at a total loss on other ideas. [Entered rdar://9781908]

EDIT: I believe this issue is fixed as of iOS5.

like image 494
David H Avatar asked Jul 15 '11 16:07

David H


People also ask

What is secure text entry?

A Boolean value that indicates whether a text object disables copying, and in some cases, prevents recording/broadcasting and also hides the text.


1 Answers

It must be input-focus issue: when focused, UITextField can change only ON->OFF.
Try next trick to switch OFF->ON:

textField.enabled = NO; textField.secureTextEntry = YES; textField.enabled = YES; [textField becomeFirstResponder]; 

EDIT (dhoerl): In iOS 9.3, I found this works but there is a problem. If you enter say 3 characters in plain view, then switch to secure text, then type a character, the 3 pre-existing characters disappear. I tried all kinds of trick to clear, then reset the text without success. I finally tried just playing with the control by cutting and pasting - pasting into the newly-switched-to-secure-mode worked great. So I emulated it in code, and now it all works fine - no need to play with the responder either. Here is what I finally ended up with:

    if textview should go into secure mode and the textfield is not empty {         textField.text = ""         textField.secureTextEntry = true          UIPasteboard.generalPasteboard().string = password         textField.paste(self)         UIPasteboard.generalPasteboard().string = ""     } 

I'd prefer to not have to do this paste, but if you want it seamless this is the only way I can find to do it.

like image 113
MikeR Avatar answered Oct 06 '22 16:10

MikeR