Password visible when Typing then *** - objective-c

I would like to make my password field visible when typing and when finished (change to another field) convert it to *.
As my app has only one password field (no confirmation) its easy the user mistype.
How can I do that?

try this,
UITextField *txtPassword = [[UITextField alloc] init];
txtPassword.secureTextEntry =YES;

You can follow these steps:
set pwdTextField.delegate = self
implement textfieldDidEndEditing delegate
Do nothing special while user input text
Now in textfieldDidEndEditing store your current password in a variable (for future use)
Replace pwdTextField.text = noOfStarsEqualToLenghtOfString
That's it. Happy coding.

Firstly I suggest to use the secure textfield. Otherwise try this:
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
if (textField == txtPassword)
{
//Check total no of characters in that textfield & replace all with *.
}
return YES;
}
And on confirm button clicked(register/login), check the password with the password which you added before replacing with *. Consider * just for displaying purpose.

Related

Resetting the search query string in UISearchController programmatically

I am failing to reset/substitute the search query string programmatically. I was trying to modify the UISearchBar query string from within the UISearchControllerDelegate.
I am using dictation input.
When the command "delete" is said, the searchController.searchBar.text should be set back to #"". The speech detection should continue as normal with the empty string.
The query string does get reset to #"", but the speech detection stops.
How can I reset search query string in UISearchController programmatically and still be able to continue the input using speech?
- (void)updateSearchResultsForSearchController:(nonnull UISearchController *)searchController {
NSString *searchQuery = self.searchController.searchBar.text;
// Deletion command
if ([self.searchController.searchBar.text hasSuffix:#"delete"]) {
// Things that I've tried that don't work
//[self.searchController.searchBar setText:#""];
//self.searchController = [self.searchController init];
//self.searchController.searchBar.text = #"";
searchQuery = #"";
}
}
This feature is yet not available in iOS. As soon as you try to clear the text in searchBar's TextField, it change the keyboard mode to keypad input.
So, the conclusion is, you can only provide the input to the textField using the dictation but can't clear it. User has to manually do the process to change again to Dictation Input.
Hope it helps.

plist.Info so that app takes icon based on command line arguments

I have a .app. I want to edit the plist.Info such that if a command line argument of -P "main" is in the path it will use another of the icons in my resources folder. And if user right clicked and said "keep in dock" it will keep in dock with command line arguments, so on next click it will launch with same command line arguments.
Is this possible?
Worst cast scenario: Any objective-c way to check the path to see if any command line arguments there? Then I'll run setApplicationIconImage programtically (worst case meaning if the above is not possible) (and then ill also have to programtically fetch the miniaturized windows with [NSWindow miniwindowImage] and draw the mini icon on their msyelf and also listen to future notifications of NSWindowWillMiniaturizeNotification and do the draw when that notification fires, so this is worst case scenario)
I am not sure I follow you fully.
But I don't think you need to edit the plist.Info and I think it is not good to do that any way.
I would just to write to the apps preference file with CFPreferencesSetValue and change an entry which determines if the app changes it's icon.
Call made from you argument check:
[self changIcon:(CFBooleanRef)false];
-(void) changIcon:(CFBooleanRef)prefValue
{
CFStringRef appID = CFSTR("com.yourApp.BundleID");
CFStringRef Key = CFSTR("swapIcon");
CFBooleanRef Value = prefValue ;// kCFBooleanTrue;//or kCFBooleanFalse
// Set up the preference.
CFPreferencesSetValue(Key,
Value,
appID,
kCFPreferencesCurrentUser,
kCFPreferencesAnyHost);
// Write out the preference data.
CFPreferencesSynchronize(appID,
kCFPreferencesCurrentUser,
kCFPreferencesAnyHost);
}
Change the icon
NSUserDefaults * defaults = [NSUserDefaults standardUserDefaults];
BOOL swapIcon = [defaults boolForKey:#"swapIcon"];
if (swapIcon ) {
NSImage * b1Image = [NSImage imageNamed:#"bl1"];
[NSApp setApplicationIconImage:b1Image];
}else {
[NSApp setApplicationIconImage:nil];//--Nil will make the app use thenormal icon
}
For a better answer you would need to explain a bit more clearly.

Xcode Like log with NSTextView

I'm trying to set up a NSTextView like the console in Xcode (or pretty much any other IDE available). That being the user cannot edit the NSTextView, however they can put in a character when appropriate, I'm trying to set up that same functionality. No clue how to go about it. Any ideas?
You could simply make an action that appends a formatted string containing a line break, a time stamp, and your desired text to the text view. Here's an example:
- (void)addToLog:(NSString *)input
{
[[self.myTextView textStorage] appendAttributedString:[[NSAttributedString alloc] initWithString:[NSString stringWithFormat:#"\n%#: %#",[NSDate date],input]]];
}
So then instead of using NSLog(#"some text"); you could call [self addToLog:#"some text"]; and it would be added to a new line in your text view.

How do I display a popup informing a NSTextField can't be empty in cocoa ?

How do I display a popup informing a NSTextField can't be empty in cocoa ?
If the user click apply and the NSTextField is empty a popup should appear saying the field can't be empty.
thanks
The answer by #beryllium only tells part of the story.
In fact, to properly validate text field input in Cocoa you should be using an NSFormatter attached to your text field cell and then in your NSTextFieldDelegate you should implement the method: control:didFailToFormatString:errorDescription:. In this delegate method you can prompt the user to correct their input.
All you need to do in your NSFormatter subclass is something like this:
#implementation RKTextFormatter
- (NSString*)stringForObjectValue:(id)object
{
return (NSString*)object;
}
- (BOOL)getObjectValue:(id*)object forString:(NSString*)string errorDescription:(NSString**)error {
BOOL stringPassesTest = NO;
//put your test here
if(!stringPassesTest)
{
//set the error and return NO
if(error)
*error = [NSError errorWithDomain:#"YourErrorDomain" code:1 userInfo:[NSDictionary dictionaryWithObject:#"It's a bingo" forKey:NSLocalizedDescriptionKey]];
return NO;
}
//otherwise, just assign the string
*object = string;
return YES;
}
#end
You would assign the formatter to your text field like so:
RKTextFormatter* formatter = [[RKTextFormatter alloc] init];
[[textField cell] setFormatter:formatter];
And then in your NSTextFieldDelegate you handle any invalid input:
- (BOOL)control:(NSControl *)control didFailToFormatString:(NSString *)string errorDescription:(NSString *)error
{
//display an alert, obviously it would be more useful than this
NSAlert* alert = [NSAlert alertWithMessageText:#"You have failed me for the last time" defaultButton:#"Revise Input" alternateButton:nil otherButton:nil informativeTextWithFormat:#"%#",error];
[alert beginSheetModalForWindow:control.window modalDelegate:nil didEndSelector:NULL contextInfo:NULL];
//if you return NO here, the user's input will not be accepted
//and the field will remain in focus
return NO;
}
If the user click apply and the NSTextField is empty a popup should
appear saying the field can't be empty.
Please, please, do not do this. You can be smarter than that.
Instead of investing your time in writing an alert dialog to handle the "unexpected" situation, invest it in creating a method that prevents the problem from occurring in the first place: keep the Apply button disabled until a proper value has been entered in the text field.
In addition, as #Rob Keniger mentioned, you should consider using NSFormatters to validate the input to make sure it's of the appropriate kind.
Try to use this code:
- (IBAction)pushBtn:(id)sender {
if(self.textfield.stringValue.length == 0){
NSAlert * alert = [NSAlert alertWithMessageText:#"Error"
defaultButton:#"OK"
alternateButton:nil
otherButton:nil
informativeTextWithFormat:#"Enter a text into text field"];
[alert beginSheetModalForWindow:self.window
modalDelegate:self
didEndSelector:#selector(alertDidHidden:)
contextInfo:nil];
//[alert runModal]; - more simple way
}
}
-(void)alertDidHidden:(NSAlert *)alert{
}

Cocoa Touch - Keyboard Capturing

How can I tell what keys the user pressed into a textView?
And before you ask since it sounds similar to a keylogger, I'm making a typing app and I need to know if what they entered of the correct matching key to what they were prompted.
Thanks!
You should set the delegate of the UITextView to one of your classes. (in IB or programmatically, does not matter)
In your delegate, you can put the following function, or something similar:
-(BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range
replacementText:(NSString *)text
{
if ( [text length] == 0 ) return YES; // always allow deletion of characters
NSString *new = [textView.text stringByReplacingCharactersInRange:range
withString:text];
if ( [new length] > 100 ) // PUT IN YOUR MAGIC CONDITION HERE
{
return NO; // don't allow the edit to happen
}
return YES; // by default, allow the edit to happen
}
this will only limit input to 100 chars, but you can make it as complicated as you see fit.
edit ps, you asked "what key the user pressed", but since we also have copy&paste and auto-correction, this may give a text which is longer than 1 char!