Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to switch between hide and view password in ios?

Is there a clever way to let the user switch between hide and view password in an ios/iphone UITextField where the user can enter a password and would have the option of either hide it or view it. thanks in advance.

like image 840
MR Mido Avatar asked Dec 17 '12 18:12

MR Mido


People also ask

Is there a password manager on iPhone?

With iCloud Keychain, you can keep your passwords and other secure information updated across your devices. iCloud Keychain remembers things, so that you don't have to. It auto-fills your information—like your Safari usernames and passwords, credit cards, and Wi-Fi passwords on any device that you approve.


1 Answers

Simply setting

.secureTextEntry = YES

wont work I think due to a bug (or a feature), if the textField has focus.

Use something like this to make it work also if the textField is currently firstResponder

-(void) toggleTextFieldSecureEntry: (UITextField*) textField {
    BOOL isFirstResponder = textField.isFirstResponder; //store whether textfield is firstResponder

    if (isFirstResponder) [textField resignFirstResponder]; //resign first responder if needed, so that setting the attribute to YES works
    textField.secureTextEntry = !textField.secureTextEntry; //change the secureText attribute to opposite
    if (isFirstResponder) [self.textField becomeFirstResponder]; //give the field focus again, if it was first responder initially
}
like image 69
Mario Avatar answered Sep 20 '22 14:09

Mario