簡體   English   中英

iOS清除緩存,模擬器是有效的,但在真實的iPhone中則無效

[英]iOS clear caches ,simulator is effective,but when in real iPhone is not

iOS清除緩存,我使用NSFileManager刪除了無用的文件,但是當我在模擬器中執行此操作時,它可以清除所有無用的文件,當我使用真實的iPhone(iPhone 5s)進行操作時,它無效,誰可以幫我解決這個問題? 非常感謝你!

碼:

#define docPath [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject]

#define cachePath [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject]

- (void)clearCaches
{
    [self clearCachesWithFilePath:cachePath];
    NSArray *docFileArr = [self getAllFileNames:docPath];
    for (NSString * fileName in docFileArr) {        
        if (![fileName isEqualToString:@"mqtt.cer"]) {
            [self clearCachesWithFilePath:[docPath stringByAppendingString:[NSString stringWithFormat:@"/%@", fileName]]];
        }
    }
}

- (NSArray *)getAllFileNames:(NSString *)dirPath{
    NSArray *files = [[NSFileManager defaultManager] subpathsOfDirectoryAtPath:dirPath error:nil];
    return files;
}

- (BOOL)clearCachesWithFilePath:(NSString *)path{
    NSFileManager *mgr = [NSFileManager defaultManager];
    return [mgr removeItemAtPath:path error:nil];
}

您試圖刪除緩存目錄本身,而是獲取緩存目錄中的內容列表,然后將它們一個一個地刪除。 希望這可以幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM