【问题标题】:Add 30 days to the date by not taking system date in account in Nsdate在 Nsdate 中不考虑系统日期,将日期添加 30 天
【发布时间】:2012-10-10 12:15:45
【问题描述】:

我想在文件夹创建日期上增加 30 天,并在 30 天后阻止该应用程序。 如果他们更改系统日期,我的应用程序也应该在计数后关闭。

NSDateComponents *components;
NSString *path = @"/Users/Syed/Library/Swasstik/KITSMAW0051_SWASSTIK_DB.sqlite";
BOOL isFile = [[NSFileManager defaultManager] fileExistsAtPath:path isDirectory:NO];
if (isFile) {
NSLog(@"File exists");
NSDictionary* fileAttribs = [[NSFileManager defaultManager] attributesOfItemAtPath:path error:nil];
NSDate *result = [fileAttribs fileCreationDate]; //or fileModificationDate
NSLog(@"%@",result);
  NSDate* date = [NSDate date];
NSLog(@"%@", [date description]);

NSCalendar *gregorianCalendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
components = [gregorianCalendar components:NSDayCalendarUnit
                                                    fromDate:result
                                                      toDate:date
                                                     options:0];

[gregorianCalendar release];
NSLog(@"%ld", [components day]);

请帮我解决这个问题。 提前致谢。

【问题讨论】:

  • 我不明白你的问题是什么。如果您获得文件的创建日期,您可以将其与现在的时间进行比较。
  • @codingFriend1 我不想与系统日期进行比较。因为用户可以更改系统日期并使用我的应用程序。

标签: macos ios5 xcode4.2 nsdate


【解决方案1】:

您可以尝试在系统上查找最近修改的文件(例如,用户的 Preferences 文件夹中的一个,因为这些文件修改非常频繁)并与该修改日期进行比较。即使用户返回他的系统时间,之前修改过的文件也会保留其较新的修改日期。

如果用户将他的系统时间永久保留在过去(如 1990 年),这当然不会起作用,但它会阻止尝试只返回时间来使用您的应用程序。我认为有人会永远保持他的系统时间错误的可能性很小,因为这真的很不方便。

【讨论】:

  • 有没有其他方法可以做到这一点???。就像在文件夹创建日期中添加 30 天一样。
  • 为什么要在创建日期上增加 30 天?但是,这当然可以使用 NSFileManager 的setAttributes:ofItemAtPath:error: 方法,类似于您在上面的代码中读取属性的方式。
  • 我想在 30 天后阻止该应用,所以我想喜欢这个。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-12-09
  • 2015-12-16
  • 1970-01-01
相关资源
最近更新 更多