从文件中删除缓存失败

时间:2016-07-04 08:23:26

标签: ios caching memory

我试图通过调用此方法从文件中释放缓存:

    -(void)removeCacheFromFiles
    {
        NSFileManager *filemgr;

        filemgr = [NSFileManager defaultManager];
        NSError *error=nil;
        if ([filemgr removeItemAtPath: [NSHomeDirectory() stringByAppendingString:@"/Library/Caches"] error: &error]  == YES)
            NSLog (@"Remove successful");
        else
            NSLog (@"Remove failed");
            NSLog(@"error %@",error);

        [filemgr createDirectoryAtPath: [NSHomeDirectory() stringByAppendingString:@"/Library/Caches"] withIntermediateDirectories:NO attributes:nil error:nil];
    }

但我每次都会“删除失败”。

我也试过这个并获得相同的结果:

    -(void)removeCacheFromFiles
    {
        NSFileManager *filemgr;

        filemgr = [NSFileManager defaultManager];

        NSArray *cachePathList = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
        NSString *cachePath = [cachePathList objectAtIndex:0];
        NSError *error=nil;
        if ([filemgr removeItemAtPath:cachePath error: &error]  == YES)
            NSLog (@"Remove successful");
        else
            NSLog (@"Remove failed");

        NSLog(@"error %@");
        [filemgr createDirectoryAtPath: [NSHomeDirectory() stringByAppendingString:@"/Library/Caches"] withIntermediateDirectories:NO attributes:nil error:nil];
    }

此处删除缓存应该在哪里?在ViewDidDisappear或ViewDidAppear中调用它是否是一个好习惯,以便在第二次调用视图控制器时清除缓存?

NSLog中的错误说:

错误错误Domain = NSCocoaErrorDomain Code = 513“”缓存“无法删除,因为您无权访问它。” UserInfo = {NSUnderlyingError = 0x15fac9a0 {Error Domain = NSPOSIXErrorDomain Code = 1“操作不被允许”},NSFilePath = / var / mobile / Containers / Data / Application / 1795C59B-185F-4607-8682-004CDF3CDED7 / Library / Caches,NSUserStringVariant = (     去掉 )}

0 个答案:

没有答案