Return to Snippet

Revision: 7143
at July 9, 2008 22:39 by zingo


Initial Code
// Based on original code by Daniel Jakult, based on an idea from Brian Cooke.
#ifdef BETA  // 4 week expiration
#define EXPIREAFTERDAYS 28
#endif

#if EXPIREAFTERDAYS
NSString* compileDateString = [NSString stringWithUTF8String:__DATE__];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setFormatterBehavior:NSDateFormatterBehavior10_4];
[dateFormatter setLocale:[[[NSLocale alloc] initWithLocaleIdentifier:@”en_US”]
	autorelease]];
[dateFormatter setDateFormat:@"MMM dd yyyy"];
NSDate *compileDate = [dateFormatter dateFromString:compileDateString];
[dateFormatter release];
NSDate *expireDate = [compileDate addTimeInterval:(60*60*24* EXPIREAFTERDAYS)];

if ([expireDate earlierDate:[NSDate date]] == expireDate)
{
	// Run an alert or whatever

	// Quit!
	[NSApp terminate:self];
}
#endif

Initial URL
http://www.daytimesoftware.com/blog/2007/10/suicidal-code-redux

Initial Description
Using a gcc predefined macro, __DATE__, the code can know for itself when it was compiled, and build in an expiration date based on that value.

The former code (http://snipplr.com/view/3448/kill-app-after-expire-date/) was not internationalised and might cause an issue when run outside of English speaking countries. This code resolves the locale issue.

Initial Title
Kill App after Expire Date (Suicidal Code Redux)

Initial Tags
osx

Initial Language
Objective C