终止日期后终止应用程序(自杀代码Redux)

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了终止日期后终止应用程序(自杀代码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

以上是关于终止日期后终止应用程序(自杀代码Redux)的主要内容,如果未能解决你的问题,请参考以下文章

做个日期查询,判断开始日期与终止日期范围必须在一个月之内,用java代码

Spring Boot / REST - 示例代码在启动后终止

JAVA怎样终止代码的执行?

*** 由于未捕获的异常“NSGenericException”而终止应用程序,原因:“开始日期不能晚于结束日期!”

如何在父进程终止后终止所有子进程?

Java:抛出异常后如何终止执行后面的代码?