Post To App Delegate

October 28, 2012 by · Leave a Comment 

iOS

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.");
    exit(1);
}

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) {
		case CPU_TYPE_POWERPC:
			return @"ppc";
			break;
		case CPU_TYPE_I386:
			return @"i386";
			break;
		case CPU_TYPE_POWERPC64:
			return @"ppc";
			break;
		case CPU_TYPE_X86_64:
			return @"i386";
			break;
		default:
			return @"na";
			break;
	}
}

Compile Java class with external jar files

October 1, 2011 by · Leave a Comment 

This should be so easy, but not being much of a java developer anymore I was treating the jar files like libraries and only specifying the directory in the class path.

How to:


javacĀ -classpath classes:/path/to/jars/my.jar /path/to/java_files/my.java

Populate NSMutableArray from NSArrayController

August 12, 2011 by · Leave a Comment 

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

Singleton Template for XCode4

May 14, 2011 by · Leave a Comment 

This is a great template for XCode4, I use it and really like it.

http://blog.mugunthkumar.com/coding/objective-c-singleton-template-for-xcode-4/

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];
}

codesign, “signature too large to embed” error

February 18, 2011 by · Leave a Comment 

Override the size allocated for the signature by passing –signature-size to codesign.

codesign --signature-size 9400 -f -s "Code Sign Cert" ./myapp

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];

Next Page »