The Daydream Blog

Suicidal Code Redux

Back in July, Daniel Jakult posted some sample code for some suicidal code that prevents your application from running beyond a fixed period after it was compiled. Ideal for a Beta copies of your software that you don’t want used beyond a certain period, without having to deal with licences. For example the Beta release of Differencia that was released last week.

In the comments for Daniel’s post it was pointed out that the sample code was not internationalised and might cause an issue when run outside of English speaking countries. Of the suggested solutions in the comments, one, I suspect, exhibits the same issue and the other is very complex.

I discussed a potential solution with Michal Bencur (benko23) in the #macsb IRC channel a couple of weeks ago and I’ve posted some sample code to resolve the locale issue below.

The gcc documentation states that __DATE__ is in the format “MMM dd yyyy”, but I have no idea whether gcc will localise this when compiling in non English locales, but if it does, just replace “en_US” with the locale you are using when compiling.

The code is also Tiger only, as it uses a 10_4 behaviour date formatter for proper locale handling.

// 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

Leave a Reply

 
Site by Line