We Recommend

Programming in Objective-C Programming in Objective-C
Programming in Objective-C is a concise, carefully written tutorial on the basics of Objective-C and object-oriented programming. The book makes no assumption about prior experience with object-oriented programming languages or with the C language (upon which Objective-C is based). And because of this, both novice and experienced programmers alike can use this book to quickly and effectively learn the fundamentals of Objective-C.


Posted By

zingo on 07/09/08


Tagged

cocoa osx objc macro expire xcode gcc


Versions (?)


Who likes this?

1 person has marked this snippet as a favorite

rastersize


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.