search

Home  >  Q&A  >  body text

objective-c - iOS开发,清除缓存功能在iOS8上有效,在iOS9和10上无效

清除缓存的代码如下:

// 清理缓存
    NSString *cachePath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) objectAtIndex:0];
    NSArray *files = [[NSFileManager defaultManager] subpathsAtPath:cachePath];
    NSLog(@"文件数 :%ld",[files count]);
    for (NSString *p in files)
    {
        NSError *error;
        NSString *path = [cachePath stringByAppendingString:[NSString stringWithFormat:@"/%@",p]];
        if([[NSFileManager defaultManager] fileExistsAtPath:path])
        {
            [[NSFileManager defaultManager] removeItemAtPath:path error:&error];
        }
    }

在iOS8上可以清理缓存,在iOS9 和 iOS10上无效。

怪我咯怪我咯2771 days ago579

reply all(1)I'll reply

  • 阿神

    阿神2017-04-18 09:45:28

    Check whether the path is valid first! Did you get an absolute path or a relative path?
    Secondly
    NSString *path = [cachePath stringByAppendingString:[NSString stringWithFormat:@"/%@",p]];
    It is best to use stringByAppendingPathComponent this way

    reply
    0
  • Cancelreply