Password validation in UITextField in iOS

This is how I would do it. The validation should be done at the end when the user has typed in the password and not in between.I will not be using NSRegularExpression. -(void)textFieldDidEndEditing:(UITextField *)textField{ int numberofCharacters = 0; BOOL lowerCaseLetter,upperCaseLetter,digit,specialCharacter = 0; if([textField.text length] >= 10) { for (int i = 0; i < [textfield.text … Read more

Run AppleScript from Cocoa Application

Solved! Xcode wasn’t saving my script file into app’s resources path. To run an AppleScript code from Cocoa Application, use this: NSString *path = [[NSBundle mainBundle] pathForResource:@”ScriptName” ofType:@”scpt”]; NSURL *url = [NSURL fileURLWithPath:path];NSDictionary *errors = [NSDictionary dictionary]; NSAppleScript *appleScript = [[NSAppleScript alloc] initWithContentsOfURL:url error:&errors]; [appleScript executeAndReturnError:nil]; Swift 5.6.1: import AppleScriptObjC import Cocoa let path = … Read more

Listen to a value change of my text field

You can set a delegate for your NSTextField instance and have the delegate implement the following method: – (void)controlTextDidChange:(NSNotification *)notification { // there was a text change in some control } Your delegate object can be the application delegate, a window controller, a view controller, or some other object in your application. The delegate can … Read more

How to Get the Display Name with the Display ID in Mac OS X?

This gives you the localized display name: static void KeyArrayCallback(const void* key, const void* value, void* context) { CFArrayAppendValue(context, key); } – (NSString*)localizedDisplayProductName { NSDictionary* screenDictionary = [[NSScreen mainScreen] deviceDescription]; NSNumber* screenID = [screenDictionary objectForKey:@”NSScreenNumber”]; CGDirectDisplayID aID = [screenID unsignedIntValue]; CFStringRef localName = NULL; io_connect_t displayPort = CGDisplayIOServicePort(aID); CFDictionaryRef dict = (CFDictionaryRef)IODisplayCreateInfoDictionary(displayPort, 0); CFDictionaryRef names … Read more

+[NSString stringWithString:] — what’s the point?

You might have a NSMutableString (or some home-grown NSString subclass) that you want to duplicate. NSMutableString *buffer = [NSMutableString string]; // do something with buffer NSString *immutableStringToKeepAround = [NSString stringWithString:buffer]; Of course, you can also just make a copy: NSMutableString *buffer = [NSMutableString string]; // do something with buffer NSString *immutableStringToKeepAround = [[buffer copy] autorelease]; … Read more

3 questions about extern used in an Objective-C project

1) you’re specifying its linkage. extern linkage allows you or any client to reference the symbol. regarding global variables: if the variable is mutable and/or needs proper construction, then you should consider methods or functions for this object. the notable exception to this is NSString constants: // MONClass.h extern NSString* const MONClassDidCompleteRenderNotification; // MONClass.m NSString* … Read more

Writing a privileged helper tool with SMJobBless()

XPC isn’t an option if you’re trying to elevate privileges (from https://developer.apple.com/library/archive/documentation/MacOSX/Conceptual/BPSystemStartup/Chapters/CreatingXPCServices.html): By default, XPC services are run in the most restricted environment possible—sandboxed with minimal filesystem access, network access, and so on. Elevating a service’s privileges to root is not supported. SMJobBless will install a helper tool and register it with Launchd, as in … Read more