首页 文章

如何删除ios应用程序的tmp目录文件?

提问于
浏览
31

我正在开发一个使用iPhone相机的应用程序,在进行了多次测试后,我意识到它将所有捕获的视频存储在应用程序的tmp目录中 . 即使手机重新启动,捕获也不会消失 .

有没有办法删除所有这些捕获或有没有办法轻松清理所有缓存和临时文件?

6 回答

  • 20

    Swift 4

    一种可能的实现方式

    extension FileManager {
        func clearTmpDirectory() {
            do {
                let tmpDirURL = FileManager.default.temporaryDirectory
                let tmpDirectory = try contentsOfDirectory(atPath: tmpDirURL.path)
                try tmpDirectory.forEach { file in
                    let fileUrl = tmpDirURL.appendingPathComponent(file)
                    try removeItem(atPath: fileUrl.path)
                }
            } catch {
               //catch the error somehow
            }
        }
    }
    
  • 3

    Swift 3版本作为扩展:

    extension FileManager {
        func clearTmpDirectory() {
            do {
                let tmpDirectory = try contentsOfDirectory(atPath: NSTemporaryDirectory())
                try tmpDirectory.forEach {[unowned self] file in
                    let path = String.init(format: "%@%@", NSTemporaryDirectory(), file)
                    try self.removeItem(atPath: path)
                }
            } catch {
                print(error)
            }
        }
    }
    

    用法示例:

    FileManager.default.clearTmpDirectory()
    

    感谢Max Maier,Swift 2版本:

    func clearTmpDirectory() {
        do {
            let tmpDirectory = try NSFileManager.defaultManager().contentsOfDirectoryAtPath(NSTemporaryDirectory())
            try tmpDirectory.forEach { file in
                let path = String.init(format: "%@%@", NSTemporaryDirectory(), file)
                try NSFileManager.defaultManager().removeItemAtPath(path)
            }
        } catch {
            print(error)
        }
    }
    
  • 5

    是 . 这种方法效果很好:

    + (void)clearTmpDirectory
    {
        NSArray* tmpDirectory = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:NSTemporaryDirectory() error:NULL];
        for (NSString *file in tmpDirectory) {
            [[NSFileManager defaultManager] removeItemAtPath:[NSString stringWithFormat:@"%@%@", NSTemporaryDirectory(), file] error:NULL];
        }
    }
    
  • 2

    Try this code to remove NSTemporaryDirectory files

    -(void)deleteTempData
    {
        NSString *tmpDirectory = NSTemporaryDirectory();
        NSFileManager *fileManager = [NSFileManager defaultManager];
        NSError *error;
        NSArray *cacheFiles = [fileManager contentsOfDirectoryAtPath:tmpDirectory error:&error];
        for (NSString *file in cacheFiles)
        {
            error = nil;
            [fileManager removeItemAtPath:[tmpDirectory stringByAppendingPathComponent:file] error:&error];
        }
    }
    

    and to check data remove or not write code in didFinishLaunchingWithOptions

    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
        // Override point for customization after application launch.
        self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
        [self.window makeKeyAndVisible];
    
        NSString *tmpDirectory = NSTemporaryDirectory();
        NSFileManager *fileManager = [NSFileManager defaultManager];
        NSError *error;
        NSArray *cacheFiles = [fileManager contentsOfDirectoryAtPath:tmpDirectory error:&error];
        NSLog(@"TempFile Count ::%lu",(unsigned long)cacheFiles.count);
    
        return YES;
    }
    
  • 4

    这适用于越狱的iPad,但我认为这应该适用于非越狱设备 .

    -(void) clearCache
    {
        for(int i=0; i< 100;i++)
        {
            NSLog(@"warning CLEAR CACHE--------");
        }
        NSFileManager *fileManager = [NSFileManager defaultManager];
        NSError * error;
        NSArray * cacheFiles = [fileManager contentsOfDirectoryAtPath:NSTemporaryDirectory() error:&error];
    
        for(NSString * file in cacheFiles)
        {
            error=nil;
            NSString * filePath = [NSTemporaryDirectory() stringByAppendingPathComponent:file ];
            NSLog(@"filePath to remove = %@",filePath);
    
            BOOL removed =[fileManager removeItemAtPath:filePath error:&error];
            if(removed ==NO)
            {
                NSLog(@"removed ==NO");
            }
            if(error)
            {
                NSLog(@"%@", [error description]);
            }
        }
    }
    
  • 44

    感谢Max Maier和Roman Barzyczak . 更新到Swift 3,使用URL而不是字符串 .

    斯威夫特3

    func clearTmpDir(){
    
            var removed: Int = 0
            do {
                let tmpDirURL = URL(string: NSTemporaryDirectory())!
                let tmpFiles = try FileManager.default.contentsOfDirectory(at: tmpDirURL, includingPropertiesForKeys: nil, options: .skipsHiddenFiles)
                print("\(tmpFiles.count) temporary files found")
                for url in tmpFiles {
                    removed += 1
                    try FileManager.default.removeItem(at: url)
                }
                print("\(removed) temporary files removed")
            } catch {
                print(error)
                print("\(removed) temporary files removed")
            }
    }
    

相关问题