Post To App Delegate

October 28, 2012 by · Leave a Comment 


AppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
[appDelegate somePublicMethod]; 

Mac OS

AppDelegate *appDelegate = [[NSApplication sharedApplication] delegate];
[appDelegate somePublicMethod];

Validate Root User is executing the command

January 24, 2012 by · Leave a Comment 

if (getuid())) {
    NSLog(@"You must be root to run this command.");

Simple get arch type

November 21, 2011 by · Leave a Comment 

#include <mach-o/arch.h>

+ (NSString *)getHostArchType
	const NXArchInfo *info = NXGetLocalArchInfo();
	switch (info->cputype) {
			return @"ppc";
		case CPU_TYPE_I386:
			return @"i386";
			return @"ppc";
		case CPU_TYPE_X86_64:
			return @"i386";
			return @"na";

Populate NSMutableArray from NSArrayController

August 12, 2011 by · Leave a Comment 

NSMutableArray *newArray = [NSMutableArray arrayWithArray:[arrayController arrangedObjects]];

Append String to NSTextView

May 14, 2011 by · Leave a Comment 

- (void)appendStringToTextView:(NSString *)aStr
	NSRange tvRange;
    tvRange = NSMakeRange ([[textView string] length], 0);
    [textView replaceCharactersInRange:tvRange withString:aStr];
	[textView scrollRangeToVisible:tvRange];
    [textView display];

Add a timeout to NSTask

February 18, 2011 by · Leave a Comment 

// 1 minute timeout
NSDate *terminateDate = [[NSDate date] addTimeInterval:60.0];
while ((theTask != nil) && ([theTask isRunning]))	{
	if ([[NSDate date] compare:(id)terminateDate] == NSOrderedDescending)	{
		NSLog(@"Error: terminating task, timeout was reached.");
		[theTask terminate];
	[NSThread sleepForTimeInterval:1.0];

NSTask without worrying about the output buffer

February 18, 2011 by · 1 Comment 

Some times you might want to run NSTask without setting up a delegate for when data becomes available or to notify once the task has terminated because it in a procedural part of your application.

Here is a quick example using system_profiler and Applications…

NSTask *task = [[[NSTask alloc] init] autorelease];
[task setLaunchPath:@"/usr/sbin/system_profiler"];
[task setArguments:[NSArray arrayWithObjects:@"SPApplicationsDataType",nil]];

NSPipe *l_Pipe = [NSPipe pipe];
NSFileHandle *readHandle = [l_Pipe fileHandleForReading];

[task setStandardOutput:l_Pipe];
[task setStandardError:l_Pipe];
[task launch];

NSMutableData *data = [[NSMutableData alloc] init];
NSData *readData;
while ((readData = [readHandle availableData]) && [readData length]) {
    [data appendData: readData];
// We now have our full results in a NSString
NSString *spStringData = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];

[spStringData release];
[data release];
[task release];

Simple NSMutableArray sorting

February 16, 2011 by · Leave a Comment 

NSSortDescriptor *sortDesc = [[[NSSortDescriptor alloc] initWithKey:@"self" ascending:NO] autorelease];
[myArray sortUsingDescriptors:[NSArray arrayWithObject:sortDesc]];

Quickly Write NSString to file

January 27, 2011 by · Leave a Comment 

NSError* error = nil;
[@"The String to write to the file..." writeToFile:@"/tmp/aFile.txt" atomically:NO encoding:NSASCIIStringEncoding error:&error];


January 23, 2011 by · 1 Comment 

Integer Value Output

NSLog(@"BOOLEAN Value: %d", (int)aBool);

String Value Output

NSLog(@"BOOLEAN Value: %@", (aBool ? @"TRUE" : @"FALSE"));

Next Page »