Posted By

zingo on 07/09/08


Tagged

cocoa osx objc macro expire xcode gcc


Versions (?)

Who likes this?

10 people have marked this snippet as a favorite

rastersize
windmarble
enajenkins
cgrainger
mattnews
trawor
davidsi
wpellis
Tintenklecks
gospaz


Kill App after Expire Date (Suicidal Code Redux)


 / Published in: Objective C
 

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

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.

  1. // Based on original code by Daniel Jakult, based on an idea from Brian Cooke.
  2. #ifdef BETA // 4 week expiration
  3. #define EXPIREAFTERDAYS 28
  4. #endif
  5.  
  6. #if EXPIREAFTERDAYS
  7. NSString* compileDateString = [NSString stringWithUTF8String:__DATE__];
  8. NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
  9. [dateFormatter setFormatterBehavior:NSDateFormatterBehavior10_4];
  10. [dateFormatter setLocale:[[[NSLocale alloc] initWithLocaleIdentifier:@”en_US”]
  11. autorelease]];
  12. [dateFormatter setDateFormat:@"MMM dd yyyy"];
  13. NSDate *compileDate = [dateFormatter dateFromString:compileDateString];
  14. [dateFormatter release];
  15. NSDate *expireDate = [compileDate addTimeInterval:(60*60*24* EXPIREAFTERDAYS)];
  16.  
  17. if ([expireDate earlierDate:[NSDate date]] == expireDate)
  18. {
  19. // Run an alert or whatever
  20.  
  21. // Quit!
  22. [NSApp terminate:self];
  23. }
  24. #endif

Report this snippet  

You need to login to post a comment.