简体   繁体   中英

Delete Cache in iOS. Cocoa error 513

My app have to download many image from web. So I want to provide a button to clear caches of this images.

  • Are these images save in /Library/Caches ?

I Assuming that there are stored in the /Library/Caches. So I attempt to delete these files by:

filemgr = [NSFileManager defaultManager];

NSError *error;
[filemgr removeItemAtPath: [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) objectAtIndex:0] error:&error]
// OR
[filemgr removeItemAtPath:[NSHomeDirectory() stringByAppendingString:@"/Library/Caches"] error:&error];

Both are error with The operation couldn't be completed. (Cocoa error 513.) The operation couldn't be completed. (Cocoa error 513.) in iOS device, but worked in Simulator.

Thank you for answer!

You cant remove "/Library/Caches". You need to remove all items in"/Library/Caches"

NSString *path = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)[0];
for (NSString *filePath in  [[NSFileManager defaultManager] enumeratorAtPath:path])
{
    NSError *error = nil;
    [[NSFileManager defaultManager] removeItemAtPath:[path stringByAppendingPathComponent:filePath] error:&error];
    if (error)
    {
        NSLog(@"%@",error.description);
    }
}

You can not remove direct Cache folder. You have to parse every file from Cache folder and remove them.

Thanks.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM