删除自己的Cocoa App

时间:2013-04-10 10:47:13

标签: macos cocoa

我正在使用Cocoa编写Mac OSX应用程序,该应用程序旨在在指定日期之后停止工作,以避免用户只是更改系统时钟然后重新运行应用程序我希望程序关闭并自行删除如果在到期日之后加载。这可能吗?

我直接分发应用程序而不是通过应用程序商店。此外,使用互联网检查日期并不是一个真正的选择,因为该应用程序需要离线使用。

谢谢, 马修

2 个答案:

答案 0 :(得分:3)

这是可能的,但不可靠。要删除您的应用程序,只需获取主包的URL并告诉NSFileManager删除它。但是您的应用程序包可能不可写 - 因此也不可删除 - 即使您设法删除它,用户也可以拥有任意数量的备份。除非我能够严格控制程序运行的系统,否则我不会写任何依赖于此的东西。 (我的意思是,我可能不会写一些这样做的东西,因为它有点疯狂。但如果我 要写这样的东西,它必须是只运行的东西我自己的系统。)

答案 1 :(得分:1)

您可以在系统中执行一些健全性检查,以了解用户是否手动将时钟设置回过去。

请注意,我仍然认为(恶意)删除用户文件的计划一般都不是一个好主意,特别是下面的方法肯定会在Sandboxing下破解。

..但出于好奇:这是一个片段,它会检查/var/log中的所有文件并返回其中一些文件是否已被修改(=系统很可能正在运行“过去“)

- (bool)isFakeSystemTime
{
   int futureFileCount = 0;

   // let's check against 1 day from now in the future to be safe
   NSTimeInterval secondsPerDay = 24 * 60 * 60;
   NSDate *tomorrow = [[[NSDate alloc] initWithTimeIntervalSinceNow:secondsPerDay] autorelease];

   NSString *directoryPath = @"/var/log";
   NSArray *filesInDirectory = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:directoryPath error:nil];

   for (NSString* fileName in filesInDirectory) 
   {
      NSDictionary *attributes = [[NSFileManager defaultManager] attributesOfItemAtPath:[directoryPath stringByAppendingPathComponent:fileName] error:nil];
      NSDate *date = [attributes valueForKey:@"NSFileModificationDate"];
      if (!date)
         continue;

      if ([date compare:tomorrow] == NSOrderedDescending)
      {
         NSLog(@"File '%@' modified >=1 day in the future", fileName);
         futureFileCount++;
      }
   }   

   // again, some heuristic to be (more) on the safe side
   return futureFileCount > 5;
}