手機清除緩存



- (void)clearCachesAction{
    //刪除本地緩存
    NSLog(@"刪除本地緩存....");
    [self.hudManager With_label:@"正在刪除" duration:30];
    
    // 獲取Caches目錄路徑
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
    NSString *cachesDir = [paths objectAtIndex:0];
    NSString * cookiesDir =[cachesDir stringByReplacingOccurrencesOfString:@"Caches" withString:@"Cookies"];
    
    NSString * temp = NSTemporaryDirectory();
    
    NSLog(@"cachesDir:%@",cachesDir);
    NSLog(@"cookiesDir:%@",cookiesDir);
    
    //判斷目錄是否存在
    NSFileManager *fileManager = [NSFileManager defaultManager];
    BOOL isCachesExist = [fileManager fileExistsAtPath:cachesDir];
    BOOL isCookiesExist = [fileManager fileExistsAtPath:cookiesDir];
    BOOL isTempExist = [fileManager fileExistsAtPath:cookiesDir];
    
    NSError *error;
    //如果存在,則刪除目錄
    if (isCachesExist) {
        [fileManager removeItemAtPath:cachesDir error:&error];
        if (error) {
            NSLog(@"成功刪除Caches");
        }
        else{
            NSLog(@"error:%@",error);
        }
    }
    if (isCookiesExist) {
        [fileManager removeItemAtPath:cookiesDir error:&error];
        if (error) {
            NSLog(@"成功刪除Cookies");
        }
        else{
            NSLog(@"error:%@",error);
        }
    }
    if (isTempExist) {
        [fileManager removeItemAtPath:temp error:&error];
    }
    [self.hudManager hide];
    [MBProgressHUDManager showTextIncenter:@"成功刪除緩存"];
}



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章