应用进入后台时删除缓存文件夹

时间:2014-07-11 13:10:36

标签: ios snapshot

我正在使用以下代码删除我的iOS应用的缓存文件夹。它在模拟器上工作正常。

- (void)applicationWillResignActive:(UIApplication *)application
{
    __block UIBackgroundTaskIdentifier bgTask = [application beginBackgroundTaskWithExpirationHandler:^{

    if (bgTask != UIBackgroundTaskInvalid) {
        [application endBackgroundTask:bgTask];
        bgTask = UIBackgroundTaskInvalid;
    }
  }];
  dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
    int64_t delayInSeconds = 3;
    dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC);

    UIBackgroundTaskIdentifier myId = bgTask;

    dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
        NSFileManager *fileManager = [NSFileManager defaultManager];
        static NSString *theCachesPath = nil; // Application caches path string
        NSArray *cachesPaths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
        theCachesPath = [[cachesPaths objectAtIndex:0] copy]; //
        [fileManager removeItemAtPath:theCachesPath error:NULL]; /// remove cache
        [application endBackgroundTask:myId];
        bgTask = UIBackgroundTaskInvalid;
    });
  });
}

问题是,当在设备上时,我按下主页按钮,应删除应用程序快照(存储在缓存文件夹中),但不会发生。

请帮忙。

1 个答案:

答案 0 :(得分:0)

如果您尝试禁用应用程序快照,则可以将屏幕截图更改为空白屏幕或隐藏手动存储的敏感数据,或者如果它是企业应用程序,则可以将它们全部停止。

这里的回答很好:https://stackoverflow.com/a/18961652/2969035

相关问题