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

如何删除ios应用程序的tmp目录文件?,ios,xcode,file-io,temp,Ios,Xcode,File Io,Temp,我正在开发一款使用iPhone摄像头的应用程序,在做了几次测试后,我意识到它正在将所有捕获的视频存储在应用程序的tmp目录中。 即使重新启动手机,捕捉到的信息也不会消失 有没有办法删除所有这些捕获,或者有没有办法轻松清理所有缓存和临时文件?这在越狱iPad上有效,但我认为这在非越狱设备上也应该有效 -(void) clearCache { for(int i=0; i< 100;i++) { NSLog(@"warning CLEAR CACHE------

我正在开发一款使用iPhone摄像头的应用程序,在做了几次测试后,我意识到它正在将所有捕获的视频存储在应用程序的tmp目录中。 即使重新启动手机,捕捉到的信息也不会消失


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

这在越狱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]);
        }
    }
}
-(无效)clearCache
{
对于(int i=0;i<100;i++)
{
NSLog(@“警告清除缓存--------”);
}
NSFileManager*fileManager=[NSFileManager defaultManager];
n错误*错误;
NSArray*cacheFiles=[fileManager内容目录路径:NSTemporaryDirectory()错误:&错误];
用于(缓存文件中的NSString*文件)
{
误差=零;
NSString*filePath=[NSTemporaryDirectory()stringByAppendingPathComponent:file];
NSLog(@“要删除的文件路径=%@”,文件路径);
BOOL removed=[fileManager removitematpath:filePath错误:&error];
如果(删除==否)
{
NSLog(@“删除==否”);
}
如果(错误)
{
NSLog(@“%@,[错误描述]);
}
}
}

是。这种方法效果很好:

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

尝试使用此代码删除NSTemporaryDirectory文件

-(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];
    }
}
- (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;
}
并检查数据删除或不在didFinishLaunchingWithOptions中写入代码

-(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];
    }
}
- (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;
}

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)
    }
}

感谢Max Maier和Roman Barzyczak。更新为Swift 3,使用URL代替字符串

斯威夫特3
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
        }
    }
}

让你知道,我在一台未越狱的iPhoneIOS6上试过这个,效果很好。谢谢。怎么样
[[NSFileManager defaultManager]removitematpath:NSTemporaryDirectory()错误:NULL]?@Itachi不应删除该目录。某些操作失败。应尝试fileManager.contentsOfDirectory(atPath:tmpdirrl.path)。它是一个扩展,因此不需要这样做。我将为每个
removietem
元素添加一个辅助do捕获。如果一个元素被卡住,其余的将永远不会删除。当这是FileManager的扩展名时,为什么要引用默认的文件管理器?