What do I use to enter obscured input in an iPhone app?
I need to obtain a Social Security Number in my app, but I don't want the number itself to display in the interface. How do I go about obtaining the input without disclosing the confidential information on the screen?
I have to fill up this application which asking for my social security system number but instead of writing down the numbers it should appear as XXX-XX-XX开发者_如何转开发XX rather than the number itsself because it's confidential.
Use a secure text field instead of a normal text field (NSSecureTextField
).
But I bet interface builder lets you just drag and drop this field.
Update:
And in fact it does:
IB http://img156.imageshack.us/img156/1207/1007040002.png
NSSecureTextField is the equivelant of a password field in the iPhone.
Here's the description from Apple:
A secure text field is a type of text field that hides its text from display or other access via the user interface. It’s suitable for use as a password-entry object, or for any item in which a secure value must be kept. Your code can get the text field’s string value using the standard stringValue method, but users can’t see it or access it. It overrides many aspects of text editing to prevent passing of the object’s value out by mechanisms available to the user (namely, through Cut, Copy, and Paste commands, and the Services facility). This object also overrides the text system’s drawing routine to draw no text at all) . . . Every method in NSSecureTextFieldCell has a cover in NSSecureTextField. (A cover is a method of the same name that calls the original method. http://developer.apple.com/mac/library/documentation/Cocoa/Conceptual/TextField/Concepts/AboutSecureTextFields.html#//apple_ref/doc/uid/20000127-BAJCGDIH
Since NSSecureTextField derives from NSTextField, you can find any tutorial on the web on how to use NSTextField -- simply swap NSTextField for NSSecureTextField in the implementation. I bolded the important phrase above (Every method in NSSecureTextFieldCell has a cover in NSSecureTextField). Interact with the NSSecureTextField just as you would a NSTextField object, the interface is exactly the same at the API level.
The NSSecureTextField will show up in an application looking like this:
When I searched Google for "NSTextField Tutorial", the first result I found was from the following website: http://www.pietrop.com/wordpress/dev-area/tutorials/cocoa-tutorial-nstextfield-nsbutton/#english
In short, you'll end up with a header that looks something like this:
// Controller.h
#import <Cocoa/Cocoa.h>
@interface Controller : NSObject
{
//IBOutlets (the inteface's controllers)
IBOutlet NSSecureTextField *inputText;
IBOutlet NSTextField *displayedText;
IBOutlet NSButton *updateButton;
IBOutlet NSButton *clearButton;
}
/* IBActions (the user interface interactions) */
//Updates the text in the Wrapping Label
- (IBAction)updateText:(id)sender;
//Clears the text in the Wrapping Label and in the TextField
- (IBAction)clearText:(id)sender;
@end
The standard UITextField for the iPhone has a 'secure' option under Text Input Traits options in Interface Builder. Try that.
精彩评论